Trait sp_std::convert::Into

1.0.0 · source · []
pub trait Into<T> {
    fn into(self) -> T;
}
Expand description

A value-to-value conversion that consumes the input value. The opposite of From.

One should avoid implementing Into and implement From instead. Implementing From automatically provides one with an implementation of Into thanks to the blanket implementation in the standard library.

Prefer using Into over From when specifying trait bounds on a generic function to ensure that types that only implement Into can be used as well.

Note: This trait must not fail. If the conversion can fail, use TryInto.

Generic Implementations

  • From<T> for U implies Into<U> for T
  • Into is reflexive, which means that Into<T> for T is implemented

Implementing Into for conversions to external types in old versions of Rust

Prior to Rust 1.41, if the destination type was not part of the current crate then you couldn’t implement From directly. For example, take this code:

struct Wrapper<T>(Vec<T>);
impl<T> From<Wrapper<T>> for Vec<T> {
    fn from(w: Wrapper<T>) -> Vec<T> {
        w.0
    }
}

This will fail to compile in older versions of the language because Rust’s orphaning rules used to be a little bit more strict. To bypass this, you could implement Into directly:

struct Wrapper<T>(Vec<T>);
impl<T> Into<Vec<T>> for Wrapper<T> {
    fn into(self) -> Vec<T> {
        self.0
    }
}

It is important to understand that Into does not provide a From implementation (as From does with Into). Therefore, you should always try to implement From and then fall back to Into if From can’t be implemented.

Examples

String implements Into<Vec<u8>>:

In order to express that we want a generic function to take all arguments that can be converted to a specified type T, we can use a trait bound of Into<T>. For example: The function is_hello takes all arguments that can be converted into a Vec<u8>.

fn is_hello<T: Into<Vec<u8>>>(s: T) {
   let bytes = b"hello".to_vec();
   assert_eq!(bytes, s.into());
}

let s = "hello".to_string();
is_hello(s);

Required methods

Performs the conversion.

Implementations on Foreign Types

Implementors

impl<'a> Into<&'a Path> for &'a Path

impl Into<PathBuf> for PathBuf

impl<M, O, T> Into<Range<BitPtr<M, O, T>>> for BitPtrRange<M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<O, T> Into<Box<[T], Global>> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Into<Vec<T, Global>> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore

impl Into<i64> for Imm64

impl Into<u64> for Uimm64

impl Into<u32> for Uimm32

impl Into<i64> for Uimm32

impl Into<i32> for Offset32

impl Into<i64> for Offset32

impl Into<u8> for OpcodePrefix

impl<T: ReservedValue> Into<Option<T>> for PackedOption<T>

impl Into<u16> for Opcode

impl Into<u8> for ResponseCode

impl<L, R> Into<Result<R, L>> for Either<L, R>

impl Into<i32> for Errno

impl Into<u64> for Pointer

impl<'input, Endian> Into<&'input [u8]> for EndianSlice<'input, Endian> where
    Endian: Endianity

impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B>

impl<T: PubSubMetadata, S: Middleware<T>> Into<MetaIoHandler<T, S>> for PubSubHandler<T, S>

impl<T> Into<Option<T>> for AllowCors<T>

impl<T> Into<Option<Vec<T, Global>>> for DomainsValidation<T>

impl<T: AsRef<[u8]>> Into<RwStreamSink<Chan<T>>> for Chan<T>

impl<T> Into<KeyBytes> for Key<T>

impl Into<Vec<u8, Global>> for ByteVec

impl<T: Scalar, const D: usize> Into<[T; D]> for SVector<T, D>

impl<T: Scalar, const D: usize> Into<[T; D]> for RowSVector<T, D> where
    Const<D>: IsNotStaticOne

impl<T: Scalar, const R: usize, const C: usize> Into<[[T; R]; C]> for SMatrix<T, R, C>

impl<'a, T: Scalar + Copy, R: Dim, C: Dim, S: ContiguousStorage<T, R, C>> Into<&'a [T]> for &'a Matrix<T, R, C, S>

impl<'a, T: Scalar + Copy, R: Dim, C: Dim, S: ContiguousStorageMut<T, R, C>> Into<&'a mut [T]> for &'a mut Matrix<T, R, C, S>

impl<T, R: Dim, C: Dim> Into<Vec<T, Global>> for VecStorage<T, R, C>

impl<T: Scalar, const D: usize> Into<[T; D]> for Point<T, D>

impl<T: Scalar, const D: usize> Into<[T; D]> for Translation<T, D>

impl<T> Into<(T, T)> for Ratio<T>

impl Into<u8> for OpCode

impl Into<u16> for CloseCode

impl<'a> Into<&'a [u32; 4]> for &'a vec128_storage

impl Into<u32> for InstIx

impl Into<u32> for BlockIx

impl<'a, D: DBAccess> Into<DBRawIteratorWithThreadMode<'a, D>> for DBIteratorWithThreadMode<'a, D>

impl<'a> Into<TrustAnchor<'a>> for &'a OwnedTrustAnchor

impl Into<Arc<dyn SyncCryptoStore + 'static>> for LocalKeystore

impl Into<Arc<dyn CryptoStore + 'static>> for LocalKeystore

impl Into<u8> for RecoveryId

impl Into<i32> for RecoveryId

impl<'a> Into<Vec<(Public, u64), Global>> for VersionedAuthorityList<'a>

impl Into<Arc<dyn SyncCryptoStore + 'static>> for KeyStore

impl Into<Arc<dyn CryptoStore + 'static>> for KeyStore

impl<'a> Into<&'a str> for UniCase<&'a str>

impl<'a> Into<String> for UniCase<String>

impl<'a> Into<Cow<'a, str>> for UniCase<Cow<'a, str>>

impl Into<u8> for Level

impl Into<Error> for Error

impl Into<Error> for ReadError

impl Into<String> for Error