Skip to content

Commit

Permalink
Update safety notice.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Aug 14, 2022
1 parent a8ee1c0 commit 6309a9a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
68 changes: 58 additions & 10 deletions packages/yew/src/platform/pinned/mpsc.rs
Expand Up @@ -64,8 +64,14 @@ impl<T> UnboundedReceiver<T> {
/// - `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<Option<T>, 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) {
Expand All @@ -85,8 +91,14 @@ impl<T> Stream for UnboundedReceiver<T> {
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
// 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) {
Expand All @@ -102,17 +114,29 @@ impl<T> Stream for UnboundedReceiver<T> {

impl<T> FusedStream for UnboundedReceiver<T> {
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
}
}

impl<T> Drop for UnboundedReceiver<T> {
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();
}
Expand All @@ -127,8 +151,14 @@ pub struct UnboundedSender<T> {
impl<T> UnboundedSender<T> {
/// Sends a value to the unbounded receiver.
pub fn send_now(&self, item: T) -> Result<(), SendError<T>> {
// 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 {
Expand All @@ -146,8 +176,14 @@ impl<T> UnboundedSender<T> {

/// 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();
}
Expand All @@ -159,8 +195,14 @@ impl<T> Clone for UnboundedSender<T> {
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;

Expand All @@ -170,8 +212,14 @@ impl<T> Clone for UnboundedSender<T> {

impl<T> Drop for UnboundedSender<T> {
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 = {
Expand Down
32 changes: 28 additions & 4 deletions packages/yew/src/platform/pinned/oneshot.rs
Expand Up @@ -32,8 +32,14 @@ impl<T> Future for Receiver<T> {
type Output = Result<T, RecvError>;

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
// 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:
Expand All @@ -58,8 +64,14 @@ impl<T> Future for Receiver<T> {

impl<T> Drop for Receiver<T> {
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;
}
Expand All @@ -74,8 +86,14 @@ pub struct Sender<T> {
impl<T> Sender<T> {
/// 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 {
Expand All @@ -94,8 +112,14 @@ impl<T> Sender<T> {

impl<T> Drop for Sender<T> {
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;
Expand Down

0 comments on commit 6309a9a

Please sign in to comment.