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
use core::fmt;
use core::hash;
use core::mem;
use core::num::NonZeroU64;
use core::ptr;
use swim_mem::alloc::{Hold, HoldError, Stow, TryClone, CloneIntoHold};
use crate::item::{Item, Value};

/// `Value` variant representing a boolean.
///
/// # Examples
///
/// Convert a primitive `bool` into a structurally typed `Bool` value:
///
/// ```
/// # use swim_structure::item::Bool;
/// let value = Bool::from(true);
/// # assert_eq!(value.to_bool(), true);
/// ```
///
/// Extract a primitive `bool` out of a structurally typed `Bool` value:
///
/// ```
/// # use swim_structure::item::Bool;
/// let value = Bool::from(false);
/// let primitive = value.to_bool();
/// # assert_eq!(primitive, false);
/// ```
#[derive(Eq)]
#[repr(C)]
pub struct Bool {
    /// Discriminant with `TRUE_TYPE` or `FALSE_TYPE` at the lowest byte address.
    ///
    /// ```text
    /// 0        1        2        3        4        5        6        7        8
    /// +--------+--------+--------+--------+--------+--------+--------+--------+
    /// |  type  |      0 |      0 |      0 |      0 |      0 |      0 |      0 |
    /// +--------+--------+--------+--------+--------+--------+--------+--------+
    /// ```
    _0: NonZeroU64,
    /// Reserved.
    _1: u64,
}

impl Bool {
    /// Constructs a new `Bool` from a `bool` value.
    pub const fn from_bool(value: bool) -> Bool {
        Bool {
            _0: unsafe { NonZeroU64::new_unchecked(Value::discriminant(Value::FALSE_TYPE + value as u8)) },
            _1: value as u64,
        }
    }

    /// Returns a pointer to the tag in the first byte of this `Bool`.
    #[inline(always)]
    pub(crate) unsafe fn tag_ptr(&self) -> *mut u8 {
        mem::transmute::<&Bool, *mut u8>(self)
    }

    /// Returns the tag from the first byte of this `Bool`.
    #[inline(always)]
    pub(crate) fn tag(&self) -> u8 {
        unsafe { *self.tag_ptr() }
    }

    /// Returns the type tag from the low 7 bits of the first byte of this `Bool`.
    #[inline(always)]
    pub(crate) fn type_tag(&self) -> u8 {
        self.tag() & Value::TYPE_MASK
    }

    /// Returns the `bool` value of this `Bool` reference.
    pub fn to_bool(&self) -> bool {
        self.type_tag() != Value::FALSE_TYPE
    }

    /// Upcasts this `Bool` reference to a `Value` reference.
    #[inline]
    pub fn as_value<'a>(&self) -> &Value<'a> {
        unsafe { mem::transmute::<&Bool, &Value<'a>>(self) }
    }

    /// Upcasts this `Bool` reference to a mutable `Value` reference.
    #[inline]
    pub fn as_mut_value<'a>(&mut self) -> &mut Value<'a> {
        unsafe { mem::transmute::<&mut Bool, &mut Value<'a>>(self) }
    }

    /// Upcasts this `Bool` to a `Value`.
    #[inline]
    pub fn into_value<'a>(self) -> Value<'a> {
        unsafe { mem::transmute::<Bool, Value<'a>>(self) }
    }

    /// Upcasts this `Bool` to an `Item`.
    #[inline]
    pub fn into_item<'a>(self) -> Item<'a> {
        Item::from_value(self.into_value())
    }
}

impl<'a> AsRef<Value<'a>> for Bool {
    #[inline]
    fn as_ref(&self) -> &Value<'a> {
        self.as_value()
    }
}

impl<'a> AsMut<Value<'a>> for Bool {
    #[inline]
    fn as_mut(&mut self) -> &mut Value<'a> {
        self.as_mut_value()
    }
}

impl PartialEq for Bool {
    #[inline]
    fn eq(&self, that: &Bool) -> bool {
        self.to_bool() == that.to_bool()
    }
}

impl hash::Hash for Bool {
    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
        self.to_bool().hash(hasher)
    }
}

impl fmt::Debug for Bool {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(if self.to_bool() { "True" } else { "False" })
    }
}

impl Clone for Bool {
    fn clone(&self) -> Bool {
        Bool::from_bool(self.to_bool())
    }
}

impl TryClone for Bool {
    fn try_clone(&self) -> Result<Bool, HoldError> {
        Ok(Bool::from_bool(self.to_bool()))
    }
}

impl<'a> CloneIntoHold<'a, Bool> for Bool {
    fn try_clone_into_hold(&self, _hold: &Hold<'a>) -> Result<Bool, HoldError> {
        Ok(Bool::from_bool(self.to_bool()))
    }
}

impl<'b> Stow<'b, Bool> for Bool {
    unsafe fn stow(src: *mut Bool, dst: *mut Bool, _hold: &Hold<'b>) -> Result<(), HoldError> {
        ptr::write(&mut (*dst)._0, NonZeroU64::new_unchecked(Value::discriminant((*src).type_tag())));
        ptr::write(&mut (*dst)._1, (*src)._1);
        Ok(())
    }

    unsafe fn unstow(_src: *mut Bool, _dst: *mut Bool) {
        // nop
    }
}

impl Default for Bool {
    #[inline]
    fn default() -> Bool {
        Bool::from_bool(false)
    }
}

impl From<bool> for Bool {
    #[inline]
    fn from(value: bool) -> Bool {
        Bool::from_bool(value)
    }
}

impl From<Bool> for bool {
    #[inline]
    fn from(value: Bool) -> bool {
        value.to_bool()
    }
}