Skip to content

Commit a33f913

Browse files
Implement Receiver for Exclusive
In the hope of being able to implement `DerefMut` in the future. Changes `Exclusive`'s methods to associated functions.
1 parent db3fd47 commit a33f913

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

library/core/src/sync/exclusive.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use core::fmt;
44
use core::future::Future;
55
use core::marker::Tuple;
6-
use core::ops::{Coroutine, CoroutineState};
6+
use core::ops::{Coroutine, CoroutineState, Receiver};
77
use core::pin::Pin;
88
use core::task::{Context, Poll};
99

@@ -22,7 +22,9 @@ use core::task::{Context, Poll};
2222
/// Rust compiler that something is `Sync` in practice.
2323
///
2424
/// ## Examples
25-
/// Using a non-`Sync` future prevents the wrapping struct from being `Sync`
25+
///
26+
/// Using a non-`Sync` future prevents the wrapping struct from being `Sync`:
27+
///
2628
/// ```compile_fail
2729
/// use core::cell::Cell;
2830
///
@@ -44,6 +46,7 @@ use core::task::{Context, Poll};
4446
///
4547
/// `Exclusive` ensures the struct is `Sync` without stripping the future of its
4648
/// functionality.
49+
///
4750
/// ```
4851
/// #![feature(exclusive_wrapper)]
4952
/// use core::cell::Cell;
@@ -109,8 +112,8 @@ impl<T: Sized> Exclusive<T> {
109112
#[rustc_const_unstable(feature = "exclusive_wrapper", issue = "98407")]
110113
#[must_use]
111114
#[inline]
112-
pub const fn into_inner(self) -> T {
113-
self.inner
115+
pub const fn into_inner(exclusive: Self) -> T {
116+
exclusive.inner
114117
}
115118
}
116119

@@ -119,8 +122,8 @@ impl<T: ?Sized> Exclusive<T> {
119122
#[unstable(feature = "exclusive_wrapper", issue = "98407")]
120123
#[must_use]
121124
#[inline]
122-
pub const fn get_mut(&mut self) -> &mut T {
123-
&mut self.inner
125+
pub const fn get_mut(exclusive: &mut Self) -> &mut T {
126+
&mut exclusive.inner
124127
}
125128

126129
/// Gets pinned exclusive access to the underlying value.
@@ -132,10 +135,10 @@ impl<T: ?Sized> Exclusive<T> {
132135
#[unstable(feature = "exclusive_wrapper", issue = "98407")]
133136
#[must_use]
134137
#[inline]
135-
pub const fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
138+
pub const fn get_pin_mut(exclusive: Pin<&mut Self>) -> Pin<&mut T> {
136139
// SAFETY: `Exclusive` can only produce `&mut T` if itself is unpinned
137140
// `Pin::map_unchecked_mut` is not const, so we do this conversion manually
138-
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }
141+
unsafe { Pin::new_unchecked(&mut exclusive.get_unchecked_mut().inner) }
139142
}
140143

141144
/// Build a _mutable_ reference to an `Exclusive<T>` from
@@ -179,7 +182,7 @@ where
179182
type Output = F::Output;
180183

181184
extern "rust-call" fn call_once(self, args: Args) -> Self::Output {
182-
self.into_inner().call_once(args)
185+
Self::into_inner(self).call_once(args)
183186
}
184187
}
185188

@@ -190,7 +193,7 @@ where
190193
Args: Tuple,
191194
{
192195
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output {
193-
self.get_mut().call_mut(args)
196+
Self::get_mut(self).call_mut(args)
194197
}
195198
}
196199

@@ -203,7 +206,7 @@ where
203206

204207
#[inline]
205208
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
206-
self.get_pin_mut().poll(cx)
209+
Self::get_pin_mut(self).poll(cx)
207210
}
208211
}
209212

@@ -217,6 +220,15 @@ where
217220

218221
#[inline]
219222
fn resume(self: Pin<&mut Self>, arg: R) -> CoroutineState<Self::Yield, Self::Return> {
220-
G::resume(self.get_pin_mut(), arg)
223+
G::resume(Self::get_pin_mut(self), arg)
221224
}
222225
}
226+
227+
// FIXME: implement `DerefMut` when this becomes possible
228+
#[unstable(feature = "arbitrary_self_types", issue = "44874")]
229+
impl<T> Receiver for Exclusive<T>
230+
where
231+
T: ?Sized,
232+
{
233+
type Target = T;
234+
}

0 commit comments

Comments
 (0)