From 5051335313576ea32d172a68beda3cdfcb2823de Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 26 Oct 2023 21:52:55 +0900 Subject: [PATCH] Remove unsafe code from AssertUnmoved --- futures-test/src/assert_unmoved.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/futures-test/src/assert_unmoved.rs b/futures-test/src/assert_unmoved.rs index 95d9a095f2..baeaeb59ab 100644 --- a/futures-test/src/assert_unmoved.rs +++ b/futures-test/src/assert_unmoved.rs @@ -7,7 +7,6 @@ use futures_io::{ use futures_sink::Sink; use pin_project::{pin_project, pinned_drop}; use std::pin::Pin; -use std::ptr; use std::thread::panicking; /// Combinator that asserts that the underlying type is not moved after being polled. @@ -24,26 +23,21 @@ use std::thread::panicking; pub struct AssertUnmoved { #[pin] inner: T, - this_ptr: *const Self, + this_addr: usize, } -// Safety: having a raw pointer in a struct makes it `!Send`, however the -// pointer is never dereferenced so this is safe. -unsafe impl Send for AssertUnmoved {} -unsafe impl Sync for AssertUnmoved {} - impl AssertUnmoved { pub(crate) fn new(inner: T) -> Self { - Self { inner, this_ptr: ptr::null() } + Self { inner, this_addr: 0 } } fn poll_with<'a, U>(mut self: Pin<&'a mut Self>, f: impl FnOnce(Pin<&'a mut T>) -> U) -> U { - let cur_this = &*self as *const Self; - if self.this_ptr.is_null() { + let cur_this = &*self as *const Self as usize; + if self.this_addr == 0 { // First time being polled - *self.as_mut().project().this_ptr = cur_this; + *self.as_mut().project().this_addr = cur_this; } else { - assert_eq!(self.this_ptr, cur_this, "AssertUnmoved moved between poll calls"); + assert_eq!(self.this_addr, cur_this, "AssertUnmoved moved between poll calls"); } f(self.project().inner) } @@ -166,9 +160,9 @@ impl PinnedDrop for AssertUnmoved { fn drop(self: Pin<&mut Self>) { // If the thread is panicking then we can't panic again as that will // cause the process to be aborted. - if !panicking() && !self.this_ptr.is_null() { - let cur_this = &*self as *const Self; - assert_eq!(self.this_ptr, cur_this, "AssertUnmoved moved before drop"); + if !panicking() && self.this_addr != 0 { + let cur_this = &*self as *const Self as usize; + assert_eq!(self.this_addr, cur_this, "AssertUnmoved moved before drop"); } } }