diff options
| author | Josef Reinhard Brandl <mail@josefbrandl.de> | 2018-06-30 21:16:44 +0200 |
|---|---|---|
| committer | Josef Reinhard Brandl <mail@josefbrandl.de> | 2018-07-02 13:59:40 +0200 |
| commit | d8bf2223672973f9d86f6c173793bcfce7890cd8 (patch) | |
| tree | 641557daeb359a6b32a1b2eca7af0bded0b80b96 | |
| parent | 9f70e7fe3c4126bf8390a78e4740ade3261ac4df (diff) | |
| download | rust-d8bf2223672973f9d86f6c173793bcfce7890cd8.tar.gz rust-d8bf2223672973f9d86f6c173793bcfce7890cd8.zip | |
Add lifetime to `FutureObj`
| -rw-r--r-- | src/liballoc/boxed.rs | 17 | ||||
| -rw-r--r-- | src/libcore/future/future.rs (renamed from src/libcore/future.rs) | 2 | ||||
| -rw-r--r-- | src/libcore/future/future_obj.rs (renamed from src/libcore/task/future_obj.rs) | 38 | ||||
| -rw-r--r-- | src/libcore/future/mod.rs | 21 | ||||
| -rw-r--r-- | src/libcore/task/executor.rs | 8 | ||||
| -rw-r--r-- | src/libcore/task/mod.rs | 3 | ||||
| -rw-r--r-- | src/test/run-pass/async-await.rs | 5 | ||||
| -rw-r--r-- | src/test/run-pass/futures-api.rs | 5 |
8 files changed, 60 insertions, 39 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 918ad657be9..5984a992afc 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -66,7 +66,8 @@ use core::marker::{Unpin, Unsize}; use core::mem::{self, PinMut}; use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState}; use core::ptr::{self, NonNull, Unique}; -use core::task::{Context, Poll, UnsafeFutureObj, FutureObj, LocalFutureObj}; +use core::future::{FutureObj, LocalFutureObj, UnsafeFutureObj}; +use core::task::{Context, Poll}; use core::convert::From; use raw_vec::RawVec; @@ -915,7 +916,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinBox<U>> for PinBox<T> {} impl<T: ?Sized> Unpin for PinBox<T> {} #[unstable(feature = "futures_api", issue = "50547")] -impl<'a, F: ?Sized + Future + Unpin> Future for Box<F> { +impl<F: ?Sized + Future + Unpin> Future for Box<F> { type Output = F::Output; fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> { @@ -924,7 +925,7 @@ impl<'a, F: ?Sized + Future + Unpin> Future for Box<F> { } #[unstable(feature = "futures_api", issue = "50547")] -impl<'a, F: ?Sized + Future> Future for PinBox<F> { +impl<F: ?Sized + Future> Future for PinBox<F> { type Output = F::Output; fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> { @@ -933,7 +934,7 @@ impl<'a, F: ?Sized + Future> Future for PinBox<F> { } #[unstable(feature = "futures_api", issue = "50547")] -unsafe impl<T, F: Future<Output = T> + 'static> UnsafeFutureObj<T> for PinBox<F> { +unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinBox<F> { fn into_raw(self) -> *mut () { PinBox::into_raw(self) as *mut () } @@ -950,28 +951,28 @@ unsafe impl<T, F: Future<Output = T> + 'static> UnsafeFutureObj<T> for PinBox<F> } #[unstable(feature = "futures_api", issue = "50547")] -impl<F: Future<Output = ()> + Send + 'static> From<PinBox<F>> for FutureObj<()> { +impl<'a, F: Future<Output = ()> + Send + 'a> From<PinBox<F>> for FutureObj<'a, ()> { fn from(boxed: PinBox<F>) -> Self { FutureObj::new(boxed) } } #[unstable(feature = "futures_api", issue = "50547")] -impl<F: Future<Output = ()> + Send + 'static> From<Box<F>> for FutureObj<()> { +impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()> { fn from(boxed: Box<F>) -> Self { FutureObj::new(PinBox::from(boxed)) } } #[unstable(feature = "futures_api", issue = "50547")] -impl<F: Future<Output = ()> + 'static> From<PinBox<F>> for LocalFutureObj<()> { +impl<'a, F: Future<Output = ()> + 'a> From<PinBox<F>> for LocalFutureObj<'a, ()> { fn from(boxed: PinBox<F>) -> Self { LocalFutureObj::new(boxed) } } #[unstable(feature = "futures_api", issue = "50547")] -impl<F: Future<Output = ()> + 'static> From<Box<F>> for LocalFutureObj<()> { +impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> { fn from(boxed: Box<F>) -> Self { LocalFutureObj::new(PinBox::from(boxed)) } diff --git a/src/libcore/future.rs b/src/libcore/future/future.rs index 153cd6c0724..10b4ca9b0b2 100644 --- a/src/libcore/future.rs +++ b/src/libcore/future/future.rs @@ -12,8 +12,6 @@ reason = "futures in libcore are unstable", issue = "50547")] -//! Asynchronous values. - use mem::PinMut; use marker::Unpin; use task::{self, Poll}; diff --git a/src/libcore/task/future_obj.rs b/src/libcore/future/future_obj.rs index 3ed3bd51cf6..c60b8b97d34 100644 --- a/src/libcore/task/future_obj.rs +++ b/src/libcore/future/future_obj.rs @@ -21,22 +21,24 @@ use task::{Context, Poll}; /// A custom trait object for polling futures, roughly akin to /// `Box<dyn Future<Output = T>>`. /// Contrary to `FutureObj`, `LocalFutureObj` does not have a `Send` bound. -pub struct LocalFutureObj<T> { +pub struct LocalFutureObj<'a, T> { ptr: *mut (), poll_fn: unsafe fn(*mut (), &mut Context) -> Poll<T>, drop_fn: unsafe fn(*mut ()), - _marker: PhantomData<T>, + _marker1: PhantomData<T>, + _marker2: PhantomData<&'a ()>, } -impl<T> LocalFutureObj<T> { +impl<'a, T> LocalFutureObj<'a, T> { /// Create a `LocalFutureObj` from a custom trait object representation. #[inline] - pub fn new<F: UnsafeFutureObj<T>>(f: F) -> LocalFutureObj<T> { + pub fn new<F: UnsafeFutureObj<'a, T> + 'a>(f: F) -> LocalFutureObj<'a, T> { LocalFutureObj { ptr: f.into_raw(), poll_fn: F::poll, drop_fn: F::drop, - _marker: PhantomData, + _marker1: PhantomData, + _marker2: PhantomData, } } @@ -45,26 +47,26 @@ impl<T> LocalFutureObj<T> { /// instance from which this `LocalFutureObj` was created actually /// implements `Send`. #[inline] - pub unsafe fn as_future_obj(self) -> FutureObj<T> { + pub unsafe fn as_future_obj(self) -> FutureObj<'a, T> { FutureObj(self) } } -impl<T> fmt::Debug for LocalFutureObj<T> { +impl<'a, T> fmt::Debug for LocalFutureObj<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("LocalFutureObj") .finish() } } -impl<T> From<FutureObj<T>> for LocalFutureObj<T> { +impl<'a, T> From<FutureObj<'a, T>> for LocalFutureObj<'a, T> { #[inline] - fn from(f: FutureObj<T>) -> LocalFutureObj<T> { + fn from(f: FutureObj<'a, T>) -> LocalFutureObj<'a, T> { f.0 } } -impl<T> Future for LocalFutureObj<T> { +impl<'a, T> Future for LocalFutureObj<'a, T> { type Output = T; #[inline] @@ -75,7 +77,7 @@ impl<T> Future for LocalFutureObj<T> { } } -impl<T> Drop for LocalFutureObj<T> { +impl<'a, T> Drop for LocalFutureObj<'a, T> { fn drop(&mut self) { unsafe { (self.drop_fn)(self.ptr) @@ -85,26 +87,26 @@ impl<T> Drop for LocalFutureObj<T> { /// A custom trait object for polling futures, roughly akin to /// `Box<dyn Future<Output = T>> + Send`. -pub struct FutureObj<T>(LocalFutureObj<T>); +pub struct FutureObj<'a, T>(LocalFutureObj<'a, T>); -unsafe impl<T> Send for FutureObj<T> {} +unsafe impl<'a, T> Send for FutureObj<'a, T> {} -impl<T> FutureObj<T> { +impl<'a, T> FutureObj<'a, T> { /// Create a `FutureObj` from a custom trait object representation. #[inline] - pub fn new<F: UnsafeFutureObj<T> + Send>(f: F) -> FutureObj<T> { + pub fn new<F: UnsafeFutureObj<'a, T> + Send>(f: F) -> FutureObj<'a, T> { FutureObj(LocalFutureObj::new(f)) } } -impl<T> fmt::Debug for FutureObj<T> { +impl<'a, T> fmt::Debug for FutureObj<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("FutureObj") .finish() } } -impl<T> Future for FutureObj<T> { +impl<'a, T> Future for FutureObj<'a, T> { type Output = T; #[inline] @@ -123,7 +125,7 @@ impl<T> Future for FutureObj<T> { /// The implementor must guarantee that it is safe to call `poll` repeatedly (in /// a non-concurrent fashion) with the result of `into_raw` until `drop` is /// called. -pub unsafe trait UnsafeFutureObj<T>: 'static { +pub unsafe trait UnsafeFutureObj<'a, T>: 'a { /// Convert a owned instance into a (conceptually owned) void pointer. fn into_raw(self) -> *mut (); diff --git a/src/libcore/future/mod.rs b/src/libcore/future/mod.rs new file mode 100644 index 00000000000..f9361a0f4e7 --- /dev/null +++ b/src/libcore/future/mod.rs @@ -0,0 +1,21 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![unstable(feature = "futures_api", + reason = "futures in libcore are unstable", + issue = "50547")] + +//! Asynchronous values. + +mod future; +pub use self::future::Future; + +mod future_obj; +pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj}; diff --git a/src/libcore/task/executor.rs b/src/libcore/task/executor.rs index 55ea5e724c1..f1db5093e98 100644 --- a/src/libcore/task/executor.rs +++ b/src/libcore/task/executor.rs @@ -13,7 +13,7 @@ issue = "50547")] use fmt; -use super::{FutureObj, LocalFutureObj}; +use future::{FutureObj, LocalFutureObj}; /// A task executor. /// @@ -29,7 +29,7 @@ pub trait Executor { /// /// The executor may be unable to spawn tasks, either because it has /// been shut down or is resource-constrained. - fn spawn_obj(&mut self, task: FutureObj<()>) -> Result<(), SpawnObjError>; + fn spawn_obj(&mut self, task: FutureObj<'static, ()>) -> Result<(), SpawnObjError>; /// Determine whether the executor is able to spawn new tasks. /// @@ -76,7 +76,7 @@ pub struct SpawnObjError { pub kind: SpawnErrorKind, /// The task for which spawning was attempted - pub task: FutureObj<()>, + pub task: FutureObj<'static, ()>, } /// The result of a failed spawn @@ -86,5 +86,5 @@ pub struct SpawnLocalObjError { pub kind: SpawnErrorKind, /// The task for which spawning was attempted - pub task: LocalFutureObj<()>, + pub task: LocalFutureObj<'static, ()>, } diff --git a/src/libcore/task/mod.rs b/src/libcore/task/mod.rs index 06cd7a9dd77..c4f07536164 100644 --- a/src/libcore/task/mod.rs +++ b/src/libcore/task/mod.rs @@ -25,8 +25,5 @@ pub use self::executor::{ mod poll; pub use self::poll::Poll; -mod future_obj; -pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj}; - mod wake; pub use self::wake::{Waker, LocalWaker, UnsafeWake}; diff --git a/src/test/run-pass/async-await.rs b/src/test/run-pass/async-await.rs index 3a67750e77e..0ac37485d3d 100644 --- a/src/test/run-pass/async-await.rs +++ b/src/test/run-pass/async-await.rs @@ -19,9 +19,10 @@ use std::sync::{ Arc, atomic::{self, AtomicUsize}, }; +use std::future::FutureObj; use std::task::{ Context, Poll, Wake, - Executor, FutureObj, SpawnObjError, + Executor, SpawnObjError, local_waker_from_nonlocal, }; @@ -37,7 +38,7 @@ impl Wake for Counter { struct NoopExecutor; impl Executor for NoopExecutor { - fn spawn_obj(&mut self, _: FutureObj<T>) -> Result<(), SpawnObjError> { + fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> { Ok(()) } } diff --git a/src/test/run-pass/futures-api.rs b/src/test/run-pass/futures-api.rs index a427b82af6a..6cb975a9560 100644 --- a/src/test/run-pass/futures-api.rs +++ b/src/test/run-pass/futures-api.rs @@ -19,10 +19,11 @@ use std::sync::{ Arc, atomic::{self, AtomicUsize}, }; +use std::future::FutureObj; use std::task::{ Context, Poll, Wake, Waker, LocalWaker, - Executor, FutureObj, SpawnObjError, + Executor, SpawnObjError, local_waker, local_waker_from_nonlocal, }; @@ -44,7 +45,7 @@ impl Wake for Counter { struct NoopExecutor; impl Executor for NoopExecutor { - fn spawn_obj(&mut self, _: FutureObj<()>) -> Result<(), SpawnObjError> { + fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> { Ok(()) } } |
