Trait AsRef

1.0.0 (const: unstable) · Source
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 any T, and can be used to accept either a reference or a value. (See also note on AsRef’s reflexibility below.)
  • Borrow also requires that Hash, Eq and Ord 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 implement AsRef, but not Borrow.

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§

1.0.0 · Source

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.

Trait Implementations§

§

impl ZReaderTrait for dyn AsRef<&[u8]>

§

fn get_byte(&self, index: usize) -> Option<&u8>

Get a single byte which is at position index Read more
§

fn get_slice(&self, index: Range<usize>) -> Option<&[u8]>

Get a slice of bytes from a range of start..end Read more
§

fn get_len(&self) -> usize

Get total length of the underlying buffer. Read more

Implementors§

1.0.0 (const: unstable) · Source§

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

1.0.0 · Source§

impl AsRef<str> for devela::all::String

Source§

impl AsRef<ByteStr> for str

Source§

impl AsRef<ByteStr> for ByteStr

Source§

impl AsRef<ByteStr> for ByteString

Source§

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]

1.7.0 · Source§

impl AsRef<CStr> for CStr

1.7.0 · Source§

impl AsRef<CStr> for CString

1.0.0 · Source§

impl AsRef<OsStr> for Component<'_>

1.0.0 · Source§

impl AsRef<OsStr> for str

1.0.0 · Source§

impl AsRef<OsStr> for devela::all::IterPath<'_>

1.0.0 · Source§

impl AsRef<OsStr> for Components<'_>

1.0.0 · Source§

impl AsRef<OsStr> for OsStr

1.0.0 · Source§

impl AsRef<OsStr> for OsString

1.0.0 · Source§

impl AsRef<OsStr> for Path

1.0.0 · Source§

impl AsRef<OsStr> for PathBuf

1.0.0 · Source§

impl AsRef<OsStr> for devela::all::String

1.8.0 · Source§

impl AsRef<Path> for Cow<'_, OsStr>

1.25.0 · Source§

impl AsRef<Path> for Component<'_>

1.0.0 · Source§

impl AsRef<Path> for str

1.0.0 · Source§

impl AsRef<Path> for devela::all::IterPath<'_>

1.0.0 · Source§

impl AsRef<Path> for Components<'_>

1.0.0 · Source§

impl AsRef<Path> for OsStr

1.0.0 · Source§

impl AsRef<Path> for OsString

1.0.0 · Source§

impl AsRef<Path> for Path

1.0.0 · Source§

impl AsRef<Path> for PathBuf

1.0.0 · Source§

impl AsRef<Path> for devela::all::String

Source§

impl AsRef<Uuid> for Braced

Source§

impl AsRef<Uuid> for Hyphenated

Source§

impl AsRef<Uuid> for Simple

Source§

impl AsRef<Uuid> for Urn

Source§

impl AsRef<Uuid> for Uuid

Source§

impl AsRef<[bool; 8]> for CodecFlags

Source§

impl AsRef<[f32; 2]> for Vec2

Source§

impl AsRef<[f32; 3]> for Vec3A

Source§

impl AsRef<[f32; 3]> for Vec3

Source§

impl AsRef<[f32; 4]> for Mat2

Source§

impl AsRef<[f32; 4]> for Quat

Source§

impl AsRef<[f32; 4]> for Vec4

Source§

impl AsRef<[f32; 9]> for Mat3

Source§

impl AsRef<[f32; 16]> for Mat4

Source§

impl AsRef<[f64; 2]> for DVec2

Source§

impl AsRef<[f64; 3]> for DVec3

Source§

impl AsRef<[f64; 4]> for DMat2

Source§

impl AsRef<[f64; 4]> for DQuat

Source§

impl AsRef<[f64; 4]> for DVec4

Source§

impl AsRef<[f64; 9]> for DMat3

Source§

impl AsRef<[f64; 16]> for DMat4

Source§

impl AsRef<[i8; 2]> for I8Vec2

Source§

impl AsRef<[i8; 3]> for I8Vec3

Source§

impl AsRef<[i8; 4]> for I8Vec4

Source§

impl AsRef<[i16; 2]> for I16Vec2

Source§

impl AsRef<[i16; 3]> for I16Vec3

Source§

impl AsRef<[i16; 4]> for I16Vec4

Source§

impl AsRef<[i32; 2]> for IVec2

Source§

impl AsRef<[i32; 3]> for IVec3

Source§

impl AsRef<[i32; 4]> for IVec4

Source§

impl AsRef<[i64; 2]> for I64Vec2

Source§

impl AsRef<[i64; 3]> for I64Vec3

Source§

impl AsRef<[i64; 4]> for I64Vec4

Source§

impl AsRef<[u8; 2]> for U8Vec2

Source§

impl AsRef<[u8; 3]> for U8Vec3

Source§

impl AsRef<[u8; 4]> for U8Vec4

1.0.0 · Source§

impl AsRef<[u8]> for str

Source§

impl AsRef<[u8]> for ByteStr

Source§

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

1.0.0 · Source§

impl AsRef<[u8]> for devela::all::String

Source§

impl AsRef<[u8]> for Uuid

§

impl AsRef<[u8]> for Bytes

§

impl AsRef<[u8]> for BytesMut

Source§

impl AsRef<[u16; 2]> for U16Vec2

Source§

impl AsRef<[u16; 3]> for U16Vec3

Source§

impl AsRef<[u16; 4]> for U16Vec4

Source§

impl AsRef<[u32; 2]> for UVec2

Source§

impl AsRef<[u32; 3]> for UVec3

Source§

impl AsRef<[u32; 4]> for UVec4

Source§

impl AsRef<[u64; 2]> for U64Vec2

Source§

impl AsRef<[u64; 3]> for U64Vec3

Source§

impl AsRef<[u64; 4]> for U64Vec4

Source§

impl AsRef<[usize; 2]> for USizeVec2

Source§

impl AsRef<[usize; 3]> for USizeVec3

Source§

impl AsRef<[usize; 4]> for USizeVec4

1.55.0 · Source§

impl<'a> AsRef<str> for devela::_dep::_alloc::string::Drain<'a>

§

impl<'a> AsRef<SurfaceRef> for Surface<'a>

1.55.0 · Source§

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,

1.46.0 · Source§

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,

Source§

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,

Source§

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>

Source§

impl<L, R> AsRef<str> for Either<L, R>
where L: AsRef<str>, R: AsRef<str>,

Source§

impl<L, R> AsRef<CStr> for Either<L, R>
where L: AsRef<CStr>, R: AsRef<CStr>,

Requires crate feature std.

Source§

impl<L, R> AsRef<OsStr> for Either<L, R>
where L: AsRef<OsStr>, R: AsRef<OsStr>,

Requires crate feature std.

Source§

impl<L, R> AsRef<Path> for Either<L, R>
where L: AsRef<Path>, R: AsRef<Path>,

Requires crate feature std.

Source§

impl<L, R, Target> AsRef<[Target]> for Either<L, R>
where L: AsRef<[Target]>, R: AsRef<[Target]>,

Source§

impl<L, R, Target> AsRef<Target> for Either<L, R>
where L: AsRef<Target>, R: AsRef<Target>,

§

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>

1.0.0 (const: unstable) · Source§

impl<T> AsRef<[T]> for [T]

1.13.0 · Source§

impl<T> AsRef<[T]> for devela::_core::slice::Iter<'_, T>

1.53.0 · Source§

impl<T> AsRef<[T]> for IterMut<'_, T>

§

impl<T> AsRef<[T]> for CVec<T>

§

impl<T> AsRef<str> for Port<T>
where T: AsRef<str>,

§

impl<T> AsRef<Py<PyAny>> for Borrowed<'_, '_, T>

§

impl<T> AsRef<Py<PyAny>> for Bound<'_, T>

§

impl<T> AsRef<Py<PyAny>> for Py<T>

1.0.0 · Source§

impl<T> AsRef<T> for Cow<'_, T>
where T: ToOwned + ?Sized,

§

impl<T> AsRef<T> for devela::all::Arc<T>
where T: ?Sized,

Source§

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>

1.46.0 · Source§

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,

1.0.0 · Source§

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,

1.0.0 · Source§

impl<T, A> AsRef<Vec<T, A>> for devela::all::Vec<T, A>
where A: Allocator,

Source§

impl<T, A> AsRef<T> for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

1.5.0 · Source§

impl<T, A> AsRef<T> for devela::_dep::_alloc::sync::Arc<T, A>
where A: Allocator, T: ?Sized,

Source§

impl<T, A> AsRef<T> for UniqueArc<T, A>
where A: Allocator, T: ?Sized,

§

impl<T, A> AsRef<T> for devela::_dep::allocator_api2::boxed::Box<T, A>
where A: Allocator, T: ?Sized,

1.5.0 · Source§

impl<T, A> AsRef<T> for devela::all::Box<T, A>
where A: Allocator, T: ?Sized,

1.5.0 · Source§

impl<T, A> AsRef<T> for Rc<T, A>
where A: Allocator, T: ?Sized,

§

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,

1.0.0 (const: unstable) · Source§

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

1.0.0 (const: unstable) · Source§

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

§

impl<T, U> AsRef<U> for PyRef<'_, T>
where T: PyClass<BaseType = U>, U: PyClass,

§

impl<T, U> AsRef<U> for PyRefMut<'_, T>
where U: PyClass<Frozen = False>, T: PyClass<BaseType = U, Frozen = False>,

§

impl<T, Z> AsRef<T> for Zeroizing<Z>
where Z: AsRef<T> + Zeroize, T: ?Sized,

Source§

impl<T, const CAP: usize> AsRef<[T]> for ArrayVec<T, CAP>

Source§

impl<T, const CAP: usize, S: Storage> AsRef<[T; CAP]> for Array<T, CAP, S>

Source§

impl<T, const N: usize> AsRef<[T; N]> for Simd<T, N>

1.0.0 · Source§

impl<T, const N: usize> AsRef<[T]> for [T; N]

Source§

impl<T, const N: usize> AsRef<[T]> for Simd<T, N>

Source§

impl<const CAP: usize> AsRef<str> for StringNonul<CAP>

Available on crate feature _str_nonul only.
Source§

impl<const CAP: usize> AsRef<str> for StringU8<CAP>

Source§

impl<const CAP: usize> AsRef<str> for ArrayString<CAP>

Source§

impl<const CAP: usize> AsRef<OsStr> for StringU8<CAP>

Available on crate feature std and (Unix or WASI) only.
Source§

impl<const CAP: usize> AsRef<Path> for ArrayString<CAP>

Source§

impl<const CAP: usize> AsRef<[u8]> for StringNonul<CAP>

Available on crate feature _str_nonul only.
Source§

impl<const CAP: usize> AsRef<[u8]> for StringU8<CAP>