Trait sp_std::convert::AsRef

1.0.0 · source · []
pub trait AsRef<T> where
    T: ?Sized
{ fn as_ref(&self) -> &T; }
Expand description

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.

AsRef has the same signature as Borrow, but Borrow is different in few aspects:

  • Unlike AsRef, Borrow has a blanket impl for any T, and can be used to accept either a reference or a value.
  • Borrow also requires that Hash, Eq and Ord for 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)

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

Performs the conversion.

Implementations on Foreign Types

Implementors

impl AsRef<dyn Error + Send + Sync + 'static> for Error

impl AsRef<dyn Error + 'static> for Error

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

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

impl<T> AsRef<T> for Async<T>

impl AsRef<Path> for Components<'_>

impl AsRef<OsStr> for Components<'_>

impl AsRef<Path> for Component<'_>

impl AsRef<Path> for Iter<'_>

impl AsRef<OsStr> for Iter<'_>

impl AsRef<OsStr> for Path

impl AsRef<Path> for Path

impl AsRef<Path> for OsStr

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

impl AsRef<Path> for OsString

impl AsRef<Path> for str

impl AsRef<Path> for String

impl AsRef<Path> for PathBuf

impl AsRef<Path> for Path

impl AsRef<Path> for Path

impl AsRef<Path> for PathBuf

impl AsRef<OsStr> for PathBuf

impl AsRef<Path> for PathBuf

impl AsRef<str> for Mnemonic

impl AsRef<[u8]> for Seed

impl<O, V> AsRef<BitSlice<O, <V as BitView>::Store>> for BitArray<O, V> where
    O: BitOrder,
    V: BitView

impl<O, T> AsRef<BitSlice<O, T>> for Iter<'_, O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> AsRef<BitSlice<O, T>> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> AsRef<BitSlice<O, T>> for Drain<'_, O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> AsRef<BitSlice<O, T>> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore

impl AsRef<[u8]> for BString

impl AsRef<BStr> for BString

impl AsRef<[u8]> for BStr

impl AsRef<BStr> for [u8]

impl AsRef<BStr> for str

impl AsRef<[u8]> for Bytes

impl AsRef<[u8]> for BytesMut

impl<T: ?Sized + Pointable> AsRef<T> for Owned<T>

impl AsRef<[u8]> for Signature

impl AsRef<[u8]> for PublicKey

impl AsRef<[u8]> for SecretKey

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

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

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

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

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

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

impl AsRef<dyn Fail + 'static> for Error

impl<T: AsRef<[u8]>> AsRef<[u8]> for Window<T>

impl<T, N> AsRef<[T]> for GenericArray<T, N> where
    N: ArrayLength<T>, 

impl<T> AsRef<[T; 1]> for GenericArray<T, U1>

impl<T> AsRef<[T; 2]> for GenericArray<T, U2>

impl<T> AsRef<[T; 3]> for GenericArray<T, U3>

impl<T> AsRef<[T; 4]> for GenericArray<T, U4>

impl<T> AsRef<[T; 5]> for GenericArray<T, U5>

impl<T> AsRef<[T; 6]> for GenericArray<T, U6>

impl<T> AsRef<[T; 7]> for GenericArray<T, U7>

impl<T> AsRef<[T; 8]> for GenericArray<T, U8>

impl<T> AsRef<[T; 9]> for GenericArray<T, U9>

impl<T> AsRef<[T; 10]> for GenericArray<T, U10>

impl<T> AsRef<[T; 11]> for GenericArray<T, U11>

impl<T> AsRef<[T; 12]> for GenericArray<T, U12>

impl<T> AsRef<[T; 13]> for GenericArray<T, U13>

impl<T> AsRef<[T; 14]> for GenericArray<T, U14>

impl<T> AsRef<[T; 15]> for GenericArray<T, U15>

impl<T> AsRef<[T; 16]> for GenericArray<T, U16>

impl<T> AsRef<[T; 17]> for GenericArray<T, U17>

impl<T> AsRef<[T; 18]> for GenericArray<T, U18>

impl<T> AsRef<[T; 19]> for GenericArray<T, U19>

impl<T> AsRef<[T; 20]> for GenericArray<T, U20>

impl<T> AsRef<[T; 21]> for GenericArray<T, U21>

impl<T> AsRef<[T; 22]> for GenericArray<T, U22>

impl<T> AsRef<[T; 23]> for GenericArray<T, U23>

impl<T> AsRef<[T; 24]> for GenericArray<T, U24>

impl<T> AsRef<[T; 25]> for GenericArray<T, U25>

impl<T> AsRef<[T; 26]> for GenericArray<T, U26>

impl<T> AsRef<[T; 27]> for GenericArray<T, U27>

impl<T> AsRef<[T; 28]> for GenericArray<T, U28>

impl<T> AsRef<[T; 29]> for GenericArray<T, U29>

impl<T> AsRef<[T; 30]> for GenericArray<T, U30>

impl<T> AsRef<[T; 31]> for GenericArray<T, U31>

impl<T> AsRef<[T; 32]> for GenericArray<T, U32>

impl AsRef<str> for HeaderName

impl AsRef<str> for Method

impl AsRef<str> for Authority

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

impl AsRef<str> for Scheme

impl AsRef<JsValue> for Array

impl AsRef<Array> for Array

impl AsRef<Object> for Array

impl AsRef<Object> for Boolean

impl AsRef<JsValue> for Error

impl AsRef<Error> for Error

impl AsRef<Object> for Error

impl AsRef<JsValue> for Map

impl AsRef<Map> for Map

impl AsRef<Object> for Map

impl AsRef<JsValue> for Number

impl AsRef<Number> for Number

impl AsRef<Object> for Number

impl AsRef<JsValue> for Date

impl AsRef<Date> for Date

impl AsRef<Object> for Date

impl AsRef<JsValue> for Object

impl AsRef<Object> for Object

impl AsRef<JsValue> for Proxy

impl AsRef<Proxy> for Proxy

impl AsRef<JsValue> for RegExp

impl AsRef<RegExp> for RegExp

impl AsRef<Object> for RegExp

impl AsRef<JsValue> for Set

impl AsRef<Set> for Set

impl AsRef<Object> for Set

impl AsRef<Error> for UriError

impl AsRef<Object> for WeakMap

impl AsRef<Object> for WeakSet

impl AsRef<JsValue> for Module

impl AsRef<Module> for Module

impl AsRef<Object> for Module

impl AsRef<JsValue> for Table

impl AsRef<Table> for Table

impl AsRef<Object> for Table

impl AsRef<JsValue> for Global

impl AsRef<Global> for Global

impl AsRef<Object> for Global

impl AsRef<JsValue> for Memory

impl AsRef<Memory> for Memory

impl AsRef<Object> for Memory

impl AsRef<JsValue> for Symbol

impl AsRef<Symbol> for Symbol

impl AsRef<Object> for Promise

impl AsRef<[u8]> for SecretKey

impl<TKey: AsRef<KeyBytes>, TVal> AsRef<KeyBytes> for EntryView<TKey, TVal>

impl<T> AsRef<KeyBytes> for Key<T>

impl AsRef<[u8]> for Key

impl AsRef<[u8]> for X25519

impl<T: AsRef<[u8]> + Zeroize> AsRef<[u8]> for SecretKey<T>

impl<T: AsRef<[u8]>> AsRef<[u8]> for PublicKey<T>

impl<'k> AsRef<str> for Key<'k>

impl AsRef<[u8]> for Mmap

impl AsRef<[u8]> for MmapMut

impl AsRef<[u8]> for ByteSlice

impl<W> AsRef<W> for Encoder<W>

impl<T, S> AsRef<[T; 1]> for Matrix<T, U1, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U1>, 

impl<T, S> AsRef<[T; 2]> for Matrix<T, U1, U2, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U2>, 

impl<T, S> AsRef<[T; 3]> for Matrix<T, U1, U3, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U3>, 

impl<T, S> AsRef<[T; 4]> for Matrix<T, U1, U4, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U4>, 

impl<T, S> AsRef<[T; 5]> for Matrix<T, U1, U5, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U5>, 

impl<T, S> AsRef<[T; 6]> for Matrix<T, U1, U6, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U6>, 

impl<T, S> AsRef<[T; 7]> for Matrix<T, U1, U7, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U7>, 

impl<T, S> AsRef<[T; 8]> for Matrix<T, U1, U8, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U8>, 

impl<T, S> AsRef<[T; 9]> for Matrix<T, U1, U9, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U9>, 

impl<T, S> AsRef<[T; 10]> for Matrix<T, U1, U10, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U10>, 

impl<T, S> AsRef<[T; 11]> for Matrix<T, U1, U11, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U11>, 

impl<T, S> AsRef<[T; 12]> for Matrix<T, U1, U12, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U12>, 

impl<T, S> AsRef<[T; 13]> for Matrix<T, U1, U13, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U13>, 

impl<T, S> AsRef<[T; 14]> for Matrix<T, U1, U14, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U14>, 

impl<T, S> AsRef<[T; 15]> for Matrix<T, U1, U15, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U15>, 

impl<T, S> AsRef<[T; 16]> for Matrix<T, U1, U16, S> where
    T: Scalar,
    S: ContiguousStorage<T, U1, U16>, 

impl<T, S> AsRef<[T; 2]> for Matrix<T, U2, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U2, U1>, 

impl<T, S> AsRef<[T; 3]> for Matrix<T, U3, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U3, U1>, 

impl<T, S> AsRef<[T; 4]> for Matrix<T, U4, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U4, U1>, 

impl<T, S> AsRef<[T; 5]> for Matrix<T, U5, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U5, U1>, 

impl<T, S> AsRef<[T; 6]> for Matrix<T, U6, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U6, U1>, 

impl<T, S> AsRef<[T; 7]> for Matrix<T, U7, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U7, U1>, 

impl<T, S> AsRef<[T; 8]> for Matrix<T, U8, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U8, U1>, 

impl<T, S> AsRef<[T; 9]> for Matrix<T, U9, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U9, U1>, 

impl<T, S> AsRef<[T; 10]> for Matrix<T, U10, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U10, U1>, 

impl<T, S> AsRef<[T; 11]> for Matrix<T, U11, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U11, U1>, 

impl<T, S> AsRef<[T; 12]> for Matrix<T, U12, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U12, U1>, 

impl<T, S> AsRef<[T; 13]> for Matrix<T, U13, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U13, U1>, 

impl<T, S> AsRef<[T; 14]> for Matrix<T, U14, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U14, U1>, 

impl<T, S> AsRef<[T; 15]> for Matrix<T, U15, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U15, U1>, 

impl<T, S> AsRef<[T; 16]> for Matrix<T, U16, U1, S> where
    T: Scalar,
    S: ContiguousStorage<T, U16, U1>, 

impl<T: Scalar, S> AsRef<[[T; 2]; 2]> for Matrix<T, U2, U2, S> where
    S: ContiguousStorage<T, U2, U2>, 

impl<T: Scalar, S> AsRef<[[T; 2]; 3]> for Matrix<T, U2, U3, S> where
    S: ContiguousStorage<T, U2, U3>, 

impl<T: Scalar, S> AsRef<[[T; 2]; 4]> for Matrix<T, U2, U4, S> where
    S: ContiguousStorage<T, U2, U4>, 

impl<T: Scalar, S> AsRef<[[T; 2]; 5]> for Matrix<T, U2, U5, S> where
    S: ContiguousStorage<T, U2, U5>, 

impl<T: Scalar, S> AsRef<[[T; 2]; 6]> for Matrix<T, U2, U6, S> where
    S: ContiguousStorage<T, U2, U6>, 

impl<T: Scalar, S> AsRef<[[T; 3]; 2]> for Matrix<T, U3, U2, S> where
    S: ContiguousStorage<T, U3, U2>, 

impl<T: Scalar, S> AsRef<[[T; 3]; 3]> for Matrix<T, U3, U3, S> where
    S: ContiguousStorage<T, U3, U3>, 

impl<T: Scalar, S> AsRef<[[T; 3]; 4]> for Matrix<T, U3, U4, S> where
    S: ContiguousStorage<T, U3, U4>, 

impl<T: Scalar, S> AsRef<[[T; 3]; 5]> for Matrix<T, U3, U5, S> where
    S: ContiguousStorage<T, U3, U5>, 

impl<T: Scalar, S> AsRef<[[T; 3]; 6]> for Matrix<T, U3, U6, S> where
    S: ContiguousStorage<T, U3, U6>, 

impl<T: Scalar, S> AsRef<[[T; 4]; 2]> for Matrix<T, U4, U2, S> where
    S: ContiguousStorage<T, U4, U2>, 

impl<T: Scalar, S> AsRef<[[T; 4]; 3]> for Matrix<T, U4, U3, S> where
    S: ContiguousStorage<T, U4, U3>, 

impl<T: Scalar, S> AsRef<[[T; 4]; 4]> for Matrix<T, U4, U4, S> where
    S: ContiguousStorage<T, U4, U4>, 

impl<T: Scalar, S> AsRef<[[T; 4]; 5]> for Matrix<T, U4, U5, S> where
    S: ContiguousStorage<T, U4, U5>, 

impl<T: Scalar, S> AsRef<[[T; 4]; 6]> for Matrix<T, U4, U6, S> where
    S: ContiguousStorage<T, U4, U6>, 

impl<T: Scalar, S> AsRef<[[T; 5]; 2]> for Matrix<T, U5, U2, S> where
    S: ContiguousStorage<T, U5, U2>, 

impl<T: Scalar, S> AsRef<[[T; 5]; 3]> for Matrix<T, U5, U3, S> where
    S: ContiguousStorage<T, U5, U3>, 

impl<T: Scalar, S> AsRef<[[T; 5]; 4]> for Matrix<T, U5, U4, S> where
    S: ContiguousStorage<T, U5, U4>, 

impl<T: Scalar, S> AsRef<[[T; 5]; 5]> for Matrix<T, U5, U5, S> where
    S: ContiguousStorage<T, U5, U5>, 

impl<T: Scalar, S> AsRef<[[T; 5]; 6]> for Matrix<T, U5, U6, S> where
    S: ContiguousStorage<T, U5, U6>, 

impl<T: Scalar, S> AsRef<[[T; 6]; 2]> for Matrix<T, U6, U2, S> where
    S: ContiguousStorage<T, U6, U2>, 

impl<T: Scalar, S> AsRef<[[T; 6]; 3]> for Matrix<T, U6, U3, S> where
    S: ContiguousStorage<T, U6, U3>, 

impl<T: Scalar, S> AsRef<[[T; 6]; 4]> for Matrix<T, U6, U4, S> where
    S: ContiguousStorage<T, U6, U4>, 

impl<T: Scalar, S> AsRef<[[T; 6]; 5]> for Matrix<T, U6, U5, S> where
    S: ContiguousStorage<T, U6, U5>, 

impl<T: Scalar, S> AsRef<[[T; 6]; 6]> for Matrix<T, U6, U6, S> where
    S: ContiguousStorage<T, U6, U6>, 

impl<T> AsRef<T> for Unit<T>

impl<O, T: ?Sized> AsRef<T> for OwningRef<O, T>

impl<O, T: ?Sized> AsRef<T> for OwningRefMut<O, T>

impl AsRef<[u8]> for Multiaddr

impl AsRef<[u64]> for U128

impl AsRef<[u64]> for U256

impl AsRef<[u64]> for U512

impl AsRef<[u8]> for H128

impl AsRef<[u8]> for H160

impl AsRef<[u8]> for H256

impl AsRef<[u8]> for H512

impl AsRef<[u8]> for Literal

impl<A> AsRef<[u8]> for Aad<A> where
    A: AsRef<[u8]>, 

impl AsRef<[u8]> for Tag

impl AsRef<[u8; 12]> for Nonce

impl AsRef<[u8]> for PublicKey

impl AsRef<[u8]> for Digest

impl AsRef<[u8]> for Tag

impl AsRef<[u8]> for Document

impl AsRef<[u8]> for Signature

impl<'a> AsRef<[u8]> for DBPinnableSlice<'a>

impl AsRef<str> for Error

impl<E, ERef> AsRef<E> for ViableEpoch<E, ERef> where
    ERef: Borrow<E>, 

impl<E: Epoch> AsRef<E> for IncrementedEpoch<E>

impl AsRef<str> for ProtocolId

impl<Hash, Extrinsic> AsRef<Extrinsic> for Transaction<Hash, Extrinsic>

impl AsRef<[u8]> for PublicKey

impl AsRef<u64> for Uleb128

impl AsRef<i64> for Sleb128

impl<D: Digest> AsRef<[u8]> for SharedSecret<D>

impl<A: Array> AsRef<[<A as Array>::Item]> for SmallVec<A>

impl AsRef<[u8]> for ByteSlice125<'_>

impl AsRef<[u8]> for Storage<'_>

impl AsRef<Public> for Public

impl AsRef<[u8]> for Public

impl AsRef<[u8]> for Signature

impl AsRef<Pair> for Pair

impl AsRef<Public> for Public

impl AsRef<[u8]> for Public

impl AsRef<[u8]> for Signature

impl AsRef<Pair> for Pair

impl AsRef<Public> for Public

impl AsRef<[u8]> for Public

impl AsRef<[u8]> for Signature

impl AsRef<Pair> for Pair

impl AsRef<[u8]> for Dummy

impl AsRef<[u8]> for Public

impl AsRef<[u8]> for Signature

impl AsRef<[u8]> for Public

impl AsRef<[u8]> for Signature

impl AsRef<Keypair> for Pair

impl AsRef<[u8]> for Public

impl AsRef<[u8]> for Signature

impl AsRef<Public> for Keyring

impl AsRef<Public> for Keyring

impl AsRef<str> for Method

impl AsRef<Path> for TempDir

impl AsRef<Path> for TempPath

impl AsRef<OsStr> for TempPath

impl<A: Array> AsRef<[<A as Array>::Item]> for ArrayVec<A>

impl<'s, T> AsRef<[T]> for SliceVec<'s, T>

impl<A: Array> AsRef<[<A as Array>::Item]> for TinyVec<A>

impl AsRef<TcpStream> for ReadHalf<'_>

impl AsRef<TcpStream> for WriteHalf<'_>

impl<T: AsRef<[u8]>> AsRef<[u8]> for Window<T>

impl AsRef<str> for Field

impl<S: AsRef<str>> AsRef<str> for Ascii<S>

impl<S: AsRef<str>> AsRef<str> for UniCase<S>

impl AsRef<str> for Url

impl<T: ?Sized> AsRef<JsValue> for Closure<T>

impl<'a> AsRef<[RuntimeValue]> for RuntimeArgs<'a>

impl AsRef<str> for DNSName

impl<T: Binary> AsRef<T> for FmtBinary<T>

impl<T: Display> AsRef<T> for FmtDisplay<T>

impl<T: LowerExp> AsRef<T> for FmtLowerExp<T>

impl<T: LowerHex> AsRef<T> for FmtLowerHex<T>

impl<T: Octal> AsRef<T> for FmtOctal<T>

impl<T: Pointer> AsRef<T> for FmtPointer<T>

impl<T: UpperExp> AsRef<T> for FmtUpperExp<T>

impl<T: UpperHex> AsRef<T> for FmtUpperHex<T>

impl AsRef<[u8]> for Packet