Skip to main content

embed_collections/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![cfg_attr(docsrs, allow(unused_attributes))]
3#![cfg_attr(not(feature = "std"), no_std)]
4#![doc = include_str!("../README.md")]
5
6extern crate alloc;
7#[cfg(any(feature = "std", test))]
8extern crate std;
9
10use alloc::boxed::Box;
11use alloc::rc::Rc;
12use alloc::sync::Arc;
13use core::ptr::NonNull;
14
15/// Abstract pointer trait to support various pointer types in collections.
16///
17/// This trait allows the collections to work with:
18/// - `Box<T>`: Owned, automatically dropped.
19/// - `Arc<T>`: Shared ownership.
20/// - `Rc<T>`: Single thread ownership.
21/// - `NonNull<T>`: Raw non-null pointers (manual memory management).
22/// - `*const T`: Raw pointers (recommend to use `NonNull<T>` instead)
23pub trait Pointer: Sized {
24    type Target;
25
26    fn as_ref(&self) -> &Self::Target;
27
28    #[allow(clippy::missing_safety_doc)]
29    unsafe fn from_raw(p: *const Self::Target) -> Self;
30
31    fn into_raw(self) -> *const Self::Target;
32}
33
34#[allow(clippy::unnecessary_cast)]
35impl<T> Pointer for *const T {
36    type Target = T;
37
38    #[inline]
39    fn as_ref(&self) -> &Self::Target {
40        unsafe { &**self }
41    }
42
43    unsafe fn from_raw(p: *const Self::Target) -> Self {
44        p as *const T
45    }
46
47    fn into_raw(self) -> *const Self::Target {
48        self as *const T
49    }
50}
51
52impl<T> Pointer for NonNull<T> {
53    type Target = T;
54
55    #[inline]
56    fn as_ref(&self) -> &Self::Target {
57        unsafe { self.as_ref() }
58    }
59
60    unsafe fn from_raw(p: *const Self::Target) -> Self {
61        unsafe { NonNull::new_unchecked(p as *mut T) }
62    }
63
64    fn into_raw(self) -> *const Self::Target {
65        self.as_ptr()
66    }
67}
68
69impl<T> Pointer for Box<T> {
70    type Target = T;
71
72    #[inline]
73    fn as_ref(&self) -> &Self::Target {
74        self
75    }
76
77    unsafe fn from_raw(p: *const Self::Target) -> Self {
78        unsafe { Box::from_raw(p as *mut T) }
79    }
80
81    fn into_raw(self) -> *const Self::Target {
82        Box::into_raw(self)
83    }
84}
85
86impl<T> Pointer for Rc<T> {
87    type Target = T;
88
89    #[inline]
90    fn as_ref(&self) -> &Self::Target {
91        self
92    }
93
94    unsafe fn from_raw(p: *const Self::Target) -> Self {
95        unsafe { Rc::from_raw(p) }
96    }
97
98    fn into_raw(self) -> *const Self::Target {
99        Rc::into_raw(self)
100    }
101}
102
103impl<T> Pointer for Arc<T> {
104    type Target = T;
105
106    #[inline]
107    fn as_ref(&self) -> &Self::Target {
108        self
109    }
110
111    unsafe fn from_raw(p: *const Self::Target) -> Self {
112        unsafe { Arc::from_raw(p) }
113    }
114
115    fn into_raw(self) -> *const Self::Target {
116        Arc::into_raw(self)
117    }
118}
119
120#[cfg(feature = "avl")]
121pub mod avl;
122pub mod const_vec;
123pub use const_vec::ConstVec;
124#[cfg(feature = "dlist")]
125pub mod dlist;
126pub mod seg_list;
127pub use seg_list::SegList;
128#[cfg(feature = "slist")]
129pub mod slist;
130#[cfg(feature = "slist")]
131pub mod slist_owned;
132pub mod various;
133pub use various::Various;