From a33f913fbbc8c368c801c70a907e46a19fd995b2 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 29 Aug 2025 15:17:06 -0400 Subject: [PATCH 1/2] Implement `Receiver` for `Exclusive` In the hope of being able to implement `DerefMut` in the future. Changes `Exclusive`'s methods to associated functions. --- library/core/src/sync/exclusive.rs | 36 ++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/library/core/src/sync/exclusive.rs b/library/core/src/sync/exclusive.rs index 340b0b79e40a3..a5f1695378c55 100644 --- a/library/core/src/sync/exclusive.rs +++ b/library/core/src/sync/exclusive.rs @@ -3,7 +3,7 @@ use core::fmt; use core::future::Future; use core::marker::Tuple; -use core::ops::{Coroutine, CoroutineState}; +use core::ops::{Coroutine, CoroutineState, Receiver}; use core::pin::Pin; use core::task::{Context, Poll}; @@ -22,7 +22,9 @@ use core::task::{Context, Poll}; /// Rust compiler that something is `Sync` in practice. /// /// ## Examples -/// Using a non-`Sync` future prevents the wrapping struct from being `Sync` +/// +/// Using a non-`Sync` future prevents the wrapping struct from being `Sync`: +/// /// ```compile_fail /// use core::cell::Cell; /// @@ -44,6 +46,7 @@ use core::task::{Context, Poll}; /// /// `Exclusive` ensures the struct is `Sync` without stripping the future of its /// functionality. +/// /// ``` /// #![feature(exclusive_wrapper)] /// use core::cell::Cell; @@ -109,8 +112,8 @@ impl Exclusive { #[rustc_const_unstable(feature = "exclusive_wrapper", issue = "98407")] #[must_use] #[inline] - pub const fn into_inner(self) -> T { - self.inner + pub const fn into_inner(exclusive: Self) -> T { + exclusive.inner } } @@ -119,8 +122,8 @@ impl Exclusive { #[unstable(feature = "exclusive_wrapper", issue = "98407")] #[must_use] #[inline] - pub const fn get_mut(&mut self) -> &mut T { - &mut self.inner + pub const fn get_mut(exclusive: &mut Self) -> &mut T { + &mut exclusive.inner } /// Gets pinned exclusive access to the underlying value. @@ -132,10 +135,10 @@ impl Exclusive { #[unstable(feature = "exclusive_wrapper", issue = "98407")] #[must_use] #[inline] - pub const fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> { + pub const fn get_pin_mut(exclusive: Pin<&mut Self>) -> Pin<&mut T> { // SAFETY: `Exclusive` can only produce `&mut T` if itself is unpinned // `Pin::map_unchecked_mut` is not const, so we do this conversion manually - unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) } + unsafe { Pin::new_unchecked(&mut exclusive.get_unchecked_mut().inner) } } /// Build a _mutable_ reference to an `Exclusive` from @@ -179,7 +182,7 @@ where type Output = F::Output; extern "rust-call" fn call_once(self, args: Args) -> Self::Output { - self.into_inner().call_once(args) + Self::into_inner(self).call_once(args) } } @@ -190,7 +193,7 @@ where Args: Tuple, { extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output { - self.get_mut().call_mut(args) + Self::get_mut(self).call_mut(args) } } @@ -203,7 +206,7 @@ where #[inline] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - self.get_pin_mut().poll(cx) + Self::get_pin_mut(self).poll(cx) } } @@ -217,6 +220,15 @@ where #[inline] fn resume(self: Pin<&mut Self>, arg: R) -> CoroutineState { - G::resume(self.get_pin_mut(), arg) + G::resume(Self::get_pin_mut(self), arg) } } + +// FIXME: implement `DerefMut` when this becomes possible +#[unstable(feature = "arbitrary_self_types", issue = "44874")] +impl Receiver for Exclusive +where + T: ?Sized, +{ + type Target = T; +} From eb40e70abdc302768ba8bb353254abc116d20607 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Wed, 3 Sep 2025 09:35:59 -0400 Subject: [PATCH 2/2] Expand comment --- library/core/src/sync/exclusive.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/core/src/sync/exclusive.rs b/library/core/src/sync/exclusive.rs index a5f1695378c55..0aa0d23303d4c 100644 --- a/library/core/src/sync/exclusive.rs +++ b/library/core/src/sync/exclusive.rs @@ -224,7 +224,10 @@ where } } -// FIXME: implement `DerefMut` when this becomes possible +// FIXME: implement `DerefMut` when this becomes possible. +// Currently, this can't be done because `DerefMut` has `Deref` +// as a supertrait. But RFC 3437 would allow changing that: +// https://github.com/rust-lang/rust/issues/98407#issuecomment-3238181337 #[unstable(feature = "arbitrary_self_types", issue = "44874")] impl Receiver for Exclusive where