diff options
author | 2017-03-07 22:56:06 -0500 | |
---|---|---|
committer | 2017-03-07 22:58:33 -0500 | |
commit | c3a35c1b6cea81aa71e8832bca79ccafa492be02 (patch) | |
tree | e868906d20c906d238be96cd5c07b07342f7a111 /src/ctxt.rs | |
parent | 9d3f3f323f3b7543d0b49e773aea2c68e535ec83 (diff) | |
download | cortex-m-c3a35c1b6cea81aa71e8832bca79ccafa492be02.tar.gz cortex-m-c3a35c1b6cea81aa71e8832bca79ccafa492be02.tar.zst cortex-m-c3a35c1b6cea81aa71e8832bca79ccafa492be02.zip |
revamp for memory safety
Diffstat (limited to 'src/ctxt.rs')
-rw-r--r-- | src/ctxt.rs | 34 |
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 {} |