Skip to content

Commit 7916ba6

Browse files
committed
Move type/trait aliases to futures-util
1 parent f71d3f7 commit 7916ba6

38 files changed

+172
-189
lines changed

futures-core/src/future.rs

-50
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,10 @@
22
33
use core::ops::DerefMut;
44
use core::pin::Pin;
5-
use core::task::{Context, Poll};
65

76
#[doc(no_inline)]
87
pub use core::future::Future;
98

10-
/// An owned dynamically typed [`Future`] for use in cases where you can't
11-
/// statically type your result or need to add some indirection.
12-
#[cfg(feature = "alloc")]
13-
pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>;
14-
15-
/// `BoxFuture`, but without the `Send` requirement.
16-
#[cfg(feature = "alloc")]
17-
pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>;
18-
199
/// A future which tracks whether or not the underlying future
2010
/// should no longer be polled.
2111
///
@@ -45,46 +35,6 @@ where
4535
}
4636
}
4737

48-
mod private_try_future {
49-
use super::Future;
50-
51-
pub trait Sealed {}
52-
53-
impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {}
54-
}
55-
56-
/// A convenience for futures that return `Result` values that includes
57-
/// a variety of adapters tailored to such futures.
58-
pub trait TryFuture: Future + private_try_future::Sealed {
59-
/// The type of successful values yielded by this future
60-
type Ok;
61-
62-
/// The type of failures yielded by this future
63-
type Error;
64-
65-
/// Poll this `TryFuture` as if it were a `Future`.
66-
///
67-
/// This method is a stopgap for a compiler limitation that prevents us from
68-
/// directly inheriting from the `Future` trait; in the future it won't be
69-
/// needed.
70-
fn try_poll(
71-
self: Pin<&mut Self>,
72-
cx: &mut Context<'_>,
73-
) -> Poll<Result<Self::Ok, Self::Error>>;
74-
}
75-
76-
impl<F, T, E> TryFuture for F
77-
where F: ?Sized + Future<Output = Result<T, E>>
78-
{
79-
type Ok = T;
80-
type Error = E;
81-
82-
#[inline]
83-
fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
84-
self.poll(cx)
85-
}
86-
}
87-
8838
#[cfg(feature = "alloc")]
8939
mod if_alloc {
9040
use alloc::boxed::Box;

futures-core/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ compile_error!("The `cfg-target-has-atomic` feature requires the `unstable` feat
1717
extern crate alloc;
1818

1919
pub mod future;
20-
#[doc(hidden)] pub use self::future::{Future, FusedFuture, TryFuture};
20+
#[doc(hidden)] pub use self::future::{Future, FusedFuture};
2121

2222
pub mod stream;
23-
#[doc(hidden)] pub use self::stream::{Stream, FusedStream, TryStream};
23+
#[doc(hidden)] pub use self::stream::{Stream, FusedStream};
2424

2525
#[macro_use]
2626
pub mod task;

futures-core/src/stream.rs

-48
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ use core::ops::DerefMut;
44
use core::pin::Pin;
55
use core::task::{Context, Poll};
66

7-
/// An owned dynamically typed [`Stream`] for use in cases where you can't
8-
/// statically type your result or need to add some indirection.
9-
#[cfg(feature = "alloc")]
10-
pub type BoxStream<'a, T> = Pin<alloc::boxed::Box<dyn Stream<Item = T> + Send + 'a>>;
11-
12-
/// `BoxStream`, but without the `Send` requirement.
13-
#[cfg(feature = "alloc")]
14-
pub type LocalBoxStream<'a, T> = Pin<alloc::boxed::Box<dyn Stream<Item = T> + 'a>>;
15-
167
/// A stream of values produced asynchronously.
178
///
189
/// If `Future<Output = T>` is an asynchronous version of `T`, then `Stream<Item
@@ -163,45 +154,6 @@ where
163154
}
164155
}
165156

166-
mod private_try_stream {
167-
use super::Stream;
168-
169-
pub trait Sealed {}
170-
171-
impl<S, T, E> Sealed for S where S: ?Sized + Stream<Item = Result<T, E>> {}
172-
}
173-
174-
/// A convenience for streams that return `Result` values that includes
175-
/// a variety of adapters tailored to such futures.
176-
pub trait TryStream: Stream + private_try_stream::Sealed {
177-
/// The type of successful values yielded by this future
178-
type Ok;
179-
180-
/// The type of failures yielded by this future
181-
type Error;
182-
183-
/// Poll this `TryStream` as if it were a `Stream`.
184-
///
185-
/// This method is a stopgap for a compiler limitation that prevents us from
186-
/// directly inheriting from the `Stream` trait; in the future it won't be
187-
/// needed.
188-
fn try_poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>)
189-
-> Poll<Option<Result<Self::Ok, Self::Error>>>;
190-
}
191-
192-
impl<S, T, E> TryStream for S
193-
where S: ?Sized + Stream<Item = Result<T, E>>
194-
{
195-
type Ok = T;
196-
type Error = E;
197-
198-
fn try_poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>)
199-
-> Poll<Option<Result<Self::Ok, Self::Error>>>
200-
{
201-
self.poll_next(cx)
202-
}
203-
}
204-
205157
#[cfg(feature = "alloc")]
206158
mod if_alloc {
207159
use alloc::boxed::Box;

futures-util/src/compat/compat03as01.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use futures_01::{
66
use futures_01::{
77
AsyncSink as AsyncSink01, Sink as Sink01, StartSend as StartSend01,
88
};
9-
use futures_core::{
9+
use crate::{
1010
task::{RawWaker, RawWakerVTable},
1111
future::TryFuture as TryFuture03,
1212
stream::TryStream as TryStream03,

futures-util/src/future/future/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::future::{assert_future, Either};
1212
use crate::never::Never;
1313
use crate::stream::assert_stream;
1414
#[cfg(feature = "alloc")]
15-
use futures_core::future::{BoxFuture, LocalBoxFuture};
15+
use crate::future::{BoxFuture, LocalBoxFuture};
1616
use futures_core::{
1717
future::Future,
1818
stream::Stream,

futures-util/src/future/mod.rs

+56-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,67 @@
99
//! from a closure that defines its return value, and [`ready`](ready()),
1010
//! which constructs a future with an immediate defined value.
1111
12+
use core::pin::Pin;
13+
use futures_core::task::{Poll, Context};
14+
#[cfg(feature = "alloc")]
15+
use alloc::boxed::Box;
16+
1217
#[doc(no_inline)]
1318
pub use core::future::Future;
1419

15-
#[cfg(feature = "alloc")]
16-
pub use futures_core::future::{BoxFuture, LocalBoxFuture};
17-
pub use futures_core::future::{FusedFuture, TryFuture};
20+
pub use futures_core::future::FusedFuture;
1821
pub use futures_task::{FutureObj, LocalFutureObj, UnsafeFutureObj};
1922

23+
/// An owned dynamically typed [`Future`] for use in cases where you can't
24+
/// statically type your result or need to add some indirection.
25+
#[cfg(feature = "alloc")]
26+
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
27+
28+
/// `BoxFuture`, but without the `Send` requirement.
29+
#[cfg(feature = "alloc")]
30+
pub type LocalBoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
31+
32+
33+
mod private_try_future {
34+
use super::Future;
35+
36+
pub trait Sealed {}
37+
38+
impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {}
39+
}
40+
41+
/// A convenience for futures that return `Result` values that includes
42+
/// a variety of adapters tailored to such futures.
43+
pub trait TryFuture: Future + private_try_future::Sealed {
44+
/// The type of successful values yielded by this future
45+
type Ok;
46+
47+
/// The type of failures yielded by this future
48+
type Error;
49+
50+
/// Poll this `TryFuture` as if it were a `Future`.
51+
///
52+
/// This method is a stopgap for a compiler limitation that prevents us from
53+
/// directly inheriting from the `Future` trait; in the future it won't be
54+
/// needed.
55+
fn try_poll(
56+
self: Pin<&mut Self>,
57+
cx: &mut Context<'_>,
58+
) -> Poll<Result<Self::Ok, Self::Error>>;
59+
}
60+
61+
impl<F, T, E> TryFuture for F
62+
where F: ?Sized + Future<Output = Result<T, E>>
63+
{
64+
type Ok = T;
65+
type Error = E;
66+
67+
#[inline]
68+
fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
69+
self.poll(cx)
70+
}
71+
}
72+
2073
// Extension traits and combinators
2174
#[allow(clippy::module_inception)]
2275
mod future;

futures-util/src/future/select_ok.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use super::assert_future;
2-
use crate::future::TryFutureExt;
2+
use crate::future::{Future, TryFuture, TryFutureExt};
33
use core::iter::FromIterator;
44
use core::mem;
55
use core::pin::Pin;
66
use alloc::vec::Vec;
7-
use futures_core::future::{Future, TryFuture};
87
use futures_core::task::{Context, Poll};
98

109
/// Future for the [`select_ok`] function.

futures-util/src/future/try_future/into_future.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::future::{FusedFuture, Future, TryFuture};
12
use core::pin::Pin;
2-
use futures_core::future::{FusedFuture, Future, TryFuture};
33
use futures_core::task::{Context, Poll};
44
use pin_project_lite::pin_project;
55

futures-util/src/future/try_future/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
66
#[cfg(feature = "compat")]
77
use crate::compat::Compat;
8-
use core::pin::Pin;
9-
use futures_core::{
8+
use crate::{
109
future::TryFuture,
1110
stream::TryStream,
1211
task::{Context, Poll},
1312
};
13+
use core::pin::Pin;
1414
#[cfg(feature = "sink")]
1515
use futures_sink::Sink;
1616

futures-util/src/future/try_future/try_flatten.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::future::{FusedFuture, Future, TryFuture};
2+
use crate::stream::{FusedStream, Stream, TryStream};
13
use core::pin::Pin;
2-
use futures_core::future::{FusedFuture, Future, TryFuture};
34
use futures_core::ready;
4-
use futures_core::stream::{FusedStream, Stream, TryStream};
55
#[cfg(feature = "sink")]
66
use futures_sink::Sink;
77
use futures_core::task::{Context, Poll};

futures-util/src/future/try_future/try_flatten_err.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::future::{FusedFuture, Future, TryFuture};
12
use core::pin::Pin;
2-
use futures_core::future::{FusedFuture, Future, TryFuture};
33
use futures_core::ready;
44
use futures_core::task::{Context, Poll};
55
use pin_project_lite::pin_project;

futures-util/src/future/try_join.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![allow(non_snake_case)]
22

3-
use crate::future::{assert_future, try_maybe_done, TryMaybeDone};
3+
use crate::future::{assert_future, try_maybe_done, Future, TryFuture, TryMaybeDone};
44
use core::fmt;
55
use core::pin::Pin;
6-
use futures_core::future::{Future, TryFuture};
76
use futures_core::task::{Context, Poll};
87
use pin_project_lite::pin_project;
98

futures-util/src/future/try_maybe_done.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Definition of the TryMaybeDone combinator
22
3-
use super::assert_future;
3+
use crate::future::{assert_future, FusedFuture, Future, TryFuture};
44
use core::mem;
55
use core::pin::Pin;
6-
use futures_core::future::{FusedFuture, Future, TryFuture};
76
use futures_core::ready;
87
use futures_core::task::{Context, Poll};
98

futures-util/src/future/try_select.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use crate::future::{Either, Future, TryFuture, TryFutureExt};
12
use core::pin::Pin;
2-
use futures_core::future::{Future, TryFuture};
33
use futures_core::task::{Context, Poll};
4-
use crate::future::{Either, TryFutureExt};
54

65
/// Future for the [`try_select()`] function.
76
#[must_use = "futures do nothing unless you `.await` or poll them"]

futures-util/src/sink/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
//! - The [`SinkExt`] trait, which provides adapters for chaining and composing
77
//! sinks.
88
9-
use crate::future::{assert_future, Either};
9+
use crate::future::{assert_future, Either, Future};
10+
use crate::stream::{Stream, TryStream};
1011
use core::pin::Pin;
11-
use futures_core::future::Future;
12-
use futures_core::stream::{Stream, TryStream};
1312
use futures_core::task::{Context, Poll};
1413

1514
#[cfg(feature = "compat")]

futures-util/src/sink/send_all.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use crate::stream::{StreamExt, TryStreamExt, Fuse};
1+
use crate::future::Future;
2+
use crate::stream::{Fuse, Stream, StreamExt, TryStream, TryStreamExt};
23
use core::fmt;
34
use core::pin::Pin;
4-
use futures_core::future::Future;
55
use futures_core::ready;
6-
use futures_core::stream::{TryStream, Stream};
76
use futures_core::task::{Context, Poll};
87
use futures_sink::Sink;
98

0 commit comments

Comments
 (0)