diff options
Diffstat (limited to 'src/sync.rs')
-rw-r--r-- | src/sync.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/sync.rs b/src/sync.rs new file mode 100644 index 0000000..2b7e1ff --- /dev/null +++ b/src/sync.rs @@ -0,0 +1,33 @@ +pub mod interface { + pub trait Mutex { + type Data; + fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut Self::Data) -> R) -> R; + } +} + +use core::cell::UnsafeCell; + +pub struct NullLock<T> where T: ?Sized { + data: UnsafeCell<T>, +} + +unsafe impl<T> Send for NullLock<T> where T: ?Sized + Send {} +unsafe impl<T> Sync for NullLock<T> where T: ?Sized + Send {} + +impl<T> NullLock<T> { + pub const fn new(data: T) -> Self { + Self { + data: UnsafeCell::new(data), + } + } +} + +impl<T> interface::Mutex for NullLock<T> { + type Data = T; + + fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut T) -> R) -> R { + let data = unsafe { &mut *self.data.get() }; + + f(data) + } +} |