platform_version/
lib.rs

1use crate::version::PlatformVersion;
2
3pub mod error;
4pub mod version;
5
6pub trait DefaultForPlatformVersion: Sized {
7    /// The type returned in the event of a conversion error.
8    type Error;
9
10    /// Performs the conversion.
11    fn default_for_platform_version(
12        platform_version: &PlatformVersion,
13    ) -> Result<Self, Self::Error>;
14}
15
16pub trait TryFromPlatformVersioned<T>: Sized {
17    /// The type returned in the event of a conversion error.
18    type Error;
19
20    /// Performs the conversion.
21    fn try_from_platform_versioned(
22        value: T,
23        platform_version: &PlatformVersion,
24    ) -> Result<Self, Self::Error>;
25}
26
27pub trait TryIntoPlatformVersioned<T>: Sized {
28    /// The type returned in the event of a conversion error.
29    type Error;
30
31    /// Performs the conversion.
32    fn try_into_platform_versioned(
33        self,
34        platform_version: &PlatformVersion,
35    ) -> Result<T, Self::Error>;
36}
37
38// TryFrom implies TryInto
39impl<T, U> TryIntoPlatformVersioned<U> for T
40where
41    U: TryFromPlatformVersioned<T>,
42{
43    type Error = U::Error;
44
45    #[inline]
46    fn try_into_platform_versioned(
47        self,
48        platform_version: &PlatformVersion,
49    ) -> Result<U, U::Error> {
50        U::try_from_platform_versioned(self, platform_version)
51    }
52}
53
54/// A trait for converting a value of type `T` into `Self` using a provided `PlatformVersion`.
55///
56/// This trait is infallible and guarantees a valid result for supported versions.
57pub trait FromPlatformVersioned<T>: Sized {
58    /// Performs the conversion.
59    fn from_platform_versioned(value: T, platform_version: &PlatformVersion) -> Self;
60}
61
62/// A trait for converting `Self` into another type `T` using a `PlatformVersion`.
63///
64/// This is the infallible counterpart of `TryIntoPlatformVersioned`.
65pub trait IntoPlatformVersioned<T>: Sized {
66    /// Performs the conversion.
67    fn into_platform_versioned(self, platform_version: &PlatformVersion) -> T;
68}
69
70// FromPlatformVersioned implies IntoPlatformVersioned
71impl<T, U> IntoPlatformVersioned<U> for T
72where
73    U: FromPlatformVersioned<T>,
74{
75    #[inline]
76    fn into_platform_versioned(self, platform_version: &PlatformVersion) -> U {
77        U::from_platform_versioned(self, platform_version)
78    }
79}