Trait sp_std::convert::TryFrom

1.34.0 · source · []
pub trait TryFrom<T> {
    type Error;
    fn try_from(value: T) -> Result<Self, Self::Error>;
}
Expand description

Simple and safe type conversions that may fail in a controlled way under some circumstances. It is the reciprocal of TryInto.

This is useful when you are doing a type conversion that may trivially succeed but may also need special handling. For example, there is no way to convert an i64 into an i32 using the From trait, because an i64 may contain a value that an i32 cannot represent and so the conversion would lose data. This might be handled by truncating the i64 to an i32 (essentially giving the i64’s value modulo i32::MAX) or by simply returning i32::MAX, or by some other method. The From trait is intended for perfect conversions, so the TryFrom trait informs the programmer when a type conversion could go bad and lets them decide how to handle it.

Generic Implementations

  • TryFrom<T> for U implies TryInto<U> for T
  • try_from is reflexive, which means that TryFrom<T> for T is implemented and cannot fail – the associated Error type for calling T::try_from() on a value of type T is Infallible. When the ! type is stabilized Infallible and ! will be equivalent.

TryFrom<T> can be implemented as follows:

struct GreaterThanZero(i32);

impl TryFrom<i32> for GreaterThanZero {
    type Error = &'static str;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        if value <= 0 {
            Err("GreaterThanZero only accepts value superior than zero!")
        } else {
            Ok(GreaterThanZero(value))
        }
    }
}

Examples

As described, i32 implements TryFrom<i64>:

let big_number = 1_000_000_000_000i64;
// Silently truncates `big_number`, requires detecting
// and handling the truncation after the fact.
let smaller_number = big_number as i32;
assert_eq!(smaller_number, -727379968);

// Returns an error because `big_number` is too big to
// fit in an `i32`.
let try_smaller_number = i32::try_from(big_number);
assert!(try_smaller_number.is_err());

// Returns `Ok(3)`.
let try_successful_smaller_number = i32::try_from(3);
assert!(try_successful_smaller_number.is_ok());

Associated Types

The type returned in the event of a conversion error.

Required methods

Performs the conversion.

Implementations on Foreign Types

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Map char with code point in U+0000..=U+00FF to byte in 0x00..=0xFF with same value, failing if the code point is greater than U+00FF.

See impl From<u8> for char for details on the encoding.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Try to create the target number type from a source number type. This returns an error if the source value is outside of the range of the target type.

Gets the entire contents of the Vec<T> as an array, if its size exactly matches that of the requested array.

Examples
assert_eq!(vec![1, 2, 3].try_into(), Ok([1, 2, 3]));
assert_eq!(<Vec<i32>>::new().try_into(), Ok([]));

If the length doesn’t match, the input comes back in Err:

let r: Result<[i32; 4], _> = (0..10).collect::<Vec<_>>().try_into();
assert_eq!(r, Err(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));

If you’re fine with just getting a prefix of the Vec<T>, you can call .truncate(N) first.

let mut v = String::from("hello world").into_bytes();
v.sort();
v.truncate(2);
let [a, b]: [_; 2] = v.try_into().unwrap();
assert_eq!(a, b' ');
assert_eq!(b, b'd');

Implementors

impl<T, const CAP: usize> TryFrom<&'_ [T]> for ArrayVec<T, CAP> where
    T: Clone

impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>

impl<'a, const CAP: usize> TryFrom<Arguments<'a>> for ArrayString<CAP>

impl<'a, O, V> TryFrom<&'a BitSlice<O, <V as BitView>::Store>> for BitArray<O, V> where
    O: BitOrder,
    V: BitView

impl<'a, O, V> TryFrom<&'a BitSlice<O, <V as BitView>::Store>> for &'a BitArray<O, V> where
    O: BitOrder,
    V: BitView

impl<'a, O, V> TryFrom<&'a mut BitSlice<O, <V as BitView>::Store>> for &'a mut BitArray<O, V> where
    O: BitOrder,
    V: BitView

impl<R> TryFrom<u8> for BitIdx<R> where
    R: BitRegister

impl<T> TryFrom<*const T> for Address<Const, T> where
    T: BitStore

impl<T> TryFrom<*mut T> for Address<Mut, T> where
    T: BitStore

impl<O, T> TryFrom<*const T> for BitPtr<Const, O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> TryFrom<*mut T> for BitPtr<Mut, O, T> where
    O: BitOrder,
    T: BitStore

impl<'a, O, T> TryFrom<&'a [T]> for &'a BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<'a, O, T> TryFrom<&'a mut [T]> for &'a mut BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

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

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

impl<const S: usize> TryFrom<String> for Cid<S>

impl<const S: usize> TryFrom<&'_ str> for Cid<S>

impl<const S: usize> TryFrom<Vec<u8, Global>> for Cid<S>

impl<const S: usize> TryFrom<&'_ [u8]> for Cid<S>

impl TryFrom<u64> for Version

impl TryFrom<u16> for Opcode

impl<'a> TryFrom<&'a [u8]> for Signature

impl<'a, K, V, T> TryFrom<&'a HashMap<K, V, RandomState>> for HeaderMap<T> where
    K: Eq + Hash,
    HeaderName: TryFrom<&'a K>,
    <HeaderName as TryFrom<&'a K>>::Error: Into<Error>,
    T: TryFrom<&'a V>,
    T::Error: Into<Error>, 

impl<'a> TryFrom<&'a str> for HeaderName

impl<'a> TryFrom<&'a String> for HeaderName

impl<'a> TryFrom<&'a str> for HeaderValue

impl<'a> TryFrom<&'a String> for HeaderValue

impl<'a> TryFrom<&'a [u8]> for Method

impl<'a> TryFrom<&'a str> for Method

impl<'a> TryFrom<&'a str> for StatusCode

impl<'a> TryFrom<&'a [u8]> for Authority

impl<'a> TryFrom<&'a str> for Authority

impl<'a> TryFrom<&'a str> for PathAndQuery

impl<'a> TryFrom<&'a [u8]> for Scheme

impl<'a> TryFrom<&'a str> for Scheme

impl<'a> TryFrom<&'a [u8]> for Uri

impl<'a> TryFrom<&'a str> for Uri

impl<'a> TryFrom<&'a String> for Uri

impl TryFrom<String> for Uri

impl TryFrom<Parts> for Uri

impl<'a> TryFrom<&'a Uri> for Uri

impl TryFrom<Vec<u8, Global>> for PeerId

impl<'a> TryFrom<&'a str> for Multiaddr

impl TryFrom<U128> for u8

impl TryFrom<U128> for u16

impl TryFrom<U128> for u32

impl TryFrom<U128> for usize

impl TryFrom<U128> for u64

impl TryFrom<U128> for i8

impl TryFrom<U128> for i16

impl TryFrom<U128> for i32

impl TryFrom<U128> for isize

impl TryFrom<U128> for i64

impl TryFrom<U128> for u128

impl TryFrom<U128> for i128

impl TryFrom<U256> for u8

impl TryFrom<U256> for u16

impl TryFrom<U256> for u32

impl TryFrom<U256> for usize

impl TryFrom<U256> for u64

impl TryFrom<U256> for i8

impl TryFrom<U256> for i16

impl TryFrom<U256> for i32

impl TryFrom<U256> for isize

impl TryFrom<U256> for i64

impl TryFrom<U256> for u128

impl TryFrom<U256> for i128

impl TryFrom<U512> for u8

impl TryFrom<U512> for u16

impl TryFrom<U512> for u32

impl TryFrom<U512> for usize

impl TryFrom<U512> for u64

impl TryFrom<U512> for i8

impl TryFrom<U512> for i16

impl TryFrom<U512> for i32

impl TryFrom<U512> for isize

impl TryFrom<U512> for i64

impl TryFrom<U512> for u128

impl TryFrom<U512> for i128

impl TryFrom<U256> for U128

impl TryFrom<U512> for U256

impl TryFrom<U512> for U128

impl<'a> TryFrom<&'a U512> for U256

impl TryFrom<u8> for OpCode

impl<'a> TryFrom<&'a [u8]> for ByteSlice125<'a>

impl<'a> TryFrom<&'a [u8]> for Public

impl<'a> TryFrom<&'a [u8]> for Public

impl<'a> TryFrom<&'a [u8]> for Public

impl TryFrom<BigUint> for u128

impl TryFrom<BigUint> for u64

impl<'a> TryFrom<&'a str> for Ss58AddressFormat

impl<'a> TryFrom<&'a [u8]> for Dummy

impl<'a> TryFrom<&'a str> for KeyTypeId

impl TryFrom<u8> for HttpError

impl TryFrom<u8> for LogLevel

impl TryFrom<u8> for ValueType

impl<T, A> TryFrom<&'_ [T]> for ArrayVec<A> where
    T: Clone + Default,
    A: Array<Item = T>, 

impl<'a, HO> TryFrom<NodeHandle<'a>> for ChildReference<HO> where
    HO: AsRef<[u8]> + AsMut<[u8]> + Default + Clone + Copy

impl<'a> TryFrom<&'a str> for Url