Skip to content

Commit 1d4dbf4

Browse files
committed
Auto merge of #51442 - tinaun:more-future-impls, r=cramertj
[futures] add a few blanket impls to std these were defined in the futures crate, but with the core definitions moving to std these would need to move too.
2 parents 0b7c9e7 + fb507ca commit 1d4dbf4

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

src/liballoc/boxed.rs

+18
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,24 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinBox<U>> for PinBox<T> {}
914914
#[unstable(feature = "pin", issue = "49150")]
915915
impl<T: ?Sized> Unpin for PinBox<T> {}
916916

917+
#[unstable(feature = "futures_api", issue = "50547")]
918+
impl<'a, F: ?Sized + Future + Unpin> Future for Box<F> {
919+
type Output = F::Output;
920+
921+
fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
922+
PinMut::new(&mut **self).poll(cx)
923+
}
924+
}
925+
926+
#[unstable(feature = "futures_api", issue = "50547")]
927+
impl<'a, F: ?Sized + Future> Future for PinBox<F> {
928+
type Output = F::Output;
929+
930+
fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
931+
self.as_pin_mut().poll(cx)
932+
}
933+
}
934+
917935
#[unstable(feature = "futures_api", issue = "50547")]
918936
unsafe impl<F: Future<Output = ()> + Send + 'static> UnsafePoll for PinBox<F> {
919937
fn into_raw(self) -> *mut () {

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#![cfg_attr(test, feature(rand, test))]
8181
#![feature(allocator_api)]
8282
#![feature(allow_internal_unstable)]
83+
#![feature(arbitrary_self_types)]
8384
#![feature(ascii_ctype)]
8485
#![feature(box_into_raw_non_null)]
8586
#![feature(box_patterns)]

src/libcore/future.rs

+17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! Asynchronous values.
1616
1717
use mem::PinMut;
18+
use marker::Unpin;
1819
use task::{self, Poll};
1920

2021
/// A future represents an asychronous computation.
@@ -91,3 +92,19 @@ pub trait Future {
9192
/// about the behavior of `poll` after a future has completed.
9293
fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output>;
9394
}
95+
96+
impl<'a, F: ?Sized + Future + Unpin> Future for &'a mut F {
97+
type Output = F::Output;
98+
99+
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
100+
F::poll(PinMut::new(&mut **self), cx)
101+
}
102+
}
103+
104+
impl<'a, F: ?Sized + Future> Future for PinMut<'a, F> {
105+
type Output = F::Output;
106+
107+
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
108+
F::poll((*self).reborrow(), cx)
109+
}
110+
}

src/libcore/task.rs

+55
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,61 @@ pub enum Poll<T> {
3232
Pending,
3333
}
3434

35+
impl<T> Poll<T> {
36+
/// Change the ready value of this `Poll` with the closure provided
37+
pub fn map<U, F>(self, f: F) -> Poll<U>
38+
where F: FnOnce(T) -> U
39+
{
40+
match self {
41+
Poll::Ready(t) => Poll::Ready(f(t)),
42+
Poll::Pending => Poll::Pending,
43+
}
44+
}
45+
46+
/// Returns whether this is `Poll::Ready`
47+
pub fn is_ready(&self) -> bool {
48+
match *self {
49+
Poll::Ready(_) => true,
50+
Poll::Pending => false,
51+
}
52+
}
53+
54+
/// Returns whether this is `Poll::Pending`
55+
pub fn is_pending(&self) -> bool {
56+
!self.is_ready()
57+
}
58+
}
59+
60+
impl<T, E> Poll<Result<T, E>> {
61+
/// Change the success value of this `Poll` with the closure provided
62+
pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
63+
where F: FnOnce(T) -> U
64+
{
65+
match self {
66+
Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
67+
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
68+
Poll::Pending => Poll::Pending,
69+
}
70+
}
71+
72+
/// Change the error value of this `Poll` with the closure provided
73+
pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
74+
where F: FnOnce(E) -> U
75+
{
76+
match self {
77+
Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
78+
Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
79+
Poll::Pending => Poll::Pending,
80+
}
81+
}
82+
}
83+
84+
impl<T> From<T> for Poll<T> {
85+
fn from(t: T) -> Poll<T> {
86+
Poll::Ready(t)
87+
}
88+
}
89+
3590
/// A `Waker` is a handle for waking up a task by notifying its executor that it
3691
/// is ready to be run.
3792
///

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
#![feature(allow_internal_unsafe)]
240240
#![feature(allow_internal_unstable)]
241241
#![feature(align_offset)]
242+
#![feature(arbitrary_self_types)]
242243
#![feature(array_error_internals)]
243244
#![feature(ascii_ctype)]
244245
#![feature(asm)]

src/libstd/panic.rs

+18
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
use any::Any;
1616
use cell::UnsafeCell;
1717
use fmt;
18+
use future::Future;
19+
use mem::PinMut;
1820
use ops::{Deref, DerefMut};
1921
use panicking;
2022
use ptr::{Unique, NonNull};
2123
use rc::Rc;
2224
use sync::{Arc, Mutex, RwLock, atomic};
25+
use task::{self, Poll};
2326
use thread::Result;
2427

2528
#[stable(feature = "panic_hooks", since = "1.10.0")]
@@ -315,6 +318,21 @@ impl<T: fmt::Debug> fmt::Debug for AssertUnwindSafe<T> {
315318
}
316319
}
317320

321+
#[unstable(feature = "futures_api", issue = "50547")]
322+
impl<'a, F: Future> Future for AssertUnwindSafe<F> {
323+
type Output = F::Output;
324+
325+
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
326+
unsafe {
327+
let pinned_field = PinMut::new_unchecked(
328+
&mut PinMut::get_mut(self.reborrow()).0
329+
);
330+
331+
pinned_field.poll(cx)
332+
}
333+
}
334+
}
335+
318336
/// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
319337
///
320338
/// This function will return `Ok` with the closure's result if the closure

0 commit comments

Comments
 (0)