gf256/
traits.rs

1//! Common traits
2//!
3//! Currently just a workaround for FromLossy/IntoLossy traits
4//!
5
6// TryFrom/TryInto forwarded for convenience
7pub use core::convert::TryFrom;
8pub use core::convert::TryInto;
9
10/// A From trait for conversions which may lose precision
11///
12/// Note this is just a temporary solution. Once [RFC2484] is implemented
13/// these traits should go away.
14///
15/// [RFC2484]: https://github.com/rust-lang/rfcs/pull/2484
16///
17pub trait FromLossy<T> {
18    /// Convert to this type lossily
19    fn from_lossy(t: T) -> Self;
20}
21
22/// An Into trait for conversions which may lose precision
23///
24/// This is similar to Into, but for FromLossy
25///
26pub trait IntoLossy<T> {
27    /// Convert this type lossily
28    fn into_lossy(self) -> T;
29}
30
31// IntoLossy is the inverse of FromLossy
32impl<T, U> IntoLossy<T> for U
33where
34    T: FromLossy<U>
35{
36    #[inline]
37    fn into_lossy(self) -> T {
38        T::from_lossy(self)
39    }
40}
41
42// All types that provide From provide FromLossy
43impl<T, U> FromLossy<T> for U
44where
45    U: From<T>
46{
47    #[inline]
48    fn from_lossy(t: T) -> Self {
49        Self::from(t)
50    }
51}
52