aboutsummaryrefslogtreecommitdiff
path: root/src/ptr_buffer.rs
blob: e1e2fc6cbfdedb2c3b5396416886f4ee5c4e9f3f (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! Pointer buffer;

use std::any::TypeId;
use std::mem::{size_of, MaybeUninit};
use std::ptr::addr_of;
use std::rc::Rc;
use std::sync::Arc;

/// Pointer buffer;
pub struct PtrBuffer
{
    buf: Box<[MaybeUninit<u8>]>,
    type_id: TypeId,
    kind: Kind,
}

impl PtrBuffer
{
    /// AA.
    #[must_use]
    pub fn new_from<Value, ValuePtr>(value: ValuePtr) -> Self
    where
        Value: ?Sized + 'static,
        ValuePtr: Into<SmartPtr<Value>>,
    {
        let value = value.into();

        let kind = value.kind();

        let buf = ptr_to_byte_buf(value.into_raw());

        Self {
            buf,
            type_id: TypeId::of::<Value>(),
            kind,
        }
    }

    pub(crate) fn cast_into_boxed<Dest>(self) -> Option<Box<Dest>>
    where
        Dest: ?Sized + 'static,
    {
        if !matches!(self.kind, Kind::Box) {
            return None;
        }

        let dest_ptr = self.cast_into()?;

        // SAFETY: We know the pointer was retrieved using Box::into_raw in the
        // new_from function since the kind is Kind::Box (checked above). We
        // also know it was the exact same pointed to type since this is checked in the
        // cast_into function
        Some(unsafe { Box::from_raw(dest_ptr) })
    }

    pub(crate) fn cast_into_rc<Dest>(self) -> Option<Rc<Dest>>
    where
        Dest: ?Sized + 'static,
    {
        if !matches!(self.kind, Kind::Rc) {
            return None;
        }

        let dest_ptr = self.cast_into()?;

        // SAFETY: We know the pointer was retrieved using Rc::into_raw in the
        // new_from function since the kind is Kind::Rc (checked above). We
        // also know it was the exact same pointed to type since this is checked in the
        // cast_into function
        Some(unsafe { Rc::from_raw(dest_ptr) })
    }

    #[cfg(feature = "async")]
    pub(crate) fn cast_into_arc<Dest>(self) -> Option<Arc<Dest>>
    where
        Dest: ?Sized + 'static,
    {
        if !matches!(self.kind, Kind::Arc) {
            return None;
        }

        let dest_ptr = self.cast_into()?;

        // SAFETY: We know the pointer was retrieved using Arc::into_raw in the
        // new_from function since the kind is Kind::Arc (checked above). We
        // also know it was the exact same pointed to type since this is checked in the
        // cast_into function
        Some(unsafe { Arc::from_raw(dest_ptr) })
    }

    fn cast_into<Dest>(self) -> Option<*mut Dest>
    where
        Dest: ?Sized + 'static,
    {
        if TypeId::of::<Dest>() != self.type_id {
            return None;
        }

        if size_of::<*mut Dest>() != self.buf.len() {
            // Pointer kinds are different so continuing would cause UB. This should
            // not be possible since the type IDs are the same but we check it just to
            // be extra safe
            return None;
        }

        let mut ptr = MaybeUninit::<*mut Dest>::uninit();

        // SAFETY:
        // - We know the source buffer is valid for reads the number of bytes since it is
        //   ensured by the array primitive
        // - We know the destination is valid for writes the number of bytes since we
        //   check above if the buffer length is the same as the size of *mut Dest
        unsafe {
            std::ptr::copy_nonoverlapping(
                self.buf.as_ptr().cast::<u8>(),
                ptr.as_mut_ptr().cast::<u8>(),
                self.buf.len(),
            );
        }

        // SAFETY: We initialize the value above by copying the buffer to it
        Some(unsafe { ptr.assume_init() })
    }
}

/// Smart pointers supported as input to [`PtrBuffer`].
#[derive(Debug)]
#[non_exhaustive]
pub enum SmartPtr<Value: ?Sized + 'static>
{
    /// Box.
    Box(Box<Value>),

    /// Rc.
    Rc(Rc<Value>),

    /// Arc.
    Arc(Arc<Value>),
}

impl<Value: ?Sized + 'static> SmartPtr<Value>
{
    fn into_raw(self) -> *const Value
    {
        match self {
            Self::Box(value) => Box::into_raw(value),
            Self::Rc(value) => Rc::into_raw(value),
            Self::Arc(value) => Arc::into_raw(value),
        }
    }

    fn kind(&self) -> Kind
    {
        match self {
            Self::Box(_) => Kind::Box,
            Self::Rc(_) => Kind::Rc,
            Self::Arc(_) => Kind::Arc,
        }
    }
}

impl<Value> From<Box<Value>> for SmartPtr<Value>
where
    Value: ?Sized + 'static,
{
    fn from(value: Box<Value>) -> Self
    {
        Self::Box(value)
    }
}

impl<Value> From<Rc<Value>> for SmartPtr<Value>
where
    Value: ?Sized + 'static,
{
    fn from(value: Rc<Value>) -> Self
    {
        Self::Rc(value)
    }
}

impl<Value> From<Arc<Value>> for SmartPtr<Value>
where
    Value: ?Sized + 'static,
{
    fn from(value: Arc<Value>) -> Self
    {
        Self::Arc(value)
    }
}

enum Kind
{
    Box,
    Rc,
    Arc,
}

fn ptr_to_byte_buf<Value>(value_ptr: *const Value) -> Box<[MaybeUninit<u8>]>
where
    Value: ?Sized + 'static,
{
    // Transform the full pointer (data pointer + (optional) metadata) into a byte
    // slice
    let value_ptr_bytes = unsafe {
        std::slice::from_raw_parts::<u8>(
            addr_of!(value_ptr).cast::<u8>(),
            size_of::<*const Value>(),
        )
    };

    value_ptr_bytes
        .iter()
        .map(|byte| MaybeUninit::new(*byte))
        .collect::<Vec<_>>()
        .into()
}

#[cfg(test)]
mod tests
{
    use std::mem::{size_of, transmute, MaybeUninit};
    use std::path::PathBuf;
    use std::rc::Rc;

    use crate::ptr_buffer::{ptr_to_byte_buf, PtrBuffer};

    trait Anything
    {
        fn get_value(&self) -> u32;
    }

    struct Something;

    impl Anything for Something
    {
        fn get_value(&self) -> u32
        {
            1234
        }
    }

    #[test]
    fn works_with_thin()
    {
        let text = Box::new("Hello there".to_string());

        let ptr_buf = PtrBuffer::new_from(text);

        assert!(ptr_buf
            .cast_into_boxed::<String>()
            .map_or_else(|| false, |text| *text == "Hello there"));
    }

    #[test]
    fn works_with_dyn()
    {
        let text: Box<dyn Anything> = Box::new(Something);

        let ptr_buf = PtrBuffer::new_from(text);

        assert!(ptr_buf
            .cast_into_boxed::<dyn Anything>()
            .map_or_else(|| false, |anything| anything.get_value() == 1234));
    }

    #[test]
    fn cast_box_when_wrong_kind_fails()
    {
        let text = Rc::new("Hello there".to_string());

        let ptr_buf = PtrBuffer::new_from(text);

        assert!(ptr_buf.cast_into_boxed::<String>().is_none());
    }

    #[test]
    fn cast_rc_when_wrong_kind_fails()
    {
        let text = Box::new("Hello there".to_string());

        let ptr_buf = PtrBuffer::new_from(text);

        assert!(ptr_buf.cast_into_rc::<String>().is_none());
    }

    #[test]
    #[cfg(feature = "async")]
    fn cast_arc_when_wrong_kind_fails()
    {
        let text = Box::new("Hello there".to_string());

        let ptr_buf = PtrBuffer::new_from(text);

        assert!(ptr_buf.cast_into_arc::<String>().is_none());
    }

    #[test]
    fn cast_into_fails_when_wrong_type()
    {
        let text = Box::new(123_456u64);

        let ptr_buf = PtrBuffer::new_from(text);

        assert!(ptr_buf.cast_into::<PathBuf>().is_none());
    }

    #[test]
    fn ptr_to_byte_buf_works()
    {
        let thin_ptr_addr = 123_456_789usize;

        assert_eq!(
            unsafe {
                slice_assume_init_ref(&ptr_to_byte_buf(thin_ptr_addr as *const u32))
            },
            thin_ptr_addr.to_ne_bytes()
        );

        let fat_ptr_addr_buf: [u8; size_of::<*const dyn Send>()] =
            [26, 88, 91, 77, 2, 0, 0, 0, 12, 34, 56, 78, 90, 9, 98, 87];

        let fat_ptr_addr: *const dyn Send = unsafe { transmute(fat_ptr_addr_buf) };

        assert_eq!(
            unsafe { slice_assume_init_ref(&ptr_to_byte_buf(fat_ptr_addr)) },
            &fat_ptr_addr_buf
        );
    }

    /// TODO: Remove when `MaybeUninit::slice_assume_init_ref` is stabilized
    const unsafe fn slice_assume_init_ref<T>(slice: &[MaybeUninit<T>]) -> &[T]
    {
        // SAFETY: casting `slice` to a `*const [T]` is safe since the caller guarantees
        // that `slice` is initialized, and `MaybeUninit` is guaranteed to have
        // the same layout as `T`. The pointer obtained is valid since it refers
        // to memory owned by `slice` which is a reference and thus guaranteed to
        // be valid for reads.
        unsafe { &*(slice as *const [MaybeUninit<T>] as *const [T]) }
    }
}