pub trait AsRef<T>where
T: ?Sized,{
// Required method
fn as_ref(&self) -> &T;
}
Expand description
core
Used to do a cheap reference-to-reference conversion.
Re-exported from core
::convert::
.
Used to do a cheap reference-to-reference conversion.
This trait is similar to AsMut
which is used for converting between mutable references.
If you need to do a costly conversion it is better to implement From
with type
&T
or write a custom function.
§Relation to Borrow
AsRef
has the same signature as Borrow
, but Borrow
is different in a few aspects:
- Unlike
AsRef
,Borrow
has a blanket impl for anyT
, and can be used to accept either a reference or a value. (See also note onAsRef
’s reflexibility below.) Borrow
also requires thatHash
,Eq
andOrd
for a borrowed value are equivalent to those of the owned value. For this reason, if you want to borrow only a single field of a struct you can implementAsRef
, but notBorrow
.
Note: This trait must not fail. If the conversion can fail, use a
dedicated method which returns an Option<T>
or a Result<T, E>
.
§Generic Implementations
AsRef
auto-dereferences if the inner type is a reference or a mutable reference
(e.g.: foo.as_ref()
will work the same if foo
has type &mut Foo
or &&mut Foo
).
Note that due to historic reasons, the above currently does not hold generally for all
dereferenceable types, e.g. foo.as_ref()
will not work the same as
Box::new(foo).as_ref()
. Instead, many smart pointers provide an as_ref
implementation which
simply returns a reference to the pointed-to value (but do not perform a cheap
reference-to-reference conversion for that value). However, AsRef::as_ref
should not be
used for the sole purpose of dereferencing; instead ‘Deref
coercion’ can be used:
let x = Box::new(5i32);
// Avoid this:
// let y: &i32 = x.as_ref();
// Better just write:
let y: &i32 = &x;
Types which implement Deref
should consider implementing AsRef<T>
as follows:
impl<T> AsRef<T> for SomeType
where
T: ?Sized,
<SomeType as Deref>::Target: AsRef<T>,
{
fn as_ref(&self) -> &T {
self.deref().as_ref()
}
}
§Reflexivity
Ideally, AsRef
would be reflexive, i.e. there would be an impl<T: ?Sized> AsRef<T> for T
with as_ref
simply returning its argument unchanged.
Such a blanket implementation is currently not provided due to technical restrictions of
Rust’s type system (it would be overlapping with another existing blanket implementation for
&T where T: AsRef<U>
which allows AsRef
to auto-dereference, see “Generic Implementations”
above).
A trivial implementation of AsRef<T> for T
must be added explicitly for a particular type T
where needed or desired. Note, however, that not all types from std
contain such an
implementation, and those cannot be added by external code due to orphan rules.
§Examples
By using trait bounds we can accept arguments of different types as long as they can be
converted to the specified type T
.
For example: By creating a generic function that takes an AsRef<str>
we express that we
want to accept all references that can be converted to &str
as an argument.
Since both String
and &str
implement AsRef<str>
we can accept both as input argument.
fn is_hello<T: AsRef<str>>(s: T) {
assert_eq!("hello", s.as_ref());
}
let s = "hello";
is_hello(s);
let s = "hello".to_string();
is_hello(s);
Required Methods§
Trait Implementations§
Implementors§
impl AsRef<str> for str
impl AsRef<str> for PyBackedStr
impl AsRef<str> for HeaderName
impl AsRef<str> for Method
impl AsRef<str> for Authority
impl AsRef<str> for Scheme
impl AsRef<str> for devela::all::String
impl AsRef<ByteStr> for str
impl AsRef<ByteStr> for ByteStr
impl AsRef<ByteStr> for ByteString
impl AsRef<LocalWaker> for Waker
impl AsRef<ContentStyle> for ContentStyle
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::decoder::Audio
impl AsRef<Context> for Decoder
impl AsRef<Context> for Opened
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::decoder::Subtitle
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::decoder::Video
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::encoder::audio::Audio
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::encoder::Audio
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::encoder::Encoder
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::encoder::Subtitle
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::encoder::Video
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::encoder::subtitle::Subtitle
impl AsRef<Context> for devela::_dep::ffmpeg_the_third::codec::encoder::video::Video
impl AsRef<timespec> for TimeSpec
impl AsRef<timeval> for TimeVal
impl AsRef<SurfaceRef> for SurfaceRef
impl AsRef<SDL_FPoint> for FPoint
impl AsRef<SDL_FRect> for FRect
impl AsRef<SDL_Point> for Point
impl AsRef<SDL_Rect> for Rect
impl AsRef<BStr> for str
impl AsRef<BStr> for [u8]
impl AsRef<Bytes> for str
impl AsRef<Bytes> for [u8]
impl AsRef<CStr> for CStr
impl AsRef<CStr> for CString
impl AsRef<OsStr> for Component<'_>
impl AsRef<OsStr> for str
impl AsRef<OsStr> for devela::all::IterPath<'_>
impl AsRef<OsStr> for Components<'_>
impl AsRef<OsStr> for OsStr
impl AsRef<OsStr> for OsString
impl AsRef<OsStr> for Path
impl AsRef<OsStr> for PathBuf
impl AsRef<OsStr> for devela::all::String
impl AsRef<Path> for Cow<'_, OsStr>
impl AsRef<Path> for Component<'_>
impl AsRef<Path> for str
impl AsRef<Path> for devela::all::IterPath<'_>
impl AsRef<Path> for Components<'_>
impl AsRef<Path> for OsStr
impl AsRef<Path> for OsString
impl AsRef<Path> for Path
impl AsRef<Path> for PathBuf
impl AsRef<Path> for devela::all::String
impl AsRef<Uuid> for Braced
impl AsRef<Uuid> for Hyphenated
impl AsRef<Uuid> for Simple
impl AsRef<Uuid> for Urn
impl AsRef<Uuid> for Uuid
impl AsRef<[bool; 8]> for CodecFlags
impl AsRef<[f32; 2]> for Vec2
impl AsRef<[f32; 3]> for Vec3A
impl AsRef<[f32; 3]> for Vec3
impl AsRef<[f32; 4]> for Mat2
impl AsRef<[f32; 4]> for Quat
impl AsRef<[f32; 4]> for Vec4
impl AsRef<[f32; 9]> for Mat3
impl AsRef<[f32; 16]> for Mat4
impl AsRef<[f64; 2]> for DVec2
impl AsRef<[f64; 3]> for DVec3
impl AsRef<[f64; 4]> for DMat2
impl AsRef<[f64; 4]> for DQuat
impl AsRef<[f64; 4]> for DVec4
impl AsRef<[f64; 9]> for DMat3
impl AsRef<[f64; 16]> for DMat4
impl AsRef<[i8; 2]> for I8Vec2
impl AsRef<[i8; 3]> for I8Vec3
impl AsRef<[i8; 4]> for I8Vec4
impl AsRef<[i16; 2]> for I16Vec2
impl AsRef<[i16; 3]> for I16Vec3
impl AsRef<[i16; 4]> for I16Vec4
impl AsRef<[i32; 2]> for IVec2
impl AsRef<[i32; 3]> for IVec3
impl AsRef<[i32; 4]> for IVec4
impl AsRef<[i64; 2]> for I64Vec2
impl AsRef<[i64; 3]> for I64Vec3
impl AsRef<[i64; 4]> for I64Vec4
impl AsRef<[u8; 2]> for U8Vec2
impl AsRef<[u8; 3]> for U8Vec3
impl AsRef<[u8; 4]> for U8Vec4
impl AsRef<[u8]> for str
impl AsRef<[u8]> for ByteStr
impl AsRef<[u8]> for ByteString
impl AsRef<[u8]> for devela::_dep::orion::aead::streaming::Nonce
impl AsRef<[u8]> for devela::_dep::orion::hash::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::aead::chacha20poly1305::Nonce
impl AsRef<[u8]> for devela::_dep::orion::hazardous::hash::sha2::sha256::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::hash::sha2::sha384::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::hash::sha2::sha512::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::hash::sha3::sha3_224::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::hash::sha3::sha3_256::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::hash::sha3::sha3_384::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::hash::sha3::sha3_512::Digest
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::mlkem512::Ciphertext
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::mlkem512::EncapsulationKey
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::mlkem768::Ciphertext
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::mlkem768::EncapsulationKey
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::mlkem1024::Ciphertext
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::mlkem1024::EncapsulationKey
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::xwing::Ciphertext
impl AsRef<[u8]> for devela::_dep::orion::hazardous::kem::xwing::EncapsulationKey
impl AsRef<[u8]> for Salt
impl AsRef<[u8]> for PyBackedBytes
impl AsRef<[u8]> for PyBackedStr
impl AsRef<[u8]> for HeaderName
impl AsRef<[u8]> for HeaderValue
impl AsRef<[u8]> for BStr
impl AsRef<[u8]> for devela::_dep::winnow::Bytes
impl AsRef<[u8]> for devela::all::String
impl AsRef<[u8]> for Uuid
impl AsRef<[u8]> for Bytes
impl AsRef<[u8]> for BytesMut
impl AsRef<[u16; 2]> for U16Vec2
impl AsRef<[u16; 3]> for U16Vec3
impl AsRef<[u16; 4]> for U16Vec4
impl AsRef<[u32; 2]> for UVec2
impl AsRef<[u32; 3]> for UVec3
impl AsRef<[u32; 4]> for UVec4
impl AsRef<[u64; 2]> for U64Vec2
impl AsRef<[u64; 3]> for U64Vec3
impl AsRef<[u64; 4]> for U64Vec4
impl AsRef<[usize; 2]> for USizeVec2
impl AsRef<[usize; 3]> for USizeVec3
impl AsRef<[usize; 4]> for USizeVec4
impl<'a> AsRef<str> for devela::_dep::_alloc::string::Drain<'a>
impl<'a> AsRef<SurfaceRef> for Surface<'a>
impl<'a> AsRef<[u8]> for devela::_dep::_alloc::string::Drain<'a>
impl<'a, T> AsRef<[T]> for CSlice<'a, T>
impl<'a, T> AsRef<[T]> for CSliceMut<'a, T>
impl<'a, T> AsRef<T> for devela::_dep::bumpalo::boxed::Box<'a, T>where
T: ?Sized,
impl<'a, T, A> AsRef<[T]> for devela::_dep::_alloc::vec::Drain<'a, T, A>where
A: Allocator,
impl<'a, T, A> AsRef<[T]> for devela::_dep::allocator_api2::vec::Drain<'a, T, A>where
A: Allocator,
impl<'a, T, A> AsRef<[T]> for Drain<'a, T, A>where
A: Allocator,
impl<'a, T: ?Sized + Ownership> AsRef<T> for MaybeOwned<'a, T>
impl<'bump> AsRef<str> for devela::_dep::bumpalo::collections::String<'bump>
impl<'bump> AsRef<[u8]> for devela::_dep::bumpalo::collections::String<'bump>
impl<'bump, T> AsRef<[T]> for devela::_dep::bumpalo::collections::Vec<'bump, T>where
T: 'bump,
impl<'bump, T> AsRef<Vec<'bump, T>> for devela::_dep::bumpalo::collections::Vec<'bump, T>where
T: 'bump,
impl<'py, T> AsRef<Bound<'py, PyAny>> for Bound<'py, T>
impl<A> AsRef<[<A as Array>::Item]> for SmallVec<A>where
A: Array,
impl<D> AsRef<ContentStyle> for StyledContent<D>where
D: Display,
impl<E, F> AsRef<E> for CodecIf<E, F>
impl<I> AsRef<I> for LocatingSlice<I>
impl<I, S> AsRef<I> for Stateful<I, S>
impl<L, R> AsRef<str> for Either<L, R>
impl<L, R> AsRef<CStr> for Either<L, R>
Requires crate feature std
.
impl<L, R> AsRef<OsStr> for Either<L, R>
Requires crate feature std
.
impl<L, R> AsRef<Path> for Either<L, R>
Requires crate feature std
.
impl<L, R, Target> AsRef<[Target]> for Either<L, R>
impl<L, R, Target> AsRef<Target> for Either<L, R>
impl<T> AsRef<[T; 2]> for Point2<T>
impl<T> AsRef<[T; 2]> for Vector2<T>
impl<T> AsRef<[T; 3]> for Point3<T>
impl<T> AsRef<[T; 3]> for Vector3<T>
impl<T> AsRef<[T; 4]> for ColumnMatrix2<T>
impl<T> AsRef<[T; 4]> for Quaternion<T>
impl<T> AsRef<[T; 4]> for RowMatrix2<T>
impl<T> AsRef<[T; 4]> for Vector4<T>
impl<T> AsRef<[T; 6]> for ColumnMatrix2x3<T>
impl<T> AsRef<[T; 6]> for ColumnMatrix3x2<T>
impl<T> AsRef<[T; 6]> for RowMatrix2x3<T>
impl<T> AsRef<[T; 6]> for RowMatrix3x2<T>
impl<T> AsRef<[T; 8]> for ColumnMatrix2x4<T>
impl<T> AsRef<[T; 8]> for ColumnMatrix4x2<T>
impl<T> AsRef<[T; 8]> for RowMatrix2x4<T>
impl<T> AsRef<[T; 8]> for RowMatrix4x2<T>
impl<T> AsRef<[T; 9]> for ColumnMatrix3<T>
impl<T> AsRef<[T; 9]> for RowMatrix3<T>
impl<T> AsRef<[T; 12]> for ColumnMatrix3x4<T>
impl<T> AsRef<[T; 12]> for ColumnMatrix4x3<T>
impl<T> AsRef<[T; 12]> for RowMatrix3x4<T>
impl<T> AsRef<[T; 12]> for RowMatrix4x3<T>
impl<T> AsRef<[T; 16]> for ColumnMatrix4<T>
impl<T> AsRef<[T; 16]> for RowMatrix4<T>
impl<T> AsRef<[T]> for [T]
impl<T> AsRef<[T]> for devela::_core::slice::Iter<'_, T>
impl<T> AsRef<[T]> for IterMut<'_, T>
impl<T> AsRef<[T]> for CVec<T>
impl<T> AsRef<str> for Port<T>
impl<T> AsRef<Py<PyAny>> for Borrowed<'_, '_, T>
impl<T> AsRef<Py<PyAny>> for Bound<'_, T>
impl<T> AsRef<Py<PyAny>> for Py<T>
impl<T> AsRef<T> for Cow<'_, T>
impl<T> AsRef<T> for devela::all::Arc<T>where
T: ?Sized,
impl<T> AsRef<T> for BareBox<T>
impl<T> AsRef<T> for Owned<T>where
T: Pointable + ?Sized,
impl<T> AsRef<T> for Spanned<T>
impl<T> AsRef<[[T; 2]; 2]> for ColumnMatrix2<T>
impl<T> AsRef<[[T; 2]; 2]> for RowMatrix2<T>
impl<T> AsRef<[[T; 2]; 3]> for ColumnMatrix2x3<T>
impl<T> AsRef<[[T; 2]; 3]> for RowMatrix3x2<T>
impl<T> AsRef<[[T; 2]; 4]> for ColumnMatrix2x4<T>
impl<T> AsRef<[[T; 2]; 4]> for RowMatrix4x2<T>
impl<T> AsRef<[[T; 3]; 2]> for ColumnMatrix3x2<T>
impl<T> AsRef<[[T; 3]; 2]> for RowMatrix2x3<T>
impl<T> AsRef<[[T; 3]; 3]> for ColumnMatrix3<T>
impl<T> AsRef<[[T; 3]; 3]> for RowMatrix3<T>
impl<T> AsRef<[[T; 3]; 4]> for ColumnMatrix3x4<T>
impl<T> AsRef<[[T; 3]; 4]> for RowMatrix4x3<T>
impl<T> AsRef<[[T; 4]; 2]> for ColumnMatrix4x2<T>
impl<T> AsRef<[[T; 4]; 2]> for RowMatrix2x4<T>
impl<T> AsRef<[[T; 4]; 3]> for ColumnMatrix4x3<T>
impl<T> AsRef<[[T; 4]; 3]> for RowMatrix3x4<T>
impl<T> AsRef<[[T; 4]; 4]> for ColumnMatrix4<T>
impl<T> AsRef<[[T; 4]; 4]> for RowMatrix4<T>
impl<T, A> AsRef<[T]> for devela::_dep::_alloc::vec::IntoIter<T, A>where
A: Allocator,
impl<T, A> AsRef<[T]> for devela::_dep::allocator_api2::vec::IntoIter<T, A>where
A: Allocator,
impl<T, A> AsRef<[T]> for devela::_dep::allocator_api2::vec::Vec<T, A>where
A: Allocator,
impl<T, A> AsRef<[T]> for devela::all::Vec<T, A>where
A: Allocator,
impl<T, A> AsRef<[T]> for IntoIter<T, A>where
A: Allocator,
impl<T, A> AsRef<[T]> for Vec<T, A>where
A: Allocator,
impl<T, A> AsRef<Vec<T, A>> for devela::_dep::allocator_api2::vec::Vec<T, A>where
A: Allocator,
impl<T, A> AsRef<Vec<T, A>> for devela::all::Vec<T, A>where
A: Allocator,
impl<T, A> AsRef<T> for UniqueRc<T, A>
impl<T, A> AsRef<T> for devela::_dep::_alloc::sync::Arc<T, A>
impl<T, A> AsRef<T> for UniqueArc<T, A>
impl<T, A> AsRef<T> for devela::_dep::allocator_api2::boxed::Box<T, A>
impl<T, A> AsRef<T> for devela::all::Box<T, A>
impl<T, A> AsRef<T> for Rc<T, A>
impl<T, A> AsRef<T> for Box<T, A>where
A: Allocator,
T: ?Sized,
impl<T, A> AsRef<Vec<T, A>> for Vec<T, A>where
A: Allocator,
impl<T, U> AsRef<U> for &T
impl<T, U> AsRef<U> for &mut T
impl<T, U> AsRef<U> for PyRef<'_, T>
impl<T, U> AsRef<U> for PyRefMut<'_, T>
impl<T, Z> AsRef<T> for Zeroizing<Z>
impl<T, const CAP: usize> AsRef<[T]> for ArrayVec<T, CAP>
impl<T, const CAP: usize, S: Storage> AsRef<[T; CAP]> for Array<T, CAP, S>
impl<T, const N: usize> AsRef<[T; N]> for Simd<T, N>
impl<T, const N: usize> AsRef<[T]> for [T; N]
impl<T, const N: usize> AsRef<[T]> for Simd<T, N>
impl<const CAP: usize> AsRef<str> for StringNonul<CAP>
_str_nonul
only.impl<const CAP: usize> AsRef<str> for StringU8<CAP>
impl<const CAP: usize> AsRef<str> for ArrayString<CAP>
impl<const CAP: usize> AsRef<OsStr> for StringU8<CAP>
std
and (Unix or WASI) only.impl<const CAP: usize> AsRef<Path> for ArrayString<CAP>
impl<const CAP: usize> AsRef<[u8]> for StringNonul<CAP>
_str_nonul
only.