Crate to_method[−][src]
Expand description
A utility micro-crate for using Into
and
TryInto
more ergonomically.
It exposes a To
extension trait with a .to()
method and .try_to()
method which you can use to invoke
Into::into
and
TryInto
while specifying the target
type and without having to abandon method-call syntax.
Being a micro-crate, it tries to be as nice of a dependency as possible and has:
- No dependencies of its own
- No feature flags
- No
build.rs
#![no_std]
#![forbid(unsafe_code)]
Regular Into
usage
let x : u8 = 5; // The type parameter is on `Into`, not on `Into::into`, // so we need to do it like this: let y = Into::<u16>::into(x); // Depending on context, inference can make this work though: let z : u32 = y.into();
With To
use to_method::To as _; let x : u8 = 5; // The type parameter is on the `to` method, so this works: let y = x.to::<u16>(); // And you can still rely on inference as well: let z : u32 = y.to();
TryInto
The same mechanism works for the TryInto
trait via the try_to
method:
use core::convert::TryInto; let x : u16 = 5; // The type parameter is on `Into`, not on `Into::into`, // so we need to do it like this: let y = TryInto::<u8>::try_into(x)?; // Depending on context, inference can make this work though: let z : u8 = y.try_into()?;
compared to:
use core::convert::TryInto; use to_method::To as _; let x : u16 = 5; // The type parameter is on the `to` method, so this works: let y = x.try_to::<u8>()?; // And you can still rely on inference as well: let z : u8 = y.try_to()?;
Traits
To |