to_method/
lib.rs

1//! A utility micro-crate for using [`Into`](::core::convert::Into) and
2//! [`TryInto`](::core::convert::TryInto) more ergonomically.
3//!
4//! It exposes a [`To`](crate::To) extension trait with a [`.to()`](To::to)
5//! method and [`.try_to()`](To::try_to) method which you can use to invoke
6//! [`Into::into`](::core::convert::Into::into) and
7//! [`TryInto`](::core::convert::TryInto::try_into) while specifying the target
8//! type and without having to abandon method-call syntax.
9//!
10//! Being a micro-crate, it tries to be as nice of a dependency as possible and has:
11//!
12//! - No dependencies of its own
13//! - No feature flags
14//! - No `build.rs`
15//! - `#![no_std]`
16//! - `#![forbid(unsafe_code)]`
17//!
18//! # Regular `Into` usage
19//!
20//! ```
21//! let x : u8 = 5;
22//!
23//! // The type parameter is on `Into`, not on `Into::into`,
24//! // so we need to do it like this:
25//! let y = Into::<u16>::into(x);
26//!
27//! // Depending on context, inference can make this work though:
28//! let z : u32 = y.into();
29//! ```
30//!
31//! # With `To`
32//!
33//! ```
34//! use to_method::To as _;
35//!
36//! let x : u8 = 5;
37//!
38//! // The type parameter is on the `to` method, so this works:
39//! let y = x.to::<u16>();
40//!
41//! // And you can still rely on inference as well:
42//! let z : u32 = y.to();
43//! ```
44//!
45//! # `TryInto`
46//!
47//! The same mechanism works for the `TryInto` trait via the `try_to` method:
48//!
49//! ```
50//! use core::convert::TryInto;
51//!
52//! # fn main() -> Result<(), core::num::TryFromIntError> {
53//!
54//! let x : u16 = 5;
55//!
56//! // The type parameter is on `Into`, not on `Into::into`,
57//! // so we need to do it like this:
58//! let y = TryInto::<u8>::try_into(x)?;
59//!
60//! // Depending on context, inference can make this work though:
61//! let z : u8 = y.try_into()?;
62//!
63//! # Ok(())
64//! # }
65//! ```
66//!
67//! compared to:
68//!
69//! ```
70//! use core::convert::TryInto;
71//!
72//! # fn main() -> Result<(), core::num::TryFromIntError> {
73//!
74//! use to_method::To as _;
75//!
76//! let x : u16 = 5;
77//!
78//! // The type parameter is on the `to` method, so this works:
79//! let y = x.try_to::<u8>()?;
80//!
81//! // And you can still rely on inference as well:
82//! let z : u8 = y.try_to()?;
83//!
84//! # Ok(())
85//! # }
86//! ```
87
88#![no_std]
89#![forbid(missing_docs)]
90#![forbid(unsafe_code)]
91
92use core::convert::TryInto;
93
94/// Extension trait providing the [`to`](To::to) and [`try_to`](To::try_to) methods.
95pub trait To {
96    /// Converts to `T` by calling `Into<T>::into`.
97    #[inline(always)]
98    fn to<T>(self) -> T
99    where
100        Self: Into<T>,
101    {
102        <Self as Into<T>>::into(self)
103    }
104    
105    /// Tries to convert to `T` by calling `TryInto<T>::try_into`.
106    fn try_to<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
107    where
108        Self: TryInto<T>,
109    {
110        <Self as TryInto<T>>::try_into(self)
111    }
112}
113
114/// Blanket impl for all types.
115/// This makes sure that everything implements `To` and
116/// that no downstream impls can exist.
117impl<T: ?Sized> To for T {}