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
#[cfg(not(feature = "c-structures"))]
pub(crate) mod rust {
    /// Rust-compatible shared list
    pub type SharedList<'a, T> = &'a [T];
}

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

    /// C-compatible shared list
    pub struct SharedList<T> {
        ptr: *mut T,
        len: usize,
    }

    impl<T> std::fmt::Debug for SharedList<T>
    where
        T: std::fmt::Debug,
    {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            std::fmt::Debug::fmt(&**self, f)
        }
    }

    impl<T> Deref for SharedList<T> {
        type Target = [T];

        fn deref(&self) -> &[T] {
            unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
        }
    }

    impl<T> SharedList<T> {
        pub(crate) fn from_raw(ptr: *mut T, len: usize) -> Self {
            Self { ptr, len }
        }
    }
}