Skip to content

Tracking issue for string-like AsRef coverage #139429

Open
@thaliaarchi

Description

@thaliaarchi

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

Metadata

Metadata

Assignees

Labels

C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions