aboutsummaryrefslogtreecommitdiff
path: root/src/ctxt.rs
blob: 7cd8a74090f867e6ff803858f39121b2290d2371 (plain) (blame)
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
//! Execution context

use core::marker::PhantomData;
use core::cell::UnsafeCell;

/// Data local to an execution context
pub struct Local<T, Ctxt>
    where Ctxt: Token
{
    _ctxt: PhantomData<Ctxt>,
    data: UnsafeCell<T>,
}

impl<T, Ctxt> Local<T, Ctxt>
    where Ctxt: Token
{
    /// Initializes the local data
    pub const fn new(value: T) -> Self {
        Local {
            _ctxt: PhantomData,
            data: UnsafeCell::new(value),
        }
    }

    /// Acquires a reference to the local data
    pub fn borrow<'a>(&'static self, _ctxt: &'a Ctxt) -> &'a T {
        unsafe { &*self.data.get() }
    }
}

unsafe impl<T, Ctxt> Sync for Local<T, Ctxt> where Ctxt: Token {}

/// A unique token that identifies an execution context
pub unsafe trait Token {}