From dd7f657535430bb44580e11440f1cdd81f47cd0e Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 6 Nov 2022 10:57:03 -0500 Subject: [PATCH] `Exclusive` nits Tracking issue: #98407 - const fns now have `rustc_const_unstable` - Added `Generator` forwarding impl - Added `FnOnce` & `FnMut` forwarding impls - Made the `From` impl unstably const --- library/core/src/lib.rs | 2 ++ library/core/src/sync/exclusive.rs | 40 ++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 659409557c910..3ac0183be8e07 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -109,6 +109,7 @@ #![feature(const_cmp)] #![feature(const_discriminant)] #![feature(const_eval_select)] +#![feature(const_exclusive_get_mut)] #![feature(const_float_bits_conv)] #![feature(const_float_classify)] #![feature(const_fmt_arguments_new)] @@ -151,6 +152,7 @@ #![feature(const_waker)] #![feature(core_panic)] #![feature(duration_consts_float)] +#![feature(exclusive_wrapper)] #![feature(maybe_uninit_uninit_array)] #![feature(ptr_alignment_type)] #![feature(ptr_metadata)] diff --git a/library/core/src/sync/exclusive.rs b/library/core/src/sync/exclusive.rs index c65c275000ce8..47343f9db4390 100644 --- a/library/core/src/sync/exclusive.rs +++ b/library/core/src/sync/exclusive.rs @@ -2,6 +2,7 @@ use core::fmt; use core::future::Future; +use core::ops::{Generator, GeneratorState}; use core::pin::Pin; use core::task::{Context, Poll}; @@ -91,7 +92,7 @@ unsafe impl Sync for Exclusive {} #[unstable(feature = "exclusive_wrapper", issue = "98407")] impl fmt::Debug for Exclusive { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Exclusive").finish_non_exhaustive() } } @@ -99,6 +100,7 @@ impl fmt::Debug for Exclusive { impl Exclusive { /// Wrap a value in an `Exclusive` #[unstable(feature = "exclusive_wrapper", issue = "98407")] + #[rustc_const_unstable(feature = "exclusive_wrapper", issue = "98407")] #[must_use] #[inline] pub const fn new(t: T) -> Self { @@ -107,6 +109,7 @@ impl Exclusive { /// Unwrap the value contained in the `Exclusive` #[unstable(feature = "exclusive_wrapper", issue = "98407")] + #[rustc_const_unstable(feature = "const_exclusive_into_inner", issue = "98407")] #[must_use] #[inline] pub const fn into_inner(self) -> T { @@ -117,6 +120,7 @@ impl Exclusive { impl Exclusive { /// Get exclusive access to the underlying value. #[unstable(feature = "exclusive_wrapper", issue = "98407")] + #[rustc_const_unstable(feature = "const_exclusive_get_mut", issue = "98407")] #[must_use] #[inline] pub const fn get_mut(&mut self) -> &mut T { @@ -130,6 +134,7 @@ impl Exclusive { /// access to the underlying value, but _pinned_ `Exclusive`s only /// produce _pinned_ access to the underlying value. #[unstable(feature = "exclusive_wrapper", issue = "98407")] + #[rustc_const_unstable(feature = "const_exclusive_get_mut", issue = "98407")] #[must_use] #[inline] pub const fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> { @@ -142,6 +147,7 @@ impl Exclusive { /// a _mutable_ reference to a `T`. This allows you to skip /// building an `Exclusive` with [`Exclusive::new`]. #[unstable(feature = "exclusive_wrapper", issue = "98407")] + #[rustc_const_unstable(feature = "const_exclusive_get_mut", issue = "98407")] #[must_use] #[inline] pub const fn from_mut(r: &'_ mut T) -> &'_ mut Exclusive { @@ -153,6 +159,7 @@ impl Exclusive { /// a _pinned mutable_ reference to a `T`. This allows you to skip /// building an `Exclusive` with [`Exclusive::new`]. #[unstable(feature = "exclusive_wrapper", issue = "98407")] + #[rustc_const_unstable(feature = "const_exclusive_get_mut", issue = "98407")] #[must_use] #[inline] pub const fn from_pin_mut(r: Pin<&'_ mut T>) -> Pin<&'_ mut Exclusive> { @@ -163,18 +170,47 @@ impl Exclusive { } #[unstable(feature = "exclusive_wrapper", issue = "98407")] -impl From for Exclusive { +#[rustc_const_unstable(feature = "core_convert", issue = "88674")] +impl const From for Exclusive { #[inline] fn from(t: T) -> Self { Self::new(t) } } +#[unstable(feature = "exclusive_wrapper", issue = "98407")] +impl> FnOnce for Exclusive { + type Output = F::Output; + + extern "rust-call" fn call_once(self, args: Args) -> Self::Output { + self.into_inner().call_once(args) + } +} + +#[unstable(feature = "exclusive_wrapper", issue = "98407")] +impl> FnMut for Exclusive { + extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output { + self.get_mut().call_mut(args) + } +} + #[unstable(feature = "exclusive_wrapper", issue = "98407")] impl Future for Exclusive { type Output = T::Output; + #[inline] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { self.get_pin_mut().poll(cx) } } + +#[unstable(feature = "generator_trait", issue = "43122")] +impl + ?Sized> Generator for Exclusive { + type Yield = G::Yield; + type Return = G::Return; + + #[inline] + fn resume(self: Pin<&mut Self>, arg: R) -> GeneratorState { + G::resume(self.get_pin_mut(), arg) + } +}