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

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

    /// Rust-compatible not-null Loc pointer (technically not a pointer, but it mimics it)
    pub type LocPtr = Loc;

    use super::IntoMaybeLocPtr;
    impl IntoMaybeLocPtr for LocPtr {
        fn into_maybe_ptr(self) -> crate::containers::MaybeLocPtr {
            Some(self)
        }
    }

    use super::UnPtr;
    impl UnPtr for LocPtr {
        fn unptr(self) -> Loc {
            self
        }
    }

    use super::LocPtrNew;
    impl LocPtrNew for LocPtr {
        fn new_ptr(loc: Loc) -> Self {
            loc
        }
    }
}

#[cfg(feature = "c-structures")]
pub(crate) mod c {
    use super::{Loc, MaybeLocPtr};
    use std::ops::Deref;

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

    impl Drop for LocPtr {
        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 LocPtr {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            std::fmt::Debug::fmt(&**self, f)
        }
    }

    impl PartialEq for LocPtr {
        fn eq(&self, other: &Self) -> bool {
            PartialEq::eq(&**self, &**other)
        }
    }

    impl Clone for LocPtr {
        fn clone(&self) -> Self {
            let value = self.as_ref().clone();
            Self::new(value)
        }
    }

    impl Deref for LocPtr {
        type Target = Loc;

        fn deref(&self) -> &Self::Target {
            unsafe { &*self.ptr }
        }
    }

    impl AsRef<Loc> for LocPtr {
        fn as_ref(&self) -> &Loc {
            unsafe { &*self.ptr }
        }
    }

    impl LocPtr {
        /// Constructs a LocPtr from Loc
        pub fn new(loc: Loc) -> Self {
            let ptr = Box::into_raw(Box::new(loc));
            Self { ptr }
        }

        /// Constructs LocPtr from a raw pointer
        pub fn from_raw(ptr: *mut Loc) -> Self {
            Self { ptr }
        }

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

    use super::IntoMaybeLocPtr;
    impl IntoMaybeLocPtr for LocPtr {
        fn into_maybe_ptr(self) -> MaybeLocPtr {
            use crate::containers::maybe_loc_ptr::MaybeLocPtrSome;
            MaybeLocPtr::some(self.unptr())
        }
    }

    use super::UnPtr;
    impl UnPtr for LocPtr {
        fn unptr(self) -> Loc {
            *unsafe { Box::from_raw(self.into_raw()) }
        }
    }

    use super::LocPtrNew;
    impl LocPtrNew for LocPtr {
        fn new_ptr(loc: Loc) -> Self {
            Self::new(loc)
        }
    }
}

/// Constructs a LocPtr from Loc
pub trait LocPtrNew {
    /// Constructs a LocPtr from Loc
    fn new_ptr(loc: Loc) -> Self
    where
        Self: Sized;
}

/// Unwraps the pointer and returns stack value
pub trait IntoMaybeLocPtr {
    /// Unwraps the pointer and returns stack value
    fn into_maybe_ptr(self) -> MaybeLocPtr
    where
        Self: Sized;
}

pub(crate) trait UnPtr {
    fn unptr(self) -> Loc
    where
        Self: Sized;
}