5
5
#![ no_std]
6
6
7
7
use core:: cell:: UnsafeCell ;
8
+ use core:: marker:: PhantomData ;
8
9
9
10
/// A peripheral
10
11
#[ derive( Debug ) ]
@@ -26,7 +27,7 @@ impl<T> Peripheral<T> {
26
27
}
27
28
28
29
/// 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 {
30
31
unsafe { & * self . get ( ) }
31
32
}
32
33
@@ -39,17 +40,18 @@ impl<T> Peripheral<T> {
39
40
/// Critical section token
40
41
///
41
42
/// 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 ( ) > ,
44
46
}
45
47
46
- impl CriticalSection {
48
+ impl < ' cs > CriticalSection < ' cs > {
47
49
/// Creates a critical section token
48
50
///
49
51
/// This method is meant to be used to create safe abstractions rather than
50
52
/// meant to be directly used in applications.
51
53
pub unsafe fn new ( ) -> Self {
52
- CriticalSection { _0 : ( ) }
54
+ CriticalSection { _0 : PhantomData }
53
55
}
54
56
}
55
57
@@ -75,13 +77,13 @@ impl<T> Mutex<T> {
75
77
76
78
impl < T > Mutex < T > {
77
79
/// 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 {
79
81
unsafe { & * self . inner . get ( ) }
80
82
}
81
83
}
82
84
83
85
/// ``` compile_fail
84
- /// fn bad(cs: & bare_metal::CriticalSection) -> &u32 {
86
+ /// fn bad(cs: bare_metal::CriticalSection) -> &u32 {
85
87
/// let x = bare_metal::Mutex::new(42u32);
86
88
/// x.borrow(cs)
87
89
/// }
0 commit comments