Skip to content

Commit d8bf222

Browse files
Add lifetime to FutureObj
1 parent 9f70e7f commit d8bf222

File tree

8 files changed

+60
-39
lines changed

8 files changed

+60
-39
lines changed

src/liballoc/boxed.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ use core::marker::{Unpin, Unsize};
6666
use core::mem::{self, PinMut};
6767
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
6868
use core::ptr::{self, NonNull, Unique};
69-
use core::task::{Context, Poll, UnsafeFutureObj, FutureObj, LocalFutureObj};
69+
use core::future::{FutureObj, LocalFutureObj, UnsafeFutureObj};
70+
use core::task::{Context, Poll};
7071
use core::convert::From;
7172

7273
use raw_vec::RawVec;
@@ -915,7 +916,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinBox<U>> for PinBox<T> {}
915916
impl<T: ?Sized> Unpin for PinBox<T> {}
916917

917918
#[unstable(feature = "futures_api", issue = "50547")]
918-
impl<'a, F: ?Sized + Future + Unpin> Future for Box<F> {
919+
impl<F: ?Sized + Future + Unpin> Future for Box<F> {
919920
type Output = F::Output;
920921

921922
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> {
924925
}
925926

926927
#[unstable(feature = "futures_api", issue = "50547")]
927-
impl<'a, F: ?Sized + Future> Future for PinBox<F> {
928+
impl<F: ?Sized + Future> Future for PinBox<F> {
928929
type Output = F::Output;
929930

930931
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> {
933934
}
934935

935936
#[unstable(feature = "futures_api", issue = "50547")]
936-
unsafe impl<T, F: Future<Output = T> + 'static> UnsafeFutureObj<T> for PinBox<F> {
937+
unsafe impl<'a, T, F: Future<Output = T> + 'a> UnsafeFutureObj<'a, T> for PinBox<F> {
937938
fn into_raw(self) -> *mut () {
938939
PinBox::into_raw(self) as *mut ()
939940
}
@@ -950,28 +951,28 @@ unsafe impl<T, F: Future<Output = T> + 'static> UnsafeFutureObj<T> for PinBox<F>
950951
}
951952

952953
#[unstable(feature = "futures_api", issue = "50547")]
953-
impl<F: Future<Output = ()> + Send + 'static> From<PinBox<F>> for FutureObj<()> {
954+
impl<'a, F: Future<Output = ()> + Send + 'a> From<PinBox<F>> for FutureObj<'a, ()> {
954955
fn from(boxed: PinBox<F>) -> Self {
955956
FutureObj::new(boxed)
956957
}
957958
}
958959

959960
#[unstable(feature = "futures_api", issue = "50547")]
960-
impl<F: Future<Output = ()> + Send + 'static> From<Box<F>> for FutureObj<()> {
961+
impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()> {
961962
fn from(boxed: Box<F>) -> Self {
962963
FutureObj::new(PinBox::from(boxed))
963964
}
964965
}
965966

966967
#[unstable(feature = "futures_api", issue = "50547")]
967-
impl<F: Future<Output = ()> + 'static> From<PinBox<F>> for LocalFutureObj<()> {
968+
impl<'a, F: Future<Output = ()> + 'a> From<PinBox<F>> for LocalFutureObj<'a, ()> {
968969
fn from(boxed: PinBox<F>) -> Self {
969970
LocalFutureObj::new(boxed)
970971
}
971972
}
972973

973974
#[unstable(feature = "futures_api", issue = "50547")]
974-
impl<F: Future<Output = ()> + 'static> From<Box<F>> for LocalFutureObj<()> {
975+
impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
975976
fn from(boxed: Box<F>) -> Self {
976977
LocalFutureObj::new(PinBox::from(boxed))
977978
}

src/libcore/future.rs renamed to src/libcore/future/future.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
reason = "futures in libcore are unstable",
1313
issue = "50547")]
1414

15-
//! Asynchronous values.
16-
1715
use mem::PinMut;
1816
use marker::Unpin;
1917
use task::{self, Poll};

src/libcore/task/future_obj.rs renamed to src/libcore/future/future_obj.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@ use task::{Context, Poll};
2121
/// A custom trait object for polling futures, roughly akin to
2222
/// `Box<dyn Future<Output = T>>`.
2323
/// Contrary to `FutureObj`, `LocalFutureObj` does not have a `Send` bound.
24-
pub struct LocalFutureObj<T> {
24+
pub struct LocalFutureObj<'a, T> {
2525
ptr: *mut (),
2626
poll_fn: unsafe fn(*mut (), &mut Context) -> Poll<T>,
2727
drop_fn: unsafe fn(*mut ()),
28-
_marker: PhantomData<T>,
28+
_marker1: PhantomData<T>,
29+
_marker2: PhantomData<&'a ()>,
2930
}
3031

31-
impl<T> LocalFutureObj<T> {
32+
impl<'a, T> LocalFutureObj<'a, T> {
3233
/// Create a `LocalFutureObj` from a custom trait object representation.
3334
#[inline]
34-
pub fn new<F: UnsafeFutureObj<T>>(f: F) -> LocalFutureObj<T> {
35+
pub fn new<F: UnsafeFutureObj<'a, T> + 'a>(f: F) -> LocalFutureObj<'a, T> {
3536
LocalFutureObj {
3637
ptr: f.into_raw(),
3738
poll_fn: F::poll,
3839
drop_fn: F::drop,
39-
_marker: PhantomData,
40+
_marker1: PhantomData,
41+
_marker2: PhantomData,
4042
}
4143
}
4244

@@ -45,26 +47,26 @@ impl<T> LocalFutureObj<T> {
4547
/// instance from which this `LocalFutureObj` was created actually
4648
/// implements `Send`.
4749
#[inline]
48-
pub unsafe fn as_future_obj(self) -> FutureObj<T> {
50+
pub unsafe fn as_future_obj(self) -> FutureObj<'a, T> {
4951
FutureObj(self)
5052
}
5153
}
5254

53-
impl<T> fmt::Debug for LocalFutureObj<T> {
55+
impl<'a, T> fmt::Debug for LocalFutureObj<'a, T> {
5456
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5557
f.debug_struct("LocalFutureObj")
5658
.finish()
5759
}
5860
}
5961

60-
impl<T> From<FutureObj<T>> for LocalFutureObj<T> {
62+
impl<'a, T> From<FutureObj<'a, T>> for LocalFutureObj<'a, T> {
6163
#[inline]
62-
fn from(f: FutureObj<T>) -> LocalFutureObj<T> {
64+
fn from(f: FutureObj<'a, T>) -> LocalFutureObj<'a, T> {
6365
f.0
6466
}
6567
}
6668

67-
impl<T> Future for LocalFutureObj<T> {
69+
impl<'a, T> Future for LocalFutureObj<'a, T> {
6870
type Output = T;
6971

7072
#[inline]
@@ -75,7 +77,7 @@ impl<T> Future for LocalFutureObj<T> {
7577
}
7678
}
7779

78-
impl<T> Drop for LocalFutureObj<T> {
80+
impl<'a, T> Drop for LocalFutureObj<'a, T> {
7981
fn drop(&mut self) {
8082
unsafe {
8183
(self.drop_fn)(self.ptr)
@@ -85,26 +87,26 @@ impl<T> Drop for LocalFutureObj<T> {
8587

8688
/// A custom trait object for polling futures, roughly akin to
8789
/// `Box<dyn Future<Output = T>> + Send`.
88-
pub struct FutureObj<T>(LocalFutureObj<T>);
90+
pub struct FutureObj<'a, T>(LocalFutureObj<'a, T>);
8991

90-
unsafe impl<T> Send for FutureObj<T> {}
92+
unsafe impl<'a, T> Send for FutureObj<'a, T> {}
9193

92-
impl<T> FutureObj<T> {
94+
impl<'a, T> FutureObj<'a, T> {
9395
/// Create a `FutureObj` from a custom trait object representation.
9496
#[inline]
95-
pub fn new<F: UnsafeFutureObj<T> + Send>(f: F) -> FutureObj<T> {
97+
pub fn new<F: UnsafeFutureObj<'a, T> + Send>(f: F) -> FutureObj<'a, T> {
9698
FutureObj(LocalFutureObj::new(f))
9799
}
98100
}
99101

100-
impl<T> fmt::Debug for FutureObj<T> {
102+
impl<'a, T> fmt::Debug for FutureObj<'a, T> {
101103
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102104
f.debug_struct("FutureObj")
103105
.finish()
104106
}
105107
}
106108

107-
impl<T> Future for FutureObj<T> {
109+
impl<'a, T> Future for FutureObj<'a, T> {
108110
type Output = T;
109111

110112
#[inline]
@@ -123,7 +125,7 @@ impl<T> Future for FutureObj<T> {
123125
/// The implementor must guarantee that it is safe to call `poll` repeatedly (in
124126
/// a non-concurrent fashion) with the result of `into_raw` until `drop` is
125127
/// called.
126-
pub unsafe trait UnsafeFutureObj<T>: 'static {
128+
pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
127129
/// Convert a owned instance into a (conceptually owned) void pointer.
128130
fn into_raw(self) -> *mut ();
129131

src/libcore/future/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![unstable(feature = "futures_api",
12+
reason = "futures in libcore are unstable",
13+
issue = "50547")]
14+
15+
//! Asynchronous values.
16+
17+
mod future;
18+
pub use self::future::Future;
19+
20+
mod future_obj;
21+
pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};

src/libcore/task/executor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
issue = "50547")]
1414

1515
use fmt;
16-
use super::{FutureObj, LocalFutureObj};
16+
use future::{FutureObj, LocalFutureObj};
1717

1818
/// A task executor.
1919
///
@@ -29,7 +29,7 @@ pub trait Executor {
2929
///
3030
/// The executor may be unable to spawn tasks, either because it has
3131
/// been shut down or is resource-constrained.
32-
fn spawn_obj(&mut self, task: FutureObj<()>) -> Result<(), SpawnObjError>;
32+
fn spawn_obj(&mut self, task: FutureObj<'static, ()>) -> Result<(), SpawnObjError>;
3333

3434
/// Determine whether the executor is able to spawn new tasks.
3535
///
@@ -76,7 +76,7 @@ pub struct SpawnObjError {
7676
pub kind: SpawnErrorKind,
7777

7878
/// The task for which spawning was attempted
79-
pub task: FutureObj<()>,
79+
pub task: FutureObj<'static, ()>,
8080
}
8181

8282
/// The result of a failed spawn
@@ -86,5 +86,5 @@ pub struct SpawnLocalObjError {
8686
pub kind: SpawnErrorKind,
8787

8888
/// The task for which spawning was attempted
89-
pub task: LocalFutureObj<()>,
89+
pub task: LocalFutureObj<'static, ()>,
9090
}

src/libcore/task/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,5 @@ pub use self::executor::{
2525
mod poll;
2626
pub use self::poll::Poll;
2727

28-
mod future_obj;
29-
pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};
30-
3128
mod wake;
3229
pub use self::wake::{Waker, LocalWaker, UnsafeWake};

src/test/run-pass/async-await.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ use std::sync::{
1919
Arc,
2020
atomic::{self, AtomicUsize},
2121
};
22+
use std::future::FutureObj;
2223
use std::task::{
2324
Context, Poll, Wake,
24-
Executor, FutureObj, SpawnObjError,
25+
Executor, SpawnObjError,
2526
local_waker_from_nonlocal,
2627
};
2728

@@ -37,7 +38,7 @@ impl Wake for Counter {
3738

3839
struct NoopExecutor;
3940
impl Executor for NoopExecutor {
40-
fn spawn_obj(&mut self, _: FutureObj<T>) -> Result<(), SpawnObjError> {
41+
fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> {
4142
Ok(())
4243
}
4344
}

src/test/run-pass/futures-api.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ use std::sync::{
1919
Arc,
2020
atomic::{self, AtomicUsize},
2121
};
22+
use std::future::FutureObj;
2223
use std::task::{
2324
Context, Poll,
2425
Wake, Waker, LocalWaker,
25-
Executor, FutureObj, SpawnObjError,
26+
Executor, SpawnObjError,
2627
local_waker, local_waker_from_nonlocal,
2728
};
2829

@@ -44,7 +45,7 @@ impl Wake for Counter {
4445
struct NoopExecutor;
4546

4647
impl Executor for NoopExecutor {
47-
fn spawn_obj(&mut self, _: FutureObj<()>) -> Result<(), SpawnObjError> {
48+
fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> {
4849
Ok(())
4950
}
5051
}

0 commit comments

Comments
 (0)