From 6309a9a03fdabe10f238e06bf6a7874e95a84314 Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Mon, 15 Aug 2022 07:42:17 +0900 Subject: [PATCH] Update safety notice. --- packages/yew/src/platform/pinned/mpsc.rs | 68 ++++++++++++++++++--- packages/yew/src/platform/pinned/oneshot.rs | 32 ++++++++-- 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/packages/yew/src/platform/pinned/mpsc.rs b/packages/yew/src/platform/pinned/mpsc.rs index b72a9a26cf1..cac9fae4b43 100644 --- a/packages/yew/src/platform/pinned/mpsc.rs +++ b/packages/yew/src/platform/pinned/mpsc.rs @@ -64,8 +64,14 @@ impl UnboundedReceiver { /// - `Ok(None)` if the channel has become closed. /// - `Err(TryRecvError)` if the channel is not closed and the channel is empty. pub fn try_next(&self) -> std::result::Result, TryRecvError> { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; match (inner.items.pop_front(), inner.closed) { @@ -85,8 +91,14 @@ impl Stream for UnboundedReceiver { self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> std::task::Poll> { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; match (inner.items.pop_front(), inner.closed) { @@ -102,8 +114,14 @@ impl Stream for UnboundedReceiver { impl FusedStream for UnboundedReceiver { fn is_terminated(&self) -> bool { - // SAFETY: This function is not used by any other functions and hence uniquely owns the - // reference. + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the + // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &*self.inner.get() }; inner.items.is_empty() && inner.closed } @@ -111,8 +129,14 @@ impl FusedStream for UnboundedReceiver { impl Drop for UnboundedReceiver { fn drop(&mut self) { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; inner.close(); } @@ -127,8 +151,14 @@ pub struct UnboundedSender { impl UnboundedSender { /// Sends a value to the unbounded receiver. pub fn send_now(&self, item: T) -> Result<(), SendError> { - // SAFETY: This function is not used by any function that have already acquired a mutable + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any function that have already acquired a mutable // reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; if inner.closed { @@ -146,8 +176,14 @@ impl UnboundedSender { /// Closes the channel. pub fn close_now(&self) { - // SAFETY: This function is not used by any other functions that have acquired a mutable - // reference and hence uniquely owns the mutable reference. + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any function that have already acquired a mutable + // reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; inner.close(); } @@ -159,8 +195,14 @@ impl Clone for UnboundedSender { inner: self.inner.clone(), }; - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; inner.sender_ctr += 1; @@ -170,8 +212,14 @@ impl Clone for UnboundedSender { impl Drop for UnboundedSender { fn drop(&mut self) { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; let sender_ctr = { diff --git a/packages/yew/src/platform/pinned/oneshot.rs b/packages/yew/src/platform/pinned/oneshot.rs index b4eab307068..2d57e128c61 100644 --- a/packages/yew/src/platform/pinned/oneshot.rs +++ b/packages/yew/src/platform/pinned/oneshot.rs @@ -32,8 +32,14 @@ impl Future for Receiver { type Output = Result; fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; // Implementation Note: @@ -58,8 +64,14 @@ impl Future for Receiver { impl Drop for Receiver { fn drop(&mut self) { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; inner.closed = true; } @@ -74,8 +86,14 @@ pub struct Sender { impl Sender { /// Send an item to the other side of the channel, consumes the sender. pub fn send(self, item: T) -> Result<(), T> { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; if inner.closed { @@ -94,8 +112,14 @@ impl Sender { impl Drop for Sender { fn drop(&mut self) { - // SAFETY: This function is not used by any other functions and hence uniquely owns the + // SAFETY: + // + // We can acquire a mutable reference without checking as: + // + // - This type is !Send. + // - This function is not used by any other functions and hence uniquely owns the // mutable reference. + // - The mutable reference is dropped at the end of this function. let inner = unsafe { &mut *self.inner.get() }; inner.closed = true;