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
//! This module provides types and traits associated to accessing of borrowed
//! values.
use std::{any::TypeId, marker::PhantomData, ptr::NonNull};

use atomic_refcell::AtomicRefCell;

use crate::{borrow::ContextBorrow, Error, IntoAccess, Result};

/// Holds all data necessary for the execution of the world.
/// The data is held by references, and needs to outlive the context itself
pub struct Context<'a> {
    data: &'a dyn Data,
}

// Safe since Send + Sync is required for impl of IntoData
unsafe impl Send for Context<'_> {}
unsafe impl Sync for Context<'_> {}

impl<'a> Context<'a> {
    /// Construct a new context from the tuple of references `data`
    pub fn new(data: &'a dyn Data) -> Context {
        Self { data }
    }

    /// Borrows data of type T from the context. Does not panic.
    pub fn borrow<T>(&'a self) -> Result<T::Target>
    where
        T: ContextBorrow<'a>,
    {
        T::borrow(self)
    }

    /// Returns the cell associated to `T`.
    /// **Note**: Types are erased, but casting is guaranteed to be correct.
    pub fn cell<T: IntoAccess>(&'a self) -> Result<&AtomicRefCell<NonNull<u8>>> {
        let access = T::access();
        self.data
            .get(access.id())
            .ok_or_else(|| Error::MissingData(access.name()))
    }
}

/// Dynamically accessed static collection of values
/// # Safety
/// Will type erase the given arguments by converting them to NonNull<u8>,
/// assuming the references are always non null.
pub unsafe trait Data {
    /// Get the cell associated to the `TypeId`.
    fn get(&self, ty: TypeId) -> Option<&AtomicRefCell<NonNull<u8>>>;
}

unsafe impl Data for () {
    fn get(&self, _: TypeId) -> Option<&AtomicRefCell<NonNull<u8>>> {
        None
    }
}

unsafe impl<A: 'static + Send + Sync> Data for (AtomicRefCell<NonNull<u8>>, PhantomData<A>) {
    fn get(&self, ty: TypeId) -> Option<&AtomicRefCell<NonNull<u8>>> {
        if ty == TypeId::of::<A>() {
            Some(&self.0)
        } else {
            None
        }
    }
}

/// Convert a tuple or other type into [Data].
pub trait IntoData: Send + Sync {
    /// The corresponding [Data] type.
    type Target: Data;
    /// Performs the conversion.
    /// # Safety
    /// Converts a tuple of references into NonNull. The lifetime is captures by
    /// [Context]
    unsafe fn into_data(self) -> Self::Target;
}

impl IntoData for () {
    type Target = ();

    unsafe fn into_data(self) -> Self::Target {}
}

macro_rules! tuple_impls {
    () => {};
    (($idx:tt => $typ:ident), $( ($nidx:tt => $ntyp:ident), )*) => {
        /*
         * Invoke recursive reversal of list that ends in the macro expansion implementation
         * of the reversed list
        */
        tuple_impls!([($idx, $typ);] $( ($nidx => $ntyp), )*);
        tuple_impls!($( ($nidx => $ntyp), )*); // invoke macro on tail
    };

        /*
     * ([accumulatedList], listToReverse); recursively calls tuple_impls until the list to reverse
     + is empty (see next pattern)
    */
    ([$(($accIdx: tt, $accTyp: ident);)+]  ($idx:tt => $typ:ident), $( ($nidx:tt => $ntyp:ident), )*) => {
      tuple_impls!([($idx, $typ); $(($accIdx, $accTyp); )*] $( ($nidx => $ntyp), ) *);
    };

    // Finally expand into the implementation
    ([($idx:tt, $typ:ident); $( ($nidx:tt, $ntyp:ident); )*]) => {
        impl<$typ, $( $ntyp ), *> IntoData for (&mut $typ, $(&mut $ntyp,) *)
            where
                $typ: 'static + Send + Sync,
                $($ntyp: 'static + Send + Sync), *
        {
            type Target = ((AtomicRefCell<NonNull<u8>>, PhantomData<$typ>), $( (AtomicRefCell<NonNull<u8>>, PhantomData<$ntyp>), )*);

            unsafe fn into_data(self) -> Self::Target {
                (
                    (AtomicRefCell::new(NonNull::new_unchecked(self.$idx as *mut _ as *mut u8)), PhantomData),
                    $( (AtomicRefCell::new(NonNull::new_unchecked(self.$nidx as *mut _ as *mut u8)), PhantomData), ) *
                )
            }

        }

        unsafe impl<$typ, $( $ntyp ), *> Data for ((AtomicRefCell<NonNull<u8>>, PhantomData<$typ>), $( (AtomicRefCell<NonNull<u8>>, PhantomData<$ntyp>), )*)
            where
                $typ: 'static,
                $($ntyp: 'static), *

        {
            fn get<'a>(&'a self, ty: TypeId) -> Option<&AtomicRefCell<NonNull<u8>>> {
                if ty == TypeId::of::<$typ>() {
                    Some(&self.$idx.0)
                } $(else if ty == TypeId::of::<$ntyp>() {
                    Some(&self.$nidx.0)
                }) *
                else {
                    None
                }
            }
        }
    };
}

tuple_impls!(
    (9 => J),
    (8 => I),
    (7 => H),
    (6 => G),
    (5 => F),
    (4 => E),
    (3 => D),
    (2 => C),
    (1 => B),
    (0 => A),
);

#[cfg(test)]
mod tests {
    use super::{Context, IntoData};

    #[test]
    fn context() {
        let mut a = 64_i32;
        let mut b = "Hello, World";

        let data = unsafe { (&mut a, &mut b).into_data() };

        let context = Context::new(&data);

        {
            let a = context.borrow::<&i32>().unwrap();
            let mut b = context.borrow::<&mut &str>().unwrap();

            assert_eq!(*b, "Hello, World");
            *b = "Foo Fighters";
            drop(b);

            let b = context.borrow::<&&str>().unwrap();
            assert_eq!(*a, 64);
            assert_eq!(*b, "Foo Fighters");

            let c = context.borrow::<&f32>();
            assert!(c.is_err());
        }
    }
}