Description
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, 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 | ✅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 | ✅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:
- Implement
AsRef<OsStr>
forCow<'_, Path>
#139432 - Implement
AsRef<ByteStr>
for[u8]
#139441 - Implement
AsRef<{OsStr, Path}>
forstd::string::Drain<'_>
. - Bring
AsRef<ByteStr>
up to parity withAsRef<[u8]>
and implementAsRef<[u8]>
forCow<'_, ByteStr>
. - Implement
AsRef
forBox
,Rc
,Arc
, andUniqueRc
containers on non-self types, as marked in the table.
@rustbot claim
@rustbot label +T-libs-api
Footnotes
-
Implemented via
impl<T: ToOwned + ?Sized> AsRef<T> for Cow<'_, T>
↩ ↩2 ↩3 ↩4 ↩5 ↩6 -
Implemented via
impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A>
↩ ↩2 ↩3 ↩4 ↩5 ↩6 -
Implemented via
impl<T: ?Sized, A: Allocator> AsRef<T> for Rc<T, A>
↩ ↩2 ↩3 ↩4 ↩5 ↩6 -
Implemented via
impl<T: ?Sized, A: Allocator> AsRef<T> for Arc<T, A>
↩ ↩2 ↩3 ↩4 ↩5 ↩6 -
Implemented via
impl<T: ?Sized, A: Allocator> AsRef<T> for UniqueRc<T, A>
↩ ↩2 ↩3 ↩4 ↩5 ↩6 -
Implemented via
impl<T> AsRef<[T]> for [T]
↩ -
Implemented via
impl<T, const N: usize> AsRef<[T]> for [T; N]
↩ -
Implemented via
impl<T, A: Allocator> AsRef<[T]> for Vec<T, A>
↩ -
Implemented via
impl<T> AsRef<[T]> for std::slice::Iter<'_, T>
↩ -
Implemented via
impl<T> AsRef<[T]> for std::slice::IterMut<'_, T>
↩ -
Implemented via
impl<T, A: Allocator> AsRef<[T]> for std::vec::IntoIter<T, A>
↩ -
Implemented via
impl<T, A: Allocator> AsRef<[T]> for std::vec::Drain<'_, T, A>
↩ -
Implemented via
impl<T: SimdElement, const N: usize> AsRef<[T]> for Simd<T, N> where LaneCount<N>: SupportedLaneCount
↩