diff --git a/src/futures.rs b/src/futures.rs index 7682816..71f827b 100644 --- a/src/futures.rs +++ b/src/futures.rs @@ -10,7 +10,7 @@ use futures_core::Stream; use crate::SendWrapper; -impl Future for SendWrapper { +impl Future for SendWrapper { type Output = F::Output; /// Polls this [`SendWrapper`] [`Future`]. @@ -28,7 +28,7 @@ impl Future for SendWrapper { } } -impl Stream for SendWrapper { +impl Stream for SendWrapper { type Item = S::Item; /// Polls this [`SendWrapper`] [`Stream`]. diff --git a/src/lib.rs b/src/lib.rs index ff07dec..8c3b5eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,9 +103,9 @@ use std::thread::{self, ThreadId}; /// A wrapper which allows you to move around non-[`Send`]-types between threads, as long as you access the contained /// value only from within the original thread and make sure that it is dropped from within the original thread. -pub struct SendWrapper { - data: ManuallyDrop, +pub struct SendWrapper { thread_id: ThreadId, + data: ManuallyDrop, } impl SendWrapper { @@ -118,11 +118,6 @@ impl SendWrapper { } } - /// Returns `true` if the value can be safely accessed from within the current thread. - pub fn valid(&self) -> bool { - self.thread_id == thread::current().id() - } - /// Takes the value out of the `SendWrapper`. /// /// # Panics @@ -141,6 +136,13 @@ impl SendWrapper { // - We only move out from `self.data` here and in drop, so `self.data` is present unsafe { ManuallyDrop::take(&mut this.data) } } +} + +impl SendWrapper { + /// Returns `true` if the value can be safely accessed from within the current thread. + pub fn valid(&self) -> bool { + self.thread_id == thread::current().id() + } #[track_caller] fn assert_valid_for_deref(&self) { @@ -157,10 +159,10 @@ impl SendWrapper { } } -unsafe impl Send for SendWrapper {} -unsafe impl Sync for SendWrapper {} +unsafe impl Send for SendWrapper {} +unsafe impl Sync for SendWrapper {} -impl Deref for SendWrapper { +impl Deref for SendWrapper { type Target = T; /// Returns a reference to the contained value. @@ -180,7 +182,7 @@ impl Deref for SendWrapper { } } -impl DerefMut for SendWrapper { +impl DerefMut for SendWrapper { /// Returns a mutable reference to the contained value. /// /// # Panics @@ -198,7 +200,7 @@ impl DerefMut for SendWrapper { } } -impl Drop for SendWrapper { +impl Drop for SendWrapper { /// Drops the contained value. /// /// # Panics @@ -232,7 +234,7 @@ impl Drop for SendWrapper { } } -impl fmt::Debug for SendWrapper { +impl fmt::Debug for SendWrapper { /// Formats the value using the given formatter. /// /// # Panics @@ -242,7 +244,7 @@ impl fmt::Debug for SendWrapper { #[track_caller] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SendWrapper") - .field("data", self.deref()) + .field("data", &self.deref()) .field("thread_id", &self.thread_id) .finish() }