Skip to main content

fre_rs/
data.rs

1//! 
2//! Abstractions for safe native data handling across the FFI boundary,
3//! including pointer passing, access, and lifecycle management.
4//! 
5
6use super::*;
7
8
9/// Should be implemented for all native data types to ensure safe pointer passing
10/// and correct memory management. Native data includes extension data, context data,
11/// and function data.
12pub trait Data: 'static + Sized {
13    fn into_boxed (self) -> Box<dyn Any> {
14        super::data::into_boxed(self)
15    }
16    fn into_raw (self) -> NonNullFREData {
17        super::data::into_raw(self.into_boxed())
18    }
19    fn from_boxed (boxed: Box<dyn Any>) -> Result<Self, Box<dyn Any>> {
20        boxed.downcast()
21            .map(|b| *b)
22            .map_err(|b| b)
23    }
24    #[allow(unsafe_op_in_unsafe_fn)]
25    unsafe fn from_raw (raw: NonNullFREData) -> Self {
26        let boxed = super::data::from_raw(raw);
27        Self::from_boxed(boxed).unwrap()
28    }
29    #[allow(unsafe_op_in_unsafe_fn)]
30    unsafe fn ref_from <'a> (raw: NonNullFREData) -> Result<&'a Self, &'a dyn Any> {
31        let any = super::data::ref_from(raw);
32        any.downcast_ref().ok_or(any)
33    }
34    #[allow(unsafe_op_in_unsafe_fn)]
35    unsafe fn mut_from <'a> (raw: NonNullFREData) -> Result<&'a mut Self, &'a mut dyn Any> {
36        let fat = super::data::mut_from(raw) as *mut dyn Any;
37        (&mut (*fat)).downcast_mut().ok_or(&mut (*fat))
38    }
39}
40impl Data for () {}
41
42
43type DataPointer = *mut *mut (dyn Any + 'static);
44
45
46pub fn into_boxed <D: 'static> (data: D) -> Box<dyn Any> {
47    Box::new(data) as Box<dyn Any>
48}
49pub fn into_raw (boxed: Box<dyn Any>) -> NonNullFREData {
50    let fat = Box::into_raw(boxed);
51    let raw: DataPointer = Box::into_raw(Box::new(fat));
52    unsafe {NonNull::new_unchecked(raw as FREData)}
53}
54#[allow(unsafe_op_in_unsafe_fn)]
55pub unsafe fn from_raw (raw: NonNullFREData) -> Box<dyn Any> {
56    let raw = raw.as_ptr() as DataPointer;
57    let fat = Box::from_raw(raw);
58    let boxed = Box::from_raw(*fat);
59    boxed
60}
61#[allow(unsafe_op_in_unsafe_fn)]
62pub unsafe fn ref_from <'a> (raw: NonNullFREData) -> &'a dyn Any {
63    let raw = raw.as_ptr() as DataPointer;
64    let any = &(**raw);
65    any
66}
67#[allow(unsafe_op_in_unsafe_fn)]
68pub unsafe fn mut_from <'a> (raw: NonNullFREData) -> &'a mut dyn Any {
69    let raw = raw.as_ptr() as DataPointer;
70    let any = &mut (**raw);
71    any
72}
73#[allow(unsafe_op_in_unsafe_fn)]
74pub unsafe fn drop_from (raw: NonNullFREData) {
75    drop(from_raw(raw));
76}