aboutsummaryrefslogtreecommitdiff
path: root/src/sync.rs
blob: 2b7e1ffeb916fe63013762455c141039f2a9b635 (plain)
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
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)
	}
}