aboutsummaryrefslogtreecommitdiff
path: root/src/ctxt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/ctxt.rs')
-rw-r--r--src/ctxt.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/ctxt.rs b/src/ctxt.rs
new file mode 100644
index 0000000..7cd8a74
--- /dev/null
+++ b/src/ctxt.rs
@@ -0,0 +1,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 {}