movable_ref/metadata/
impls.rs

1use super::traits::PointerRecomposition;
2use crate::offset::Ptr;
3use std::ptr::NonNull;
4
5#[cfg(not(feature = "std"))]
6extern crate alloc;
7#[cfg(not(feature = "std"))]
8use alloc::{string::String, vec::Vec};
9
10unsafe impl<T: ?Sized> PointerRecomposition for &T {
11    type Components = ();
12    #[inline]
13    fn decompose(_: &Self) -> Self::Components {}
14    #[inline]
15    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
16        ptr.map(NonNull::cast)
17    }
18}
19unsafe impl<T: ?Sized> PointerRecomposition for &mut T {
20    type Components = ();
21    #[inline]
22    fn decompose(_: &Self) -> Self::Components {}
23    #[inline]
24    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
25        ptr.map(NonNull::cast)
26    }
27}
28
29unsafe impl PointerRecomposition for u8 {
30    type Components = ();
31    #[inline]
32    fn decompose(_: &Self) -> Self::Components {}
33    #[inline]
34    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
35        ptr.map(NonNull::cast)
36    }
37}
38unsafe impl PointerRecomposition for u16 {
39    type Components = ();
40    #[inline]
41    fn decompose(_: &Self) -> Self::Components {}
42    #[inline]
43    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
44        ptr.map(NonNull::cast)
45    }
46}
47unsafe impl PointerRecomposition for u32 {
48    type Components = ();
49    #[inline]
50    fn decompose(_: &Self) -> Self::Components {}
51    #[inline]
52    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
53        ptr.map(NonNull::cast)
54    }
55}
56unsafe impl PointerRecomposition for u64 {
57    type Components = ();
58    #[inline]
59    fn decompose(_: &Self) -> Self::Components {}
60    #[inline]
61    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
62        ptr.map(NonNull::cast)
63    }
64}
65unsafe impl PointerRecomposition for u128 {
66    type Components = ();
67    #[inline]
68    fn decompose(_: &Self) -> Self::Components {}
69    #[inline]
70    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
71        ptr.map(NonNull::cast)
72    }
73}
74unsafe impl PointerRecomposition for usize {
75    type Components = ();
76    #[inline]
77    fn decompose(_: &Self) -> Self::Components {}
78    #[inline]
79    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
80        ptr.map(NonNull::cast)
81    }
82}
83
84unsafe impl PointerRecomposition for i8 {
85    type Components = ();
86    #[inline]
87    fn decompose(_: &Self) -> Self::Components {}
88    #[inline]
89    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
90        ptr.map(NonNull::cast)
91    }
92}
93unsafe impl PointerRecomposition for i16 {
94    type Components = ();
95    #[inline]
96    fn decompose(_: &Self) -> Self::Components {}
97    #[inline]
98    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
99        ptr.map(NonNull::cast)
100    }
101}
102unsafe impl PointerRecomposition for i32 {
103    type Components = ();
104    #[inline]
105    fn decompose(_: &Self) -> Self::Components {}
106    #[inline]
107    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
108        ptr.map(NonNull::cast)
109    }
110}
111unsafe impl PointerRecomposition for i64 {
112    type Components = ();
113    #[inline]
114    fn decompose(_: &Self) -> Self::Components {}
115    #[inline]
116    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
117        ptr.map(NonNull::cast)
118    }
119}
120unsafe impl PointerRecomposition for i128 {
121    type Components = ();
122    #[inline]
123    fn decompose(_: &Self) -> Self::Components {}
124    #[inline]
125    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
126        ptr.map(NonNull::cast)
127    }
128}
129unsafe impl PointerRecomposition for isize {
130    type Components = ();
131    #[inline]
132    fn decompose(_: &Self) -> Self::Components {}
133    #[inline]
134    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
135        ptr.map(NonNull::cast)
136    }
137}
138
139unsafe impl PointerRecomposition for f32 {
140    type Components = ();
141    #[inline]
142    fn decompose(_: &Self) -> Self::Components {}
143    #[inline]
144    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
145        ptr.map(NonNull::cast)
146    }
147}
148unsafe impl PointerRecomposition for f64 {
149    type Components = ();
150    #[inline]
151    fn decompose(_: &Self) -> Self::Components {}
152    #[inline]
153    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
154        ptr.map(NonNull::cast)
155    }
156}
157
158unsafe impl PointerRecomposition for bool {
159    type Components = ();
160    #[inline]
161    fn decompose(_: &Self) -> Self::Components {}
162    #[inline]
163    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
164        ptr.map(NonNull::cast)
165    }
166}
167unsafe impl PointerRecomposition for char {
168    type Components = ();
169    #[inline]
170    fn decompose(_: &Self) -> Self::Components {}
171    #[inline]
172    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
173        ptr.map(NonNull::cast)
174    }
175}
176
177// Arrays
178unsafe impl<T, const N: usize> PointerRecomposition for [T; N] {
179    type Components = ();
180    #[inline]
181    fn decompose(_: &Self) -> Self::Components {}
182    #[inline]
183    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
184        ptr.map(NonNull::cast)
185    }
186}
187
188// Common container types
189unsafe impl<T> PointerRecomposition for Option<T> {
190    type Components = ();
191    #[inline]
192    fn decompose(_: &Self) -> Self::Components {}
193    #[inline]
194    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
195        ptr.map(NonNull::cast)
196    }
197}
198unsafe impl<T, E> PointerRecomposition for Result<T, E> {
199    type Components = ();
200    #[inline]
201    fn decompose(_: &Self) -> Self::Components {}
202    #[inline]
203    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
204        ptr.map(NonNull::cast)
205    }
206}
207unsafe impl<T> PointerRecomposition for Vec<T> {
208    type Components = ();
209    #[inline]
210    fn decompose(_: &Self) -> Self::Components {}
211    #[inline]
212    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
213        ptr.map(NonNull::cast)
214    }
215}
216unsafe impl PointerRecomposition for String {
217    type Components = ();
218    #[inline]
219    fn decompose(_: &Self) -> Self::Components {}
220    #[inline]
221    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
222        ptr.map(NonNull::cast)
223    }
224}
225
226unsafe impl PointerRecomposition for () {
227    type Components = ();
228    #[inline]
229    fn decompose(_: &Self) -> Self::Components {}
230    #[inline]
231    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
232        ptr.map(NonNull::cast)
233    }
234}
235unsafe impl<A> PointerRecomposition for (A,) {
236    type Components = ();
237    #[inline]
238    fn decompose(_: &Self) -> Self::Components {}
239    #[inline]
240    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
241        ptr.map(NonNull::cast)
242    }
243}
244unsafe impl<A, B> PointerRecomposition for (A, B) {
245    type Components = ();
246    #[inline]
247    fn decompose(_: &Self) -> Self::Components {}
248    #[inline]
249    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
250        ptr.map(NonNull::cast)
251    }
252}
253unsafe impl<A, B, C> PointerRecomposition for (A, B, C) {
254    type Components = ();
255    #[inline]
256    fn decompose(_: &Self) -> Self::Components {}
257    #[inline]
258    unsafe fn recompose(ptr: Ptr<u8>, (): Self::Components) -> Ptr<Self> {
259        ptr.map(NonNull::cast)
260    }
261}
262
263unsafe impl<T> PointerRecomposition for [T] {
264    type Components = usize;
265
266    #[inline]
267    fn decompose(this: &Self) -> Self::Components {
268        this.len()
269    }
270
271    #[inline]
272    unsafe fn recompose(ptr: Ptr<u8>, data: Self::Components) -> Ptr<Self> {
273        let ptr = ptr?.cast::<T>();
274        Some(NonNull::slice_from_raw_parts(ptr, data))
275    }
276}
277
278unsafe impl PointerRecomposition for str {
279    type Components = usize;
280
281    #[inline]
282    fn decompose(this: &Self) -> Self::Components {
283        this.len()
284    }
285
286    #[inline]
287    unsafe fn recompose(ptr: Ptr<u8>, data: Self::Components) -> Ptr<Self> {
288        let ptr = ptr?.as_ptr();
289        let slice = std::ptr::slice_from_raw_parts_mut(ptr, data);
290        NonNull::new(slice as *mut str)
291    }
292}