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
52#[allow(clippy::unnecessary_cast)]
53impl<T> Pointer for *mut T {
54    type Target = T;
55
56    #[inline]
57    fn as_ref(&self) -> &Self::Target {
58        unsafe { &**self }
59    }
60
61    unsafe fn from_raw(p: *const Self::Target) -> Self {
62        p as *mut T
63    }
64
65    fn into_raw(self) -> *const Self::Target {
66        self as *mut T
67    }
68}
69
70impl<T> Pointer for NonNull<T> {
71    type Target = T;
72
73    #[inline]
74    fn as_ref(&self) -> &Self::Target {
75        unsafe { self.as_ref() }
76    }
77
78    unsafe fn from_raw(p: *const Self::Target) -> Self {
79        unsafe { NonNull::new_unchecked(p as *mut T) }
80    }
81
82    fn into_raw(self) -> *const Self::Target {
83        self.as_ptr()
84    }
85}
86
87impl<T> Pointer for Box<T> {
88    type Target = T;
89
90    #[inline]
91    fn as_ref(&self) -> &Self::Target {
92        self
93    }
94
95    unsafe fn from_raw(p: *const Self::Target) -> Self {
96        unsafe { Box::from_raw(p as *mut T) }
97    }
98
99    fn into_raw(self) -> *const Self::Target {
100        Box::into_raw(self)
101    }
102}
103
104impl<T> Pointer for Rc<T> {
105    type Target = T;
106
107    #[inline]
108    fn as_ref(&self) -> &Self::Target {
109        self
110    }
111
112    unsafe fn from_raw(p: *const Self::Target) -> Self {
113        unsafe { Rc::from_raw(p) }
114    }
115
116    fn into_raw(self) -> *const Self::Target {
117        Rc::into_raw(self)
118    }
119}
120
121impl<T> Pointer for Arc<T> {
122    type Target = T;
123
124    #[inline]
125    fn as_ref(&self) -> &Self::Target {
126        self
127    }
128
129    unsafe fn from_raw(p: *const Self::Target) -> Self {
130        unsafe { Arc::from_raw(p) }
131    }
132
133    fn into_raw(self) -> *const Self::Target {
134        Arc::into_raw(self)
135    }
136}
137
138#[cfg(feature = "avl")]
139pub mod avl;
140pub mod const_vec;
141pub use const_vec::ConstVec;
142#[cfg(feature = "dlist")]
143pub mod dlist;
144pub mod seg_list;
145pub use seg_list::SegList;
146#[cfg(feature = "slist")]
147pub mod slist;
148#[cfg(feature = "slist")]
149pub mod slist_owned;
150pub mod various;
151pub use various::Various;