Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 47f3542

Browse files
committedMar 30, 2023
update safety comments
1 parent ffa028d commit 47f3542

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed
 

‎library/core/src/task/wake.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ impl PartialEq for RawWakerVTable {
180180
#[stable(feature = "futures_api", since = "1.36.0")]
181181
impl fmt::Debug for RawWakerVTable {
182182
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183+
// SAFETY: Matching on unions is always unsafe.
184+
// The determinant of this union is that `v2`'s padding must always be null pointers, i.e. v1's adapter fields must be `Some(_)`
183185
unsafe {
184186
match self {
185-
RawWakerVTable { v1 } if v1.other_adapter.is_none() => self.v1.fmt(f),
187+
RawWakerVTable { v1 } if v1.other_adapter.is_some() => self.v1.fmt(f),
186188
RawWakerVTable { v2 } => v2.fmt(f),
187189
}
188190
}
@@ -196,12 +198,14 @@ unsafe extern "C" fn clone_adapter(
196198
clone: unsafe fn(*const ()) -> RawWaker,
197199
data: *const (),
198200
) -> RawWaker {
201+
// SAFETY: The safety constraints are passed up to the caller
199202
unsafe { (clone)(data) }
200203
}
201204
#[allow(improper_ctypes_definitions)]
202205
/// # Safety
203206
/// This function must only be called with function pointers sourced from the same shared object
204207
unsafe extern "C" fn other_adapter(other: unsafe fn(*const ()), data: *const ()) {
208+
// SAFETY: The safety constraints are passed up to the caller
205209
unsafe { (other)(data) }
206210
}
207211
impl RawWakerVTable {
@@ -447,6 +451,8 @@ impl Waker {
447451
// SAFETY: This is safe because `Waker::from_raw` is the only way
448452
// to initialize `wake` and `data` requiring the user to acknowledge
449453
// that the contract of `RawWaker` is upheld.
454+
// Matching on unions is always unsafe.
455+
// The determinant of this union is that `v2`'s padding must always be null pointers, i.e. v1's adapter fields must be `Some(_)`
450456
unsafe {
451457
match *vtable {
452458
RawWakerVTable {
@@ -470,6 +476,8 @@ impl Waker {
470476
let RawWaker { data, vtable } = self.waker;
471477

472478
// SAFETY: see `wake`
479+
// Matching on unions is always unsafe.
480+
// The determinant of this union is that `v2`'s padding must always be null pointers, i.e. v1's adapter fields must be `Some(_)`
473481
unsafe {
474482
match *vtable {
475483
RawWakerVTable {
@@ -525,6 +533,8 @@ impl Clone for Waker {
525533
// SAFETY: This is safe because `Waker::from_raw` is the only way
526534
// to initialize `clone` and `data` requiring the user to acknowledge
527535
// that the contract of [`RawWaker`] is upheld.
536+
// Matching on unions is always unsafe.
537+
// The determinant of this union is that `v2`'s padding must always be null pointers, i.e. v1's adapter fields must be `Some(_)`
528538
waker: unsafe {
529539
match *vtable {
530540
RawWakerVTable {
@@ -545,6 +555,8 @@ impl Drop for Waker {
545555
// SAFETY: This is safe because `Waker::from_raw` is the only way
546556
// to initialize `drop` and `data` requiring the user to acknowledge
547557
// that the contract of `RawWaker` is upheld.
558+
// Matching on unions is always unsafe.
559+
// The determinant of this union is that `v2`'s padding must always be null pointers, i.e. v1's adapter fields must be `Some(_)`
548560
unsafe {
549561
match *vtable {
550562
RawWakerVTable {

0 commit comments

Comments
 (0)
Please sign in to comment.