diff options
author | Alex Crichton <alex@alexcrichton.com> | 2016-08-12 11:54:19 -0700 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2016-08-12 15:39:41 -0700 |
commit | b508964db63b526ccd88805195a50b5a66da8438 (patch) | |
tree | 5e2b3c94d72d0b8952f5f190b06128532d481ded /src/bin/sink.rs | |
parent | 9911f421eba909012cb1856d4d983fc68d34569c (diff) |
Remove Send from Future/Stream
This bound existed for two primary reasons, both detail below, and both of which
have now been solved.
One of the primary reasons this existed was due to the presence of `tailcall`.
Each standard combinator will call `tailcall` as appropriate, storing the
resulting trait object. Storing trait objects influences the applicatoin of the
`Send` and `Sync` bounds normally, but a key insight here is that we're not
storing trait objects but rather just pieces of otherwise internal futures.
With this insight the main storage for these futures, `Collapsed`, could simply
implement `Send` so long as the future itself originally implemented `Send`.
This in turn means that `tailcall` must be an `unsafe` method, but it seems well
worth the benefit of relaxing the `Send` bound.
The second primary reason for this bound was so the `Task` itself could be send.
This is critical for ensuring that futures can receive notifications from
multiple threads (e.g. be a future waiting on sources of multiple events).
Another key insight here, however, is that only the *outer* future needs to be
`Send`. We already have a solution, with `LoopData`, to make non-`Send` data
`Send`. By implementing `Future` directly for `LoopData<F: Future>`, this means
that it's trivial to make any future sendable by simply pinning it to an event
loop!
With these two pieces combined, it means that `Send` is no longer needed as a
bound on the `Future` and `Stream` traits. It may practically mean that
`LoopData` is used commonly in some scenarios, but that's quite a small price to
pay for relaxing the requirements of the core trait.
Some other ramifications of this change are:
* The `Future::boxed` and `Stream::boxed` methods now require `Self` to adhere
to `Send`. This is expected to be the most common case, and in the less common
case of not-`Send` `Box::new` can be used.
* Two new type aliases, `BoxFuture` and `BoxStream` have been introduced to
assist in writing APIs that return a trait object which is `Send`. Both of
these type aliases package in the `Send` bound.
* A new `LoopPin` type, added in the previous commit, can be used to easily
generate handles that can be used to pin futures to an event loop without
having a literal reference to the event loop itself.
Diffstat (limited to 'src/bin/sink.rs')
-rw-r--r-- | src/bin/sink.rs | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/src/bin/sink.rs b/src/bin/sink.rs index a506833e..9f81b30c 100644 --- a/src/bin/sink.rs +++ b/src/bin/sink.rs @@ -14,6 +14,7 @@ use std::net::SocketAddr; use futures::Future; use futures::stream::Stream; +use futures_io::IoFuture; fn main() { let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); @@ -33,7 +34,7 @@ fn main() { l.run(server).unwrap(); } -fn write(socket: futures_mio::TcpStream) -> Box<futures_io::IoFuture<()>> { +fn write(socket: futures_mio::TcpStream) -> IoFuture<()> { static BUF: &'static [u8] = &[0; 64 * 1024]; socket.into_future().map_err(|e| e.0).and_then(move |(ready, mut socket)| { let ready = match ready { |