Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for string-like AsRef coverage #139429

Open
3 tasks
thaliaarchi opened this issue Apr 6, 2025 · 2 comments
Open
3 tasks

Tracking issue for string-like AsRef coverage #139429

thaliaarchi opened this issue Apr 6, 2025 · 2 comments
Assignees
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@thaliaarchi
Copy link
Contributor

thaliaarchi commented Apr 6, 2025

Almost all of the implementations of AsRef in core and std are for cheap conversions between string-like types. This tracks all of these such implementations and coordinates efforts to expand coverage.

Each column represents a type that can be converted to and each column represents a type that can be converted from. In the tables, ✅ marks an existing impl, ⚠️ marks an impl that I argue should be added, N/A marks an impl that does not make sense for that pair of types, and N/A? marks an impl that may make sense (via as_encoded_bytes). Impls that are from a more generic impl are marked with a footnote.

impl AsRef<T> for U AsRef​<str> AsRef​<OsStr> AsRef​<Path> AsRef​<ByteStr> AsRef​<[u8]>
str
String ⚠️
Cow<'_, str> 1 ⚠️ ⚠️ ⚠️ ⚠️
Box<str> 2 ⚠️ ⚠️ ⚠️ ⚠️
Rc<str> 3 ⚠️ ⚠️ ⚠️ ⚠️
Arc<str> 4 ⚠️ ⚠️ ⚠️ ⚠️
UniqueRc<str> 5 ⚠️ ⚠️ ⚠️ ⚠️
std::string::Drain<'_> ⚠️ ⚠️ ⚠️
OsStr N/A N/A? N/A?
OsString N/A N/A? N/A?
Cow<'_, OsStr> N/A 1 N/A? N/A?
Box<OsStr> N/A 2 ⚠️ N/A? N/A?
Rc<OsStr> N/A 3 ⚠️ N/A? N/A?
Arc<OsStr> N/A 4 ⚠️ N/A? N/A?
UniqueRc<OsStr> N/A 5 ⚠️ N/A? N/A?
Path N/A N/A? N/A?
PathBuf N/A N/A? N/A?
Cow<'_, Path> N/A ⚠️#139432 1 N/A? N/A?
Box<Path> N/A ⚠️ 2 N/A? N/A?
Rc<Path> N/A ⚠️ 3 N/A? N/A?
Arc<Path> N/A ⚠️ 4 N/A? N/A?
UniqueRc<Path> N/A ⚠️ 5 N/A? N/A?
std::path::Component<'_> N/A N/A? N/A?
std::path::Components<'_> N/A N/A? N/A?
std::path::Iter<'_> N/A N/A? N/A?
ByteStr N/A N/A N/A
ByteString N/A N/A N/A
Cow<'_, ByteStr> N/A N/A N/A 1 ⚠️
Box<ByteStr> N/A N/A N/A 2 ⚠️
Rc<ByteStr> N/A N/A N/A 3 ⚠️
Arc<ByteStr> N/A N/A N/A 4 ⚠️
UniqueRc<ByteStr> N/A N/A N/A 5 ⚠️
[u8] N/A N/A N/A ⚠️#139441 6
[u8; N] N/A N/A N/A ⚠️ 7
Vec<u8> N/A N/A N/A ⚠️ 8
Cow<'_, [u8]> N/A N/A N/A ⚠️ 1
Box<[u8]> N/A N/A N/A ⚠️ 2
Rc<[u8]> N/A N/A N/A ⚠️ 3
Arc<[u8]> N/A N/A N/A ⚠️ 4
UniqueRc<[u8]> N/A N/A N/A ⚠️ 5
std::slice::Iter<'_, u8> N/A N/A N/A ⚠️ 9
std::slice::IterMut<'_, u8> N/A N/A N/A ⚠️ 10
std::vec::IntoIter<u8> N/A N/A N/A ⚠️ 11
std::vec::Drain<'_, u8> N/A N/A N/A ⚠️ 12
Simd<u8, N> N/A N/A N/A ⚠️ 13

Although CStr is string-like, its AsStr impls do not interact with other string-like types:

impl AsRef<CStr> for T AsRef<CStr>
CStr
CString
Cow<'_, CStr> 1
Box<CStr> 2
Rc<CStr> 3
Arc<CStr> 4
UniqueRc<CStr> 5

Additionally, all of the above impls may be used with any number of references:

impl<T: AsRef<U> + ?Sized, U: ?Sized> AsRef<U> for &T
impl<T: AsRef<U> + ?Sized, U: ?Sized> AsRef<U> for &mut T

Steps

These are the steps I plan to break this effort into:

@rustbot claim

@rustbot label +T-libs-api

Footnotes

  1. Implemented via impl<T: ToOwned + ?Sized> AsRef<T> for Cow<'_, T> 2 3 4 5 6

  2. Implemented via impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> 2 3 4 5 6

  3. Implemented via impl<T: ?Sized, A: Allocator> AsRef<T> for Rc<T, A> 2 3 4 5 6

  4. Implemented via impl<T: ?Sized, A: Allocator> AsRef<T> for Arc<T, A> 2 3 4 5 6

  5. Implemented via impl<T: ?Sized, A: Allocator> AsRef<T> for UniqueRc<T, A> 2 3 4 5 6

  6. Implemented via impl<T> AsRef<[T]> for [T]

  7. Implemented via impl<T, const N: usize> AsRef<[T]> for [T; N]

  8. Implemented via impl<T, A: Allocator> AsRef<[T]> for Vec<T, A>

  9. Implemented via impl<T> AsRef<[T]> for std::slice::Iter<'_, T>

  10. Implemented via impl<T> AsRef<[T]> for std::slice::IterMut<'_, T>

  11. Implemented via impl<T, A: Allocator> AsRef<[T]> for std::vec::IntoIter<T, A>

  12. Implemented via impl<T, A: Allocator> AsRef<[T]> for std::vec::Drain<'_, T, A>

  13. Implemented via impl<T: SimdElement, const N: usize> AsRef<[T]> for Simd<T, N> where LaneCount<N>: SupportedLaneCount

@thaliaarchi thaliaarchi added the C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC label Apr 6, 2025
@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 6, 2025
@MatthijsKok
Copy link

MatthijsKok commented Apr 6, 2025

Nice work on the tracking issue!
It's super helpful that this information is nicely compiled in one place.

I've recently opened a PR that covers some of these, please check out #139318
Specifically:

  • impl AsRef<[u8]> for Box<str>
  • impl AsRef<[u8]> for Rc<str>
  • impl AsRef<OsStr> for Box<str>
  • impl AsRef<OsStr> for Rc<str>
  • impl AsRef<Path> for Box<str>
  • impl AsRef<Path> for Rc<str>

@thaliaarchi
Copy link
Contributor Author

@MatthijsKok Thanks for the PR. Let’s pause at these three PRs and wait for decisions on them, before pursuing more. I think, due to the high number of inference failures, they are unlikely to be merged, although I wish that wasn’t the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants