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
/*! # Method-Directed Type Conversion

The `std::convert` module provides traits for converting values from one type to
another. The first of these, [`From<T>`], provides an associated function
[`from(orig: T) -> Self`]. This function can only be called in prefix-position,
as it does not have a `self` receiver. The second, [`Into<T>`], provides a
method [`into(self) -> T`] which *can* be called in suffix-position; due to
intractable problems in the type solver, this method cannot have any *further*
method calls attached to it. It must be bound directly into a `let` or function
call.

The [`TryFrom<T>`] and [`TryInto<T>`] traits have the same properties, but
permit failure.

This module provides traits that place the conversion type parameter in the
method, rather than in the trait, so that users can write `.conv::<T>()` to
convert the preceding expression into `T`, without causing any failures in the
type solver. These traits are blanket-implemented on all types that have an
`Into<T>` implementation, which covers both the blanket implementation of `Into`
for types with `From`, and manual implementations of `Into`.

[`From<T>`]: https://doc.rust-lang.org/std/convert/trait.From.html
[`Into<T>`]: https://doc.rust-lang.org/std/convert/trait.Into.html
[`TryFrom<T>`]: https://doc.rust-lang.org/std/convert/trait.TryFrom.html
[`TryInto<T>`]: https://doc.rust-lang.org/std/convert/trait.TryInto.html
[`from(orig: T) -> Self`]: https://doc.rust-lang.org/std/convert/trait.From.html#tymethod.from
[`into(self) -> T`]: https://doc.rust-lang.org/std/convert/trait.Into.html#tymethod.into
!*/

use core::convert::TryInto;

/// Wraps `Into::<T>::into` as a method that can be placed in pipelines.
pub trait Conv
where
	Self: Sized,
{
	/// Converts `self` into `T` using `Into<T>`.
	///
	/// # Examples
	///
	/// ```rust
	/// use tap::conv::Conv;
	///
	/// let len = "Saluton, mondo!"
	///   .conv::<String>()
	///   .len();
	/// ```
	#[inline(always)]
	fn conv<T>(self) -> T
	where
		Self: Into<T>,
		T: Sized,
	{
		Into::<T>::into(self)
	}
}

impl<T> Conv for T {}

/// Wraps `TryInto::<T>::try_into` as a method that can be placed in pipelines.
pub trait TryConv
where
	Self: Sized,
{
	/// Attempts to convert `self` into `T` using `TryInto<T>`.
	///
	/// # Examples
	///
	/// ```rust
	/// use tap::conv::TryConv;
	///
	/// let len = "Saluton, mondo!"
	///   .try_conv::<String>()
	///   .unwrap()
	///   .len();
	/// ```
	#[inline(always)]
	fn try_conv<T>(self) -> Result<T, Self::Error>
	where
		Self: TryInto<T>,
		T: Sized,
	{
		TryInto::<T>::try_into(self)
	}
}

impl<T> TryConv for T {}