1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! A utility micro-crate for using [`Into`](::core::convert::Into) and
//! [`TryInto`](::core::convert::TryInto) more ergonomically.
//!
//! It exposes a [`To`](crate::To) extension trait with a [`.to()`](To::to)
//! method and [`.try_to()`](To::try_to) method which you can use to invoke
//! [`Into::into`](::core::convert::Into::into) and
//! [`TryInto`](::core::convert::TryInto::try_into) 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;
//!
//! # fn main() -> Result<(), core::num::TryFromIntError> {
//!
//! 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()?;
//!
//! # Ok(())
//! # }
//! ```
//!
//! compared to:
//!
//! ```
//! use core::convert::TryInto;
//!
//! # fn main() -> Result<(), core::num::TryFromIntError> {
//!
//! 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()?;
//!
//! # Ok(())
//! # }
//! ```

#![no_std]
#![forbid(missing_docs)]
#![forbid(unsafe_code)]

use core::convert::TryInto;

/// Extension trait providing the [`to`](To::to) and [`try_to`](To::try_to) methods.
pub trait To {
    /// Converts to `T` by calling `Into<T>::into`.
    #[inline(always)]
    fn to<T>(self) -> T
    where
        Self: Into<T>,
    {
        <Self as Into<T>>::into(self)
    }
    
    /// Tries to convert to `T` by calling `TryInto<T>::try_into`.
    fn try_to<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
    where
        Self: TryInto<T>,
    {
        <Self as TryInto<T>>::try_into(self)
    }
}

/// Blanket impl for all types.
/// This makes sure that everything implements `To` and
/// that no downstream impls can exist.
impl<T: ?Sized> To for T {}