Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclusive nits #104057

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down
40 changes: 38 additions & 2 deletions library/core/src/sync/exclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -91,14 +92,15 @@ unsafe impl<T: ?Sized> Sync for Exclusive<T> {}

#[unstable(feature = "exclusive_wrapper", issue = "98407")]
impl<T: ?Sized> fmt::Debug for Exclusive<T> {
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()
}
}

impl<T: Sized> Exclusive<T> {
/// 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 {
Expand All @@ -107,6 +109,7 @@ impl<T: Sized> Exclusive<T> {

/// 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 {
Expand All @@ -117,6 +120,7 @@ impl<T: Sized> Exclusive<T> {
impl<T: ?Sized> Exclusive<T> {
/// 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 {
Expand All @@ -130,6 +134,7 @@ impl<T: ?Sized> Exclusive<T> {
/// 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> {
Expand All @@ -142,6 +147,7 @@ impl<T: ?Sized> Exclusive<T> {
/// 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<T> {
Expand All @@ -153,6 +159,7 @@ impl<T: ?Sized> Exclusive<T> {
/// 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<T>> {
Expand All @@ -163,18 +170,47 @@ impl<T: ?Sized> Exclusive<T> {
}

#[unstable(feature = "exclusive_wrapper", issue = "98407")]
impl<T> From<T> for Exclusive<T> {
#[rustc_const_unstable(feature = "core_convert", issue = "88674")]
impl<T> const From<T> for Exclusive<T> {
#[inline]
fn from(t: T) -> Self {
Self::new(t)
}
}

#[unstable(feature = "exclusive_wrapper", issue = "98407")]
impl<Args, F: FnOnce<Args>> FnOnce<Args> for Exclusive<F> {
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<Args, F: FnMut<Args>> FnMut<Args> for Exclusive<F> {
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<T: Future + ?Sized> Future for Exclusive<T> {
type Output = T::Output;

#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_pin_mut().poll(cx)
}
}

#[unstable(feature = "generator_trait", issue = "43122")]
impl<R, G: Generator<R> + ?Sized> Generator<R> for Exclusive<G> {
type Yield = G::Yield;
type Return = G::Return;

#[inline]
fn resume(self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
G::resume(self.get_pin_mut(), arg)
}
}