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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*!
Types used to represent values at compile-time,eg:True/False.
*/

/**
Type-level booleans.

This is a re-export from `core_extensions::type_level_bool`,
so as to allow glob imports (`abi_stable::type_level::bools::*`)
without worrying about importing too many items.
*/
pub mod bools{
    #[doc(no_inline)]
    pub use core_extensions::type_level_bool::{True,False,Boolean};
}


// Uncomment if I have a use for type-level `Option`s
// pub mod option;

/// Type-level enum representing whether a 
/// `DynTrait`/`RObject`/`#[sabi_trait]`-generated trait object
/// can be converted back into the concrete type they were constructed with.
pub mod unerasability{
    use crate::{
        sabi_types::{Constructor,MaybeCmp},
        std_types::utypeid::{UTypeId,no_utypeid,some_utypeid},
    };


    /// Indicates that a type implements `Any`.
    ///
    /// A trait object wrapping that type can be unerased back into taht type.
    #[allow(non_camel_case_types)]
    #[derive(Copy,Clone)]
    pub struct TU_Unerasable;

    /// Indicates that a type does not implement `Any`,
    ///
    /// A trait object wrapping that type can't be unerased.
    #[allow(non_camel_case_types)]
    #[derive(Copy,Clone)]
    pub struct TU_Opaque;
    




    /// Gets a function optionally returning the `UTypeId` of `T`.
    ///
    /// Whether the function returns `MaybeCmp::Just(typeid)` is determined by implementors:
    /// 
    /// - `TU_Unerasable`: the function always returns `MaybeCmp::Just(typeid)`.
    /// 
    /// - `TU_Opaque`: the function always returns `MaybeCmp::Nothing`.
    pub trait GetUTID<T> {
        /// A struct wrapping the function.
        const UID:Constructor<MaybeCmp<UTypeId>>;
    }


    impl<T> GetUTID<T> for TU_Unerasable
    where T:'static
    {
        const UID:Constructor<MaybeCmp<UTypeId>>=Constructor( some_utypeid::<T> );
    }

    impl<T> GetUTID<T> for TU_Opaque{
        const UID:Constructor<MaybeCmp<UTypeId>>=Constructor( no_utypeid );
    }
}


/// Marker types representing traits.
pub mod trait_marker{
    pub struct Send;
    pub struct Sync;
    pub struct Clone;
    pub struct Default;
    pub struct Display;
    pub struct Debug;
    pub struct Eq;
    pub struct PartialEq;
    pub struct Ord;
    pub struct PartialOrd;
    pub struct Hash;

    /// Represents the `serde::Deserialize` trait.
    pub struct Deserialize;

    /// Represents the `serde::Serialize` trait.
    pub struct Serialize;
    
    pub struct Iterator;
    pub struct DoubleEndedIterator;
    
    /// Represents the `std::fmt::Write` trait.
    pub struct FmtWrite;
    
    /// Represents the `std::io::Write` trait.
    pub struct IoWrite;
    
    /// Represents the `std::io::Seek` trait.
    pub struct IoSeek;
    
    /// Represents the `std::io::Read` trait.
    pub struct IoRead;

    /// Represents the `std::io::BufRead` trait.
    pub struct IoBufRead;
    
    /// Represents the `std::error::Error` trait.
    pub struct Error;
    
    #[doc(hidden)]
    #[allow(non_camel_case_types)]
    pub struct define_this_in_the_impl_InterfaceType_macro;
}


/// Type-level-enum representing whether a trait is implemented or not implemented.
pub mod impl_enum{

    use crate::marker_type::NonOwningPhantom;

    use core_extensions::type_level_bool::{True,False};

    mod sealed{
        pub trait Sealed{}
    }
    use self::sealed::Sealed;

    /// Queries whether this type is `Implemented<T>`
    pub trait IsImplemented:Sealed{
        /// Whether the trait represented by the type parameter must be implemented.
        const VALUE:bool;
    }
    

    /// Converts a type to either `Unimplemented` or `Implemented`.
    ///
    /// The `T` type parameter represents the (un)required trait.
    ///
    pub trait ImplFrom_<T:?Sized>{
        /// Either `Unimplemented` or `Implemented`.
        type Impl:?Sized;
        
        /// Either `Unimplemented` or `Implemented`.
        const IMPL:Self::Impl;
    }

    impl<T:?Sized> ImplFrom_<T> for False {
        type Impl=Unimplemented<T>;
        const IMPL:Unimplemented<T>=Unimplemented::NEW;
    }

    impl<T:?Sized> ImplFrom_<T> for True {
        type Impl=Implemented<T>;
        const IMPL:Implemented<T>=Implemented::NEW;
    }

    impl<T:?Sized> ImplFrom_<T> for Unimplemented<T> {
        type Impl=Unimplemented<T>;
        const IMPL:Unimplemented<T>=Unimplemented::NEW;
    }

    impl<T:?Sized> ImplFrom_<T> for Implemented<T> {
        type Impl=Implemented<T>;
        const IMPL:Implemented<T>=Implemented::NEW;
    }

    /// Converts `B` to either `Unimplemented<T>` or `Implemented<T>`.
    ///
    /// The `T` type parameter represents the (un)required trait.
    pub type ImplFrom<B,T>=
        <B as ImplFrom_<T>>::Impl;



    /// Describes that a trait must be implemented.
    ///
    /// The `T` type parameter represents the required trait.
    pub struct Implemented<T:?Sized>(NonOwningPhantom<T>);

    impl<T:?Sized> Implemented<T>{
        /// Constructs an `Implemented`.
        pub const NEW:Implemented<T>=Implemented(NonOwningPhantom::NEW);
    }

    impl<T> Sealed for Implemented<T>{}
    impl<T> IsImplemented for Implemented<T>{
        const VALUE:bool=true;
    }



    /// Describes that a trait does not need to be implemented.
    ///
    /// The `T` type parameter represents the trait.
    pub struct Unimplemented<T:?Sized>(NonOwningPhantom<T>);

    impl<T:?Sized> Unimplemented<T>{
        /// Constructs an `Unimplemented`.
        pub const NEW:Unimplemented<T>=Unimplemented(NonOwningPhantom::NEW);
    }

    impl<T> Sealed for Unimplemented<T>{}
    impl<T> IsImplemented for Unimplemented<T>{
        const VALUE:bool=false;
    }
}