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
/// /// The same as [`TryInto`] from `std/core`, except that it allows generic implementations. /// Details are in this [issue](https://github.com/rust-lang/rust/issues/50133) /// /// [Official documentation](https://doc.rust-lang.org/std/convert/trait.TryInto.html) /// /// [`TryFrom`]: trait.TryFrom.html /// [`TryInto`]: https://doc.rust-lang.org/std/convert/trait.TryInto.html /// [`Into`]: https://doc.rust-lang.org/std/convert/trait.Into.html pub trait TryInto<T>: Sized { /// The type returned in the event of a conversion error. type Error; /// Performs the conversion. fn try_into(self) -> Result<T, Self::Error>; } /// The same as [`TryFrom`] from `std/core`, except that it allows generic implementations. /// Details are in this [issue](https://github.com/rust-lang/rust/issues/50133) /// /// [Official documentation](https://doc.rust-lang.org/std/convert/trait.TryFrom.html) /// /// Usually you don't need to implement is yourself. /// This trait here is for safe conversion into [`Points`] /// /// # Example /// ``` /// use cubic_spline::{TryFrom, Points, Error}; /// /// let my_points: Vec<(f64,f64)> = vec![(1.0, 1.0)]; /// let prepared_points = Points::try_from(&my_points); /// /// assert_eq!(prepared_points.unwrap_err(), Error::TooFewPoints); /// /// let another_try = Points::try_from( &[ [3.0, 5.1], [10.3, 11.9] ] ); /// assert!(another_try.is_ok()); /// /// ``` /// /// [`try_from`]: trait.TryFrom.html#tymethod.try_from /// [`TryFrom`]: https://doc.rust-lang.org/std/convert/trait.TryFrom.html /// [`Points`]: struct.Points.html /// /// pub trait TryFrom<T>: Sized { /// The type returned in the event of a conversion error. type Error; /// Performs the conversion. fn try_from(value: T) -> Result<Self, Self::Error>; } //////////////////////////////////////////////////////////////////////////////// // GENERIC IMPLS //////////////////////////////////////////////////////////////////////////////// // TryFrom implies TryInto impl<T, U> TryInto<U> for T where U: TryFrom<T>, { type Error = U::Error; fn try_into(self) -> Result<U, U::Error> { U::try_from(self) } } // // This don't allow to implement a generic TryFrom // Infallible conversions are semantically equivalent to fallible conversions // with an uninhabited error type. // impl<T, U> TryFrom<U> for T // where // U: Into<T>, // { // type Error = Infallible; // // fn try_from(value: U) -> Result<Self, Self::Error> { // Ok(U::into(value)) // } // }