Skip to content

Commit 3f1c814

Browse files
committed
Remove unsafe code from AssertUnmoved
1 parent de8e42e commit 3f1c814

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

futures-test/src/assert_unmoved.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use futures_io::{
77
use futures_sink::Sink;
88
use pin_project::{pin_project, pinned_drop};
99
use std::pin::Pin;
10-
use std::ptr;
1110
use std::thread::panicking;
1211

1312
/// Combinator that asserts that the underlying type is not moved after being polled.
@@ -24,26 +23,21 @@ use std::thread::panicking;
2423
pub struct AssertUnmoved<T> {
2524
#[pin]
2625
inner: T,
27-
this_ptr: *const Self,
26+
this_addr: usize,
2827
}
2928

30-
// Safety: having a raw pointer in a struct makes it `!Send`, however the
31-
// pointer is never dereferenced so this is safe.
32-
unsafe impl<T: Send> Send for AssertUnmoved<T> {}
33-
unsafe impl<T: Sync> Sync for AssertUnmoved<T> {}
34-
3529
impl<T> AssertUnmoved<T> {
3630
pub(crate) fn new(inner: T) -> Self {
37-
Self { inner, this_ptr: ptr::null() }
31+
Self { inner, this_addr: 0 }
3832
}
3933

4034
fn poll_with<'a, U>(mut self: Pin<&'a mut Self>, f: impl FnOnce(Pin<&'a mut T>) -> U) -> U {
41-
let cur_this = &*self as *const Self;
42-
if self.this_ptr.is_null() {
35+
let cur_this = &*self as *const Self as usize;
36+
if self.this_addr == 0 {
4337
// First time being polled
44-
*self.as_mut().project().this_ptr = cur_this;
38+
*self.as_mut().project().this_addr = cur_this;
4539
} else {
46-
assert_eq!(self.this_ptr, cur_this, "AssertUnmoved moved between poll calls");
40+
assert_eq!(self.this_addr, cur_this, "AssertUnmoved moved between poll calls");
4741
}
4842
f(self.project().inner)
4943
}
@@ -166,9 +160,9 @@ impl<T> PinnedDrop for AssertUnmoved<T> {
166160
fn drop(self: Pin<&mut Self>) {
167161
// If the thread is panicking then we can't panic again as that will
168162
// cause the process to be aborted.
169-
if !panicking() && !self.this_ptr.is_null() {
170-
let cur_this = &*self as *const Self;
171-
assert_eq!(self.this_ptr, cur_this, "AssertUnmoved moved before drop");
163+
if !panicking() && self.this_addr != 0 {
164+
let cur_this = &*self as *const Self as usize;
165+
assert_eq!(self.this_addr, cur_this, "AssertUnmoved moved before drop");
172166
}
173167
}
174168
}

0 commit comments

Comments
 (0)