Skip to content

Commit 23156b0

Browse files
committed
Use CriticalSection<'cs> instead of &'cs CriticalSection.
Fixes #7. A `CriticalSection<'cs>` has a size of zero unlike a `&'cs CriticalSection`, which has the size of a pointer.
1 parent a2f8b22 commit 23156b0

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![no_std]
66

77
use core::cell::UnsafeCell;
8+
use core::marker::PhantomData;
89

910
/// A peripheral
1011
#[derive(Debug)]
@@ -26,7 +27,7 @@ impl<T> Peripheral<T> {
2627
}
2728

2829
/// Borrows the peripheral for the duration of a critical section
29-
pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
30+
pub fn borrow<'cs>(&self, _ctxt: CriticalSection<'cs>) -> &'cs T {
3031
unsafe { &*self.get() }
3132
}
3233

@@ -39,17 +40,18 @@ impl<T> Peripheral<T> {
3940
/// Critical section token
4041
///
4142
/// Indicates that you are executing code within a critical section
42-
pub struct CriticalSection {
43-
_0: (),
43+
#[derive(Clone, Copy)]
44+
pub struct CriticalSection<'cs> {
45+
_0: PhantomData<&'cs ()>,
4446
}
4547

46-
impl CriticalSection {
48+
impl<'cs> CriticalSection<'cs> {
4749
/// Creates a critical section token
4850
///
4951
/// This method is meant to be used to create safe abstractions rather than
5052
/// meant to be directly used in applications.
5153
pub unsafe fn new() -> Self {
52-
CriticalSection { _0: () }
54+
CriticalSection { _0: PhantomData }
5355
}
5456
}
5557

@@ -75,13 +77,13 @@ impl<T> Mutex<T> {
7577

7678
impl<T> Mutex<T> {
7779
/// Borrows the data for the duration of the critical section
78-
pub fn borrow<'cs>(&'cs self, _cs: &'cs CriticalSection) -> &'cs T {
80+
pub fn borrow<'cs>(&'cs self, _cs: CriticalSection<'cs>) -> &'cs T {
7981
unsafe { &*self.inner.get() }
8082
}
8183
}
8284

8385
/// ``` compile_fail
84-
/// fn bad(cs: &bare_metal::CriticalSection) -> &u32 {
86+
/// fn bad(cs: bare_metal::CriticalSection) -> &u32 {
8587
/// let x = bare_metal::Mutex::new(42u32);
8688
/// x.borrow(cs)
8789
/// }

0 commit comments

Comments
 (0)