# Impl AsRef\<Path\> for Cow\<str\>

**URL:** <https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201>\
**Category:** Uncategorized\
**Created:** [August 21, 2021, 8:44am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201 "2021-08-21T08:44:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidhewitt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davidhewitt/32/5902_2.png) [@davidhewitt](https://internals.rust-lang.org/u/davidhewitt)\
**Post date:** [August 21, 2021, 8:44am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/1 "2021-08-21T08:44:39Z")

</div>

I noticed that we have the following impls:

```rust
impl AsRef<Path> for OsStr
impl AsRef<Path> for str
impl AsRef<Path> for Cow<OsStr>

```

but not

```rust
impl AsRef<Path> for Cow<str>

```

Do you folks think this impl would be useful? Admittedly it's a bit edge-casey, and users can always deref the `Cow` (to get a `str`) in cases where they want to pass a `Cow<str>` to functions which take `AsRef<Path>`.

However from a symmetry argument, we already implemented for `Cow<OsStr>`, and arguably `Cow<str>` is a more common type from what I've seen, so the implementation may save users a few characters here and there.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [August 21, 2021, 10:32am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/2 "2021-08-21T10:32:40Z")

</div>

Yeah, send a PR with the impl. Looks like an oversight.

---

<div class="post-metadata">

**Author:** ![davidhewitt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davidhewitt/32/5902_2.png) [@davidhewitt](https://internals.rust-lang.org/u/davidhewitt)\
**Post date:** [August 27, 2021, 10:47pm UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/3 "2021-08-27T22:47:54Z")

</div>

Hmm, actually looks like it's not that simple. Adding this impl can break code, as `.as_ref()` on `Cow<str>` suddenly takes on a second meaning. It's probably not worth trying to figure this out!

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [August 28, 2021, 4:19am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/4 "2021-08-28T04:19:53Z")

</div>

> [@davidhewitt](#):
>
> Adding this impl can break code, as `.as_ref()` on `Cow<str>` suddenly takes on a second meaning.

That kind of breakage is relatively allowed, though. There have been plenty of `AsRef` additions that have caused things like that.

---

<div class="post-metadata">

**Author:** ![tcsc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tcsc/32/3806_2.png) [@tcsc](https://internals.rust-lang.org/u/tcsc)\
**Post date:** [August 28, 2021, 4:30am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/5 "2021-08-28T04:30:31Z")

</div>

I have a feeling that this one in particular is pretty common though, since getting a &str from a `Cow<str>` is a common operation (obviously), and this is one of the easier/more common ways to do it.

That's just to say, it _might_ be worth doing a crater run as a smoke test.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [August 28, 2021, 5:49am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/6 "2021-08-28T05:49:32Z")

</div>

👍 on a potential crater run. I don't think method resolution otherwise has a way to distinguish between which should be called.

---

<div class="post-metadata">

**Author:** ![davidhewitt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davidhewitt/32/5902_2.png) [@davidhewitt](https://internals.rust-lang.org/u/davidhewitt)\
**Post date:** [August 28, 2021, 6:22am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/7 "2021-08-28T06:22:09Z")

</div>

Yeah, I mean this really seems like it'll break a lot. If I clone the Rust repository, add this impl to the stdlib, and then try `./x.py test --stage 1`

```rust
error[E0282]: type annotations needed
    --> /home/david/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-1.4.6/src/re_unicode.rs:1247:14
     |
1247 | self.as_ref().replace_append(caps, dst)
     | -----^^^^^^--
     | | |
     | | cannot infer type for type parameter `T` declared on the trait `AsRef`
     | this method call resolves to `&T`
     |
     = note: type must be known at this point

error[E0282]: type annotations needed
    --> /home/david/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-1.4.6/src/re_unicode.rs:1257:14
     |
1257 | self.as_ref().replace_append(caps, dst)
     | -----^^^^^^--
     | | |
     | | cannot infer type for type parameter `T` declared on the trait `AsRef`
     | this method call resolves to `&T`
     |
     = note: type must be known at this point

```

... I'm only a very occasional contributor to the compiler, so I don't even know how to continue with the compiler testing framework without patching the `regex` crate 😂

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [August 28, 2021, 6:34am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/8 "2021-08-28T06:34:50Z")

</div>

> [@davidhewitt](#):
>
> Yeah, I mean this really seems like it'll break a lot.

You could probably add an inherent `as_ref` to `Cow` as a way to make it non-breaking.

Dunno if libs would be willing to go along with that, though.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [August 28, 2021, 8:14am UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/9 "2021-08-28T08:14:50Z")

</div>

^^ that's probably the only way to do it if we want this impl, short of special-casing the compiler.

---

<div class="post-metadata">

**Author:** ![davidhewitt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davidhewitt/32/5902_2.png) [@davidhewitt](https://internals.rust-lang.org/u/davidhewitt)\
**Post date:** [August 28, 2021, 9:43pm UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/10 "2021-08-28T21:43:56Z")

</div>

Agreed that inherent `as_ref` would be the way to go. Though I didn't notice prior art of an inherent `as_ref` anywhere else in the stdlib when I looked briefly earlier. I would argue that special-casing the compiler is undesirable - the array `.into_iter()` special case was _much_ more worth the effort than this nice-to-have.

Not sure I'm invested enough in having this impl at this point! I'm going to leave this unimplemented for now, though I guess anyone who wants this impl in the future always has this thread as a good starting block.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [August 28, 2021, 10:32pm UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/11 "2021-08-28T22:32:24Z")

</div>

> [@davidhewitt](#):
>
> Agreed that inherent `as_ref` would be the way to go.

I think that an inherent `as_ref` here would be a weird hack. Especially since the use-cases of `as_ref` for `&Cow<'_, T> -> &T` are covered by the `Deref` implementation of `Cow`. The two occurrences in `re_unicode` probably will work with the `.as_ref()` calls simply removed entirely (using the `Deref` for the method call then).

> [@davidhewitt](#):
>
> I would argue that special-casing the compiler is undesirable - the array `.into_iter()` special case was _much_ more worth the effort than this nice-to-have.

I think that aiming for a more general solution would be worth the effort of implementing such a special-casing mechanism. Ideally, if we could somehow make certain trait implementations take precedence over others for type inference (?) or method resolution (?) or whatever the most relevant mechanism at play here is, that’s a neat feature to improve the current situation of “allowed breakage” from adding trait impls in the standard library. Even for things less common than `Cow<str>`, we could avoid any potential for breakage entirely by somehow marking such new trait implementation as “newer” and hence “less important” an letting the compiler pretend it isn’t there in some contexts (i.e. in type inference or method resolution or where ever necessary). Similar to `.into_iter()`, we’d always eventually remove those special precedence rules over then next edition.

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [August 28, 2021, 10:38pm UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/12 "2021-08-28T22:38:31Z")

</div>

If we're getting an inherent method, wouldn't naming it `as_path` be better? That would at least match with the (presumably) `&Path` return type.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [August 28, 2021, 11:01pm UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/13 "2021-08-28T23:01:32Z")

</div>

> [@mathstuf](#):
>
> If we're getting an inherent method, wouldn't naming it `as_path` be better? That would at least match with the (presumably) `&Path` return type.

As far as I understand, the intention of the suggestion of an inherent method “`as_ref`” (particularly on `Cow<str>` taking `&self`, returning `&str`) was to avoid breakage in existing code. A different name isn’t an option, if that’s the goal. Notably, the inherent method was not meant for the conversion to `&Path`. The `AsRef` trait is commonly used as a trait bound on functions to make them less picky w.r.t. their argument type. An `fn foo(x: impl AsRef<Path>)` can take a `&PathBuf` or `&str` or `String` or an `OsString` or a `&&&Cow<'_, OsStr>`, etc. The change suggested here would allow such a method `foo` to not only accept `Cow<OsStr>` and `Cow<Path>` but also `Cow<str>`. One example of an existing such method is [`File::open`](https://doc.rust-lang.org/std/fs/struct.File.html#method.open).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 26, 2021, 11:02pm UTC](https://internals.rust-lang.org/t/impl-asref-path-for-cow-str/15201/14 "2021-11-26T23:02:06Z")

</div>

Closed on November 26, 2021, 11:02pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
