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
use crate::Loc;

#[cfg(not(feature = "c-structures"))]
pub(crate) mod rust {
    use super::Loc;

    /// Rust-compatible nullable pointer
    pub type MaybeLocPtr = Option<Loc>;

    use super::MaybeLocPtrSome;
    impl MaybeLocPtrSome for MaybeLocPtr {
        fn some(loc: Loc) -> Self {
            Some(loc)
        }
    }

    use super::MaybeLocPtrNone;
    impl MaybeLocPtrNone for MaybeLocPtr {
        fn none() -> Self {
            None
        }
    }

    use super::AsLocOption;
    impl AsLocOption for MaybeLocPtr {
        fn as_option(&self) -> Option<&Loc> {
            self.as_ref()
        }
    }

    use super::IntoLocOption;
    impl IntoLocOption for MaybeLocPtr {
        fn into_option(self) -> Option<Loc> {
            self
        }
    }
}

#[cfg(feature = "c-structures")]
pub(crate) mod c {
    use super::Loc;
    use crate::containers::LocPtr;

    /// C-compatible nullable pointer
    #[repr(C)]
    pub struct MaybeLocPtr {
        ptr: *mut Loc,
    }

    impl Drop for MaybeLocPtr {
        fn drop(&mut self) {
            if self.ptr.is_null() {
                return;
            }

            drop(unsafe { Box::from_raw(self.ptr) });
            self.ptr = std::ptr::null_mut();
        }
    }

    impl std::fmt::Debug for MaybeLocPtr {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            std::fmt::Debug::fmt(&self.as_option(), f)
        }
    }

    impl PartialEq for MaybeLocPtr {
        fn eq(&self, other: &Self) -> bool {
            PartialEq::eq(&self.as_option(), &other.as_option())
        }
    }

    impl Clone for MaybeLocPtr {
        fn clone(&self) -> Self {
            match self.as_option() {
                Some(loc) => Self::some(loc.clone()),
                None => Self::none(),
            }
        }
    }

    use super::MaybeLocPtrSome;
    impl MaybeLocPtrSome for MaybeLocPtr {
        fn some(loc: Loc) -> Self {
            let ptr = Box::into_raw(Box::new(loc));
            Self { ptr }
        }
    }

    use super::MaybeLocPtrNone;
    impl MaybeLocPtrNone for MaybeLocPtr {
        fn none() -> Self {
            Self {
                ptr: std::ptr::null_mut(),
            }
        }
    }

    impl MaybeLocPtr {
        /// Constructs a pointer with a given raw pointer
        pub fn from_raw(ptr: *mut Loc) -> Self {
            Self { ptr }
        }

        /// Unwraps into raw pointer, consumes self
        pub fn into_raw(mut self) -> *mut Loc {
            let ptr = self.ptr;
            self.ptr = std::ptr::null_mut();
            ptr
        }

        /// Equivalent of Option::or_else
        pub fn or_else<F>(self, f: F) -> Self
        where
            F: FnOnce() -> Self,
        {
            if self.ptr.is_null() {
                f()
            } else {
                self
            }
        }

        /// Equivalent of Option::unwrap
        pub fn unwrap(self) -> LocPtr {
            let ptr = self.into_raw();
            if ptr.is_null() {
                panic!("failed to unwrap null MaybeLocPtr")
            } else {
                LocPtr::from_raw(ptr)
            }
        }

        /// Equivalent of Option::unwrap_or_else
        pub fn unwrap_or_else<F>(self, f: F) -> LocPtr
        where
            F: FnOnce() -> LocPtr,
        {
            let ptr = self.into_raw();
            if ptr.is_null() {
                f()
            } else {
                LocPtr::from_raw(ptr)
            }
        }

        /// Equivalent of Option::expect
        pub fn expect(self, message: &str) -> LocPtr {
            let ptr = self.into_raw();
            if ptr.is_null() {
                panic!("{}", message)
            } else {
                LocPtr::from_raw(ptr)
            }
        }

        /// Equivalent of Option::map
        pub fn map<F>(self, f: F) -> Self
        where
            F: FnOnce(LocPtr) -> LocPtr,
        {
            if self.ptr.is_null() {
                self
            } else {
                let ptr = self.into_raw();
                let ptr = LocPtr::from_raw(ptr);
                let ptr = f(ptr);
                let ptr = ptr.into_raw();
                Self::from_raw(ptr)
            }
        }

        /// Equivalent of Option::is_none
        pub fn is_none(&self) -> bool {
            self.ptr.is_null()
        }
    }

    use super::AsLocOption;
    impl AsLocOption for MaybeLocPtr {
        fn as_option(&self) -> Option<&Loc> {
            unsafe { self.ptr.as_ref() }
        }
    }

    use super::IntoLocOption;
    impl IntoLocOption for MaybeLocPtr {
        fn into_option(self) -> Option<Loc> {
            if self.ptr.is_null() {
                None
            } else {
                use crate::containers::loc_ptr::UnPtr;
                Some(self.unwrap().unptr())
            }
        }
    }
}

pub(crate) trait MaybeLocPtrSome {
    fn some(value: Loc) -> Self
    where
        Self: Sized;
}

pub(crate) trait MaybeLocPtrNone {
    fn none() -> Self
    where
        Self: Sized;
}

/// Trait for converting &MaybeLocPtr into Option<&Loc>
pub trait AsLocOption {
    /// Converts &MaybeLocPtr into Option<&Loc>
    fn as_option(&self) -> Option<&Loc>;
}

pub(crate) trait IntoLocOption {
    fn into_option(self) -> Option<Loc>
    where
        Self: Sized;
}