Skip to content

Commit e11f0fb

Browse files
PinMut::get_mut_unchecked, Add Sized bound to poll_unpin
1 parent acffc9b commit e11f0fb

File tree

21 files changed

+54
-52
lines changed

21 files changed

+54
-52
lines changed

futures-core/src/future/either.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<A, B> Stream for Either<A, B>
2828

2929
fn poll_next(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<A::Item>> {
3030
unsafe {
31-
match PinMut::get_mut(self) {
31+
match PinMut::get_mut_unchecked(self) {
3232
Either::Left(a) => PinMut::new_unchecked(a).poll_next(cx),
3333
Either::Right(b) => PinMut::new_unchecked(b).poll_next(cx),
3434
}

futures-core/src/future/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ pub use core::future::Future;
1818
pub trait CoreFutureExt: Future {
1919
/// A convenience for calling `Future::poll` on `Unpin` future types.
2020
fn poll_unpin(&mut self, cx: &mut task::Context) -> Poll<Self::Output>
21-
where Self: Unpin
21+
where Self: Unpin + Sized
2222
{
2323
PinMut::new(self).poll(cx)
2424
}
2525
}
2626

2727
impl<T: ?Sized> CoreFutureExt for T where T: Future {}
28-
28+
2929
/// A convenience for futures that return `Result` values that includes
3030
/// a variety of adapters tailored to such futures.
3131
pub trait TryFuture {

futures-core/src/future/option.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@ use core::mem::PinMut;
99
/// Created by the `IntoFuture` implementation for `std::option::Option`.
1010
#[derive(Debug, Clone)]
1111
#[must_use = "futures do nothing unless polled"]
12-
pub struct FutureOption<T> {
13-
inner: Option<T>,
12+
pub struct FutureOption<F> {
13+
option: Option<F>,
14+
}
15+
16+
impl<F> FutureOption<F> {
17+
unsafe_pinned!(option -> Option<F>);
1418
}
1519

1620
impl<F: Future> Future for FutureOption<F> {
1721
type Output = Option<F::Output>;
1822

19-
fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
20-
unsafe {
21-
match &mut PinMut::get_mut(self).inner {
22-
None => Poll::Ready(None),
23-
Some(x) => PinMut::new_unchecked(x).poll(cx).map(Some),
24-
}
23+
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
24+
match self.option().as_pin_mut() {
25+
Some(x) => x.poll(cx).map(Some),
26+
None => Poll::Ready(None),
2527
}
2628
}
2729
}
2830

2931
impl<T> From<Option<T>> for FutureOption<T> {
30-
fn from(o: Option<T>) -> Self {
31-
FutureOption { inner: o }
32+
fn from(option: Option<T>) -> Self {
33+
FutureOption { option }
3234
}
3335
}

futures-core/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ macro_rules! if_std {
2727
macro_rules! pinned_deref {
2828
($e:expr) => (
2929
::core::mem::PinMut::new_unchecked(
30-
&mut **::core::mem::PinMut::get_mut($e.reborrow())
30+
&mut **::core::mem::PinMut::get_mut_unchecked($e.reborrow())
3131
)
3232
)
3333
}
@@ -36,7 +36,7 @@ macro_rules! pinned_deref {
3636
macro_rules! pinned_field {
3737
($e:expr, $f:tt) => (
3838
::core::mem::PinMut::new_unchecked(
39-
&mut ::core::mem::PinMut::get_mut($e.reborrow()).$f
39+
&mut ::core::mem::PinMut::get_mut_unchecked($e.reborrow()).$f
4040
)
4141
)
4242
}
@@ -57,7 +57,7 @@ macro_rules! unsafe_unpinned {
5757
($f:tt -> $t:ty) => (
5858
fn $f<'a>(self: &'a mut PinMut<Self>) -> &'a mut $t {
5959
unsafe {
60-
&mut ::core::mem::PinMut::get_mut(self.reborrow()).$f
60+
&mut ::core::mem::PinMut::get_mut_unchecked(self.reborrow()).$f
6161
}
6262
}
6363
)

futures-core/src/stream/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub trait Stream {
4949

5050
/// A convenience for calling `Stream::poll_next` on `Unpin` stream types.
5151
fn poll_next_unpin(&mut self, cx: &mut task::Context) -> Poll<Option<Self::Item>>
52-
where Self: Unpin
52+
where Self: Unpin + Sized
5353
{
5454
PinMut::new(self).poll_next(cx)
5555
}
@@ -79,7 +79,7 @@ if_std! {
7979
type Item = S::Item;
8080

8181
fn poll_next(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
82-
(**self).poll_next_unpin(cx)
82+
PinMut::new(&mut **self).poll_next(cx)
8383
}
8484
}
8585

futures-sink/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ if_std! {
184184

185185
fn start_send(self: PinMut<Self>, item: Self::SinkItem) -> Result<(), Self::SinkError> {
186186
// TODO: impl<T> Unpin for Vec<T> {}
187-
unsafe { PinMut::get_mut(self) }.push(item);
187+
unsafe { PinMut::get_mut_unchecked(self) }.push(item);
188188
Ok(())
189189
}
190190

@@ -207,7 +207,7 @@ if_std! {
207207

208208
fn start_send(self: PinMut<Self>, item: Self::SinkItem) -> Result<(), Self::SinkError> {
209209
// TODO: impl<T> Unpin for Vec<T> {}
210-
unsafe { PinMut::get_mut(self) }.push_back(item);
210+
unsafe { PinMut::get_mut_unchecked(self) }.push_back(item);
211211
Ok(())
212212
}
213213

@@ -257,7 +257,7 @@ impl<A, B> Sink for Either<A, B>
257257

258258
fn poll_ready(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Result<(), Self::SinkError>> {
259259
unsafe {
260-
match PinMut::get_mut(self) {
260+
match PinMut::get_mut_unchecked(self) {
261261
Either::Left(x) => PinMut::new_unchecked(x).poll_ready(cx),
262262
Either::Right(x) => PinMut::new_unchecked(x).poll_ready(cx),
263263
}
@@ -266,7 +266,7 @@ impl<A, B> Sink for Either<A, B>
266266

267267
fn start_send(self: PinMut<Self>, item: Self::SinkItem) -> Result<(), Self::SinkError> {
268268
unsafe {
269-
match PinMut::get_mut(self) {
269+
match PinMut::get_mut_unchecked(self) {
270270
Either::Left(x) => PinMut::new_unchecked(x).start_send(item),
271271
Either::Right(x) => PinMut::new_unchecked(x).start_send(item),
272272
}
@@ -275,7 +275,7 @@ impl<A, B> Sink for Either<A, B>
275275

276276
fn poll_flush(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Result<(), Self::SinkError>> {
277277
unsafe {
278-
match PinMut::get_mut(self) {
278+
match PinMut::get_mut_unchecked(self) {
279279
Either::Left(x) => PinMut::new_unchecked(x).poll_flush(cx),
280280
Either::Right(x) => PinMut::new_unchecked(x).poll_flush(cx),
281281
}
@@ -284,7 +284,7 @@ impl<A, B> Sink for Either<A, B>
284284

285285
fn poll_close(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Result<(), Self::SinkError>> {
286286
unsafe {
287-
match PinMut::get_mut(self) {
287+
match PinMut::get_mut_unchecked(self) {
288288
Either::Left(x) => PinMut::new_unchecked(x).poll_close(cx),
289289
Either::Right(x) => PinMut::new_unchecked(x).poll_close(cx),
290290
}

futures-util/src/future/chain.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl<Fut1, Fut2, Data> Chain<Fut1, Fut2, Data>
2424
let mut f = Some(f);
2525

2626
loop {
27-
// safe to `get_mut` here because we don't move out
28-
let fut2 = match unsafe { PinMut::get_mut(self.reborrow()) } {
27+
// Safe to use `get_mut_unchecked` here because we don't move out
28+
let fut2 = match unsafe { PinMut::get_mut_unchecked(self.reborrow()) } {
2929
Chain::First(fut1, data) => {
3030
// safe to create a new `PinMut` because `fut1` will never move
3131
// before it's dropped.
@@ -37,17 +37,17 @@ impl<Fut1, Fut2, Data> Chain<Fut1, Fut2, Data>
3737
}
3838
}
3939
Chain::Second(fut2) => {
40-
// safe to create a new `PinMut` because `fut2` will never move
40+
// Safe to create a new `PinMut` because `fut2` will never move
4141
// before it's dropped; once we're in `Chain::Second` we stay
4242
// there forever.
4343
return unsafe { PinMut::new_unchecked(fut2) }.poll(cx)
4444
}
4545
};
4646

47-
// safe because we're using the `&mut` to do an assignment, not for moving out
47+
// Safe because we're using the `&mut` to do an assignment, not for moving out
4848
unsafe {
49-
// note: it's safe to move the `fut2` here because we haven't yet polled it
50-
*PinMut::get_mut(self.reborrow()) = Chain::Second(fut2);
49+
// note: It's safe to move the `fut2` here because we haven't yet polled it
50+
*PinMut::get_mut_unchecked(self.reborrow()) = Chain::Second(fut2);
5151
}
5252
}
5353
}

futures-util/src/future/flatten_stream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<F> Stream for FlattenStream<F>
4747
fn poll_next(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
4848
loop {
4949
// safety: data is never moved via the resulting &mut reference
50-
let stream = match &mut unsafe { PinMut::get_mut(self.reborrow()) }.state {
50+
let stream = match &mut unsafe { PinMut::get_mut_unchecked(self.reborrow()) }.state {
5151
State::Future(f) => {
5252
// safety: the future we're re-pinning here will never be moved;
5353
// it will just be polled, then dropped in place
@@ -74,7 +74,7 @@ impl<F> Stream for FlattenStream<F>
7474
unsafe {
7575
// safety: we use the &mut only for an assignment, which causes
7676
// only an in-place drop
77-
PinMut::get_mut(self.reborrow()).state = State::Stream(stream);
77+
PinMut::get_mut_unchecked(self.reborrow()).state = State::Stream(stream);
7878
}
7979
}
8080
}

futures-util/src/future/with_executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<F, E> Future for WithExecutor<F, E>
3232
type Output = F::Output;
3333

3434
fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<F::Output> {
35-
let this = unsafe { PinMut::get_mut(self) };
35+
let this = unsafe { PinMut::get_mut_unchecked(self) };
3636
let fut = unsafe { PinMut::new_unchecked(&mut this.future) };
3737
let exec = &mut this.executor;
3838
fut.poll(&mut cx.with_executor(exec))

futures-util/src/io/split.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn lock_and_then<T, U, E, F>(lock: &BiLock<T>, cx: &mut task::Context, f: F) ->
2424
match lock.poll_lock(cx) {
2525
// Safety: the value behind the bilock used by `ReadHalf` and `WriteHalf` is never exposed
2626
// as a `PinMut` anywhere other than here as a way to get to `&mut`.
27-
Poll::Ready(mut l) => f(unsafe { PinMut::get_mut(l.as_pin_mut()) }, cx),
27+
Poll::Ready(mut l) => f(unsafe { PinMut::get_mut_unchecked(l.as_pin_mut()) }, cx),
2828
Poll::Pending => Poll::Pending,
2929
}
3030
}

0 commit comments

Comments
 (0)