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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Type level booleans
//!
//! # Example
//!
//! Access of privileges based on renamed Boolean types.
//!
#![cfg_attr(feature = "phantom", doc = " ```rust")]
#![cfg_attr(not(feature = "phantom"), doc = " ```ignore")]
//! use std::mem;
//! use std::marker::PhantomData;
//!
//! use core_extensions::CovariantPhantom;
//!
//! pub use core_extensions::type_level_bool::{
//!     Boolean as PrivilegeLevel,
//!     False as Unprivileged,
//!     True as Privileged,
//! };
//!
//!
//! #[repr(C)]
//! #[derive(Debug)]
//! struct User<P>{
//!     name: String,
//!     privilege_level: CovariantPhantom<P>,
//! }
//!
//! impl<P: PrivilegeLevel> User<P>{
//!     fn new(name: String, privilege_level: P) -> Self {
//!         Self{name, privilege_level: PhantomData}
//!     }
//!     fn name(&self)->&str{
//!         &self.name
//!     }
//!     fn into_unprivileged(self) -> User<Unprivileged> {
//!         User{name: self.name, privilege_level: PhantomData}
//!     }
//!     fn as_unprivileged(self: &mut User<P>) -> &mut User<Unprivileged> {
//!         // Only the type parameter P changes here
//!         unsafe{ mem::transmute(self) }
//!     }
//! }
//!
//! impl User<Privileged>{
//!     fn set_name(&mut self, name: String){
//!         self.name = name;
//!     }
//! }
//!
//! let mut user: User<Privileged> = User::new("bob".into(), Privileged);
//! assert_eq!(user.name(), "bob");
//!
//! user.set_name("paul".into());
//! assert_eq!(user.name(), "paul");
//!
//! {
//!     let user: &mut User<Unprivileged> = user.as_unprivileged();
//!
//!     // Unprivileged Users can't change their name.
//!     // user.set_name("james".into());
//!
//!     assert_eq!(user.name(), "paul");
//! }
//!
//! user.set_name("john".into());
//! assert_eq!(user.name(), "john");
//!
//! ```
//!
//!

#[cfg(feature = "const_default")]
use crate::ConstDefault;

#[cfg(not(feature = "const_default"))]
use std_::marker::Sized as ConstDefault;



use std_::fmt::{self, Debug, Display};
use std_::ops;

#[cfg(feature = "marker_type")]
use crate::MarkerType;

#[cfg(not(feature = "marker_type"))]
use std_::marker::Sized as MarkerType;


/// Represents a type-level `true`
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct True;

/// Represents a type-level `false`
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct False;

impl Display for True {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("True")
    }
}

impl Display for False {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("False")
    }
}

mod sealed {
    use super::{False, True};
    pub trait Sealed {}
    impl Sealed for True {}
    impl Sealed for False {}
}
use self::sealed::Sealed;

#[cfg(feature = "marker_type")]
unsafe impl MarkerType for True {}

#[cfg(feature = "marker_type")]
unsafe impl MarkerType for False {}

const _: &[[(); 0]] = &[
    [(); std_::mem::size_of::<True>()],
    [(); std_::mem::size_of::<False>()],
    [(); std_::mem::align_of::<True>() - 1],
    [(); std_::mem::align_of::<False>() - 1],
];

#[cfg(feature = "const_default")]
impl ConstDefault for True {
    const DEFAULT: Self = True;
}

#[cfg(feature = "const_default")]
impl ConstDefault for False {
    const DEFAULT: Self = False;
}

/// Represents a type-level `bool`
///
/// Only implemented on [`True`] and [`False`].
///
/// For examples look at [the module-level documentation](./index.html).
///
/// This trait is sealed and cannot be implemented for types outside this crate.
///
/// [`True`]: ./struct.True.html
/// [`False`]: ./struct.False.html
pub trait Boolean:
    Sealed
    + MarkerType
    + ConstDefault
    + Default
    + Sized
    + Debug
    + Copy
    + Clone
    + ops::Not
    + ops::BitAnd<True, Output = Self>
    + ops::BitAnd<False, Output = False>
    + ops::BitAnd<Self, Output = Self>
    + ops::BitOr<True, Output = True>
    + ops::BitOr<False, Output = Self>
    + ops::BitOr<Self, Output = Self>
    + ops::BitXor<True, Output = <Self as ops::Not>::Output>
    + ops::BitXor<False, Output = Self>
    + ops::BitXor<Self, Output = False>
{
    /// The `bool` value of this type
    const VALUE: bool;
}

impl Boolean for True {
    const VALUE: bool = true;
}
impl Boolean for False {
    const VALUE: bool = false;
}

mod internals {
    #[cfg(feature = "const_default")]
    use crate::ConstDefault;

    use super::{Boolean, False, True, Not};

    use std_::ops;

    impl ops::Not for True {
        type Output = False;
        fn not(self) -> Self::Output {
            False
        }
    }
    impl ops::Not for False {
        type Output = True;
        fn not(self) -> Self::Output {
            True
        }
    }

    impl<B> ops::BitAnd<B> for False {
        type Output = False;
        fn bitand(self, _: B) -> Self::Output {
            False
        }
    }
    impl<B> ops::BitAnd<B> for True {
        type Output = B;
        fn bitand(self, v: B) -> Self::Output {
            v
        }
    }

    impl<B> ops::BitOr<B> for True {
        type Output = True;
        fn bitor(self, _: B) -> Self::Output {
            True
        }
    }
    impl<B> ops::BitOr<B> for False {
        type Output = B;
        fn bitor(self, v: B) -> Self::Output {
            v
        }
    }

    impl<B: Boolean> ops::BitXor<B> for True 
    where
        Not<B>: Boolean
    {
        type Output = Not<B>;

        fn bitxor(self, _: B) -> Self::Output {
            #[cfg(feature = "const_default")]
            {
                Not::<B>::DEFAULT
            }
            #[cfg(not(feature = "const_default"))]
            {
                Not::<B>::default()
            }
        }
    }
    impl<B> ops::BitXor<B> for False {
        type Output = B;
        fn bitxor(self, v: B) -> Self::Output {
            v
        }
    }
}

/// Negates a [Boolean](./trait.Boolean.html).
///
///     # use core_extensions::type_level_bool::*;
///     assert_eq!(Not::<True >::VALUE, false);
///     assert_eq!(Not::<False>::VALUE, true);
///
pub type Not<T> = <T as ops::Not>::Output;

/// `And`s two [Boolean](./trait.Boolean.html)s
///
///
///     # use core_extensions::type_level_bool::*;
///     assert_eq!(And::<True , True >::VALUE, true);
///     assert_eq!(And::<False, True >::VALUE, false);
///     assert_eq!(And::<True , False>::VALUE, false);
///     assert_eq!(And::<False, False>::VALUE, false);
///
pub type And<L, R> = <L as ops::BitAnd<R>>::Output;

/// `Or`s two [Boolean](./trait.Boolean.html)s
///
///
///     # use core_extensions::type_level_bool::*;
///     assert_eq!(Or::<True , True >::VALUE, true);
///     assert_eq!(Or::<False, True >::VALUE, true);
///     assert_eq!(Or::<True , False>::VALUE, true);
///     assert_eq!(Or::<False, False>::VALUE, false);
///
pub type Or<L, R> = <L as ops::BitOr<R>>::Output;

/// `Xor`s two [Boolean](./trait.Boolean.html)s
///
///
///     # use core_extensions::type_level_bool::*;
///     assert_eq!(Xor::<True , True >::VALUE, false);
///     assert_eq!(Xor::<False, True >::VALUE, true);
///     assert_eq!(Xor::<True , False>::VALUE, true);
///     assert_eq!(Xor::<False, False>::VALUE, false);
///
pub type Xor<L, R> = <L as ops::BitXor<R>>::Output;