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::mem;
14use core::ptr::NonNull;
15
16/// Abstract pointer trait to support various pointer types in collections.
17///
18/// This trait allows the collections to work with:
19/// - `Box<T>`: Owned, automatically dropped.
20/// - `Arc<T>`: Shared ownership.
21/// - `Rc<T>`: Single thread ownership.
22/// - `NonNull<T>`: Raw non-null pointers (manual memory management).
23/// - `*const T`: Raw pointers (recommend to use `NonNull<T>` instead)
24pub trait Pointer: Sized {
25    type Target;
26
27    fn as_ref(&self) -> &Self::Target;
28
29    unsafe fn from_raw(p: *const Self::Target) -> Self;
30
31    fn into_raw(self) -> *const Self::Target;
32}
33
34impl<T> Pointer for *const T {
35    type Target = T;
36
37    #[inline]
38    fn as_ref(&self) -> &Self::Target {
39        unsafe { mem::transmute(*self) }
40    }
41
42    unsafe fn from_raw(p: *const Self::Target) -> Self {
43        p as *const T
44    }
45
46    fn into_raw(self) -> *const Self::Target {
47        self as *const T
48    }
49}
50
51impl<T> Pointer for NonNull<T> {
52    type Target = T;
53
54    #[inline]
55    fn as_ref(&self) -> &Self::Target {
56        unsafe { self.as_ref() }
57    }
58
59    unsafe fn from_raw(p: *const Self::Target) -> Self {
60        unsafe { NonNull::new_unchecked(p as *mut T) }
61    }
62
63    fn into_raw(self) -> *const Self::Target {
64        self.as_ptr()
65    }
66}
67
68impl<T> Pointer for Box<T> {
69    type Target = T;
70
71    #[inline]
72    fn as_ref(&self) -> &Self::Target {
73        &**self
74    }
75
76    unsafe fn from_raw(p: *const Self::Target) -> Self {
77        unsafe { Box::from_raw(p as *mut T) }
78    }
79
80    fn into_raw(self) -> *const Self::Target {
81        Box::into_raw(self)
82    }
83}
84
85impl<T> Pointer for Rc<T> {
86    type Target = T;
87
88    #[inline]
89    fn as_ref(&self) -> &Self::Target {
90        &**self
91    }
92
93    unsafe fn from_raw(p: *const Self::Target) -> Self {
94        unsafe { Rc::from_raw(p) }
95    }
96
97    fn into_raw(self) -> *const Self::Target {
98        Rc::into_raw(self)
99    }
100}
101
102impl<T> Pointer for Arc<T> {
103    type Target = T;
104
105    #[inline]
106    fn as_ref(&self) -> &Self::Target {
107        &**self
108    }
109
110    unsafe fn from_raw(p: *const Self::Target) -> Self {
111        unsafe { Arc::from_raw(p) }
112    }
113
114    fn into_raw(self) -> *const Self::Target {
115        Arc::into_raw(self)
116    }
117}
118
119#[cfg(feature = "avl")]
120pub mod avl;
121#[cfg(feature = "dlist")]
122pub mod dlist;
123#[cfg(feature = "avl")]
124pub mod range_tree;
125#[cfg(feature = "slist")]
126pub mod slist;