abi_stable/
type_level.rs

1//! Types used to represent values at compile-time, eg: True/False.
2
3/// Type-level booleans.
4///
5/// This is a re-export from `core_extensions::type_level_bool`,
6/// so as to allow glob imports (`abi_stable::type_level::bools::*`)
7/// without worrying about importing too many items.
8pub mod bools {
9    #[doc(no_inline)]
10    pub use core_extensions::type_level_bool::{Boolean, False, True};
11}
12
13// Uncomment if I have a use for type-level `Option`s
14// pub mod option;
15
16/// Type-level enum representing whether a
17/// `DynTrait`/`RObject`/`#[sabi_trait]`-generated trait object
18/// can be converted back into the concrete type they were constructed with.
19pub mod downcasting;
20
21/// Marker types representing traits.
22pub mod trait_marker {
23    ///
24    pub struct Send;
25
26    ///
27    pub struct Sync;
28
29    ///
30    pub struct Clone;
31
32    ///
33    pub struct Default;
34
35    ///
36    pub struct Display;
37
38    ///
39    pub struct Debug;
40
41    ///
42    pub struct Eq;
43
44    ///
45    pub struct PartialEq;
46
47    ///
48    pub struct Ord;
49
50    ///
51    pub struct PartialOrd;
52
53    ///
54    pub struct Hash;
55
56    /// Represents the [`serde::Deserialize`] trait.
57    pub struct Deserialize;
58
59    /// Represents the [`serde::Serialize`] trait.
60    pub struct Serialize;
61
62    ///
63    pub struct Iterator;
64
65    ///
66    pub struct DoubleEndedIterator;
67
68    /// Represents the [`std::fmt::Write`] trait.
69    pub struct FmtWrite;
70
71    /// Represents the [`std::io::Write`] trait.
72    pub struct IoWrite;
73
74    /// Represents the [`std::io::Seek`] trait.
75    pub struct IoSeek;
76
77    /// Represents the [`std::io::Read`] trait.
78    pub struct IoRead;
79
80    /// Represents the [`std::io::BufRead`] trait.
81    pub struct IoBufRead;
82
83    /// Represents the [`std::error::Error`] trait.
84    pub struct Error;
85
86    /// Represents the [`std::marker::Unpin`] trait.
87    pub struct Unpin;
88
89    #[doc(hidden)]
90    #[allow(non_camel_case_types)]
91    pub struct define_this_in_the_impl_InterfaceType_macro;
92}
93
94/// Type-level-enum representing whether a trait is implemented or not implemented.
95pub mod impl_enum {
96
97    use crate::marker_type::NonOwningPhantom;
98
99    use core_extensions::type_level_bool::{False, True};
100
101    mod sealed {
102        pub trait Sealed {}
103    }
104    use self::sealed::Sealed;
105
106    /// Trait for [`Implemented`] and [`Unimplemented`]
107    pub trait Implementability: Sealed {
108        /// Whether the trait represented by the type parameter must be implemented.
109        const IS_IMPLD: bool;
110    }
111
112    /// Converts a type to either `Unimplemented` or `Implemented`.
113    ///
114    /// The `T` type parameter represents the (un)required trait.
115    ///
116    pub trait ImplFrom_<T: ?Sized> {
117        /// Either `Unimplemented` or `Implemented`.
118        type Impl: ?Sized + Implementability;
119    }
120
121    impl<T: ?Sized> ImplFrom_<T> for False {
122        type Impl = Unimplemented<T>;
123    }
124
125    impl<T: ?Sized> ImplFrom_<T> for True {
126        type Impl = Implemented<T>;
127    }
128
129    impl<T: ?Sized> ImplFrom_<T> for Unimplemented<T> {
130        type Impl = Unimplemented<T>;
131    }
132
133    impl<T: ?Sized> ImplFrom_<T> for Implemented<T> {
134        type Impl = Implemented<T>;
135    }
136
137    /// Converts `B` to either `Unimplemented<T>` or `Implemented<T>`.
138    ///
139    /// The `T` type parameter represents the (un)required trait.
140    pub type ImplFrom<B, T> = <B as ImplFrom_<T>>::Impl;
141
142    /// Describes that a trait must be implemented.
143    ///
144    /// The `T` type parameter represents the required trait.
145    pub struct Implemented<T: ?Sized>(NonOwningPhantom<T>);
146
147    impl<T: ?Sized> Sealed for Implemented<T> {}
148    impl<T: ?Sized> Implementability for Implemented<T> {
149        const IS_IMPLD: bool = true;
150    }
151
152    /// Describes that a trait does not need to be implemented.
153    ///
154    /// The `T` type parameter represents the trait.
155    pub struct Unimplemented<T: ?Sized>(NonOwningPhantom<T>);
156
157    impl<T: ?Sized> Sealed for Unimplemented<T> {}
158    impl<T: ?Sized> Implementability for Unimplemented<T> {
159        const IS_IMPLD: bool = false;
160    }
161}