@@ -4,21 +4,22 @@ use std::task::Waker;
4
4
5
5
const NUM_WAKERS : usize = 32 ;
6
6
7
+ /// A list of wakers to be woken.
8
+ ///
9
+ /// # Invariants
10
+ ///
11
+ /// The first `curr` elements of `inner` are initialized.
7
12
pub ( crate ) struct WakeList {
8
13
inner : [ MaybeUninit < Waker > ; NUM_WAKERS ] ,
9
14
curr : usize ,
10
15
}
11
16
12
17
impl WakeList {
13
18
pub ( crate ) fn new ( ) -> Self {
19
+ const UNINIT_WAKER : MaybeUninit < Waker > = MaybeUninit :: uninit ( ) ;
20
+
14
21
Self {
15
- inner : unsafe {
16
- // safety: Create an uninitialized array of `MaybeUninit`. The
17
- // `assume_init` is safe because the type we are claiming to
18
- // have initialized here is a bunch of `MaybeUninit`s, which do
19
- // not require initialization.
20
- MaybeUninit :: uninit ( ) . assume_init ( )
21
- } ,
22
+ inner : [ UNINIT_WAKER ; NUM_WAKERS ] ,
22
23
curr : 0 ,
23
24
}
24
25
}
@@ -39,6 +40,8 @@ impl WakeList {
39
40
assert ! ( self . curr <= NUM_WAKERS ) ;
40
41
while self . curr > 0 {
41
42
self . curr -= 1 ;
43
+ // SAFETY: The first `curr` elements of `WakeList` are initialized, so by decrementing
44
+ // `curr`, we can take ownership of the last item.
42
45
let waker = unsafe { ptr:: read ( self . inner [ self . curr ] . as_mut_ptr ( ) ) } ;
43
46
waker. wake ( ) ;
44
47
}
@@ -47,7 +50,9 @@ impl WakeList {
47
50
48
51
impl Drop for WakeList {
49
52
fn drop ( & mut self ) {
50
- let slice = ptr:: slice_from_raw_parts_mut ( self . inner . as_mut_ptr ( ) as * mut Waker , self . curr ) ;
53
+ let slice =
54
+ ptr:: slice_from_raw_parts_mut ( self . inner . as_mut_ptr ( ) . cast :: < Waker > ( ) , self . curr ) ;
55
+ // SAFETY: The first `curr` elements are initialized, so we can drop them.
51
56
unsafe { ptr:: drop_in_place ( slice) } ;
52
57
}
53
58
}
0 commit comments