Skip to content

Commit 4f9d185

Browse files
aturoncramertj
authored andcommitted
Transition futures-channel to 0.3.0-alpha
1 parent 622dc44 commit 4f9d185

File tree

6 files changed

+62
-52
lines changed

6 files changed

+62
-52
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ matrix:
4747
# - cargo test --manifest-path futures/testcrate/Cargo.toml
4848

4949
script:
50-
- cargo test --all
51-
- cargo test --all --release
50+
- cargo build --all # for now build only; tests require the full suite of crates
51+
# - cargo test --all
52+
# - cargo test --all --release
5253

5354
env:
5455
global:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = [
33
# "futures",
44
# "futures-async-runtime",
55
"futures-core",
6-
# "futures-channel",
6+
"futures-channel",
77
# "futures-executor",
88
# "futures-io",
99
# "futures-macro-async",

futures-channel/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "futures-channel"
3-
version = "0.2.0"
3+
version = "0.3.0-alpha"
44
authors = ["Alex Crichton <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
repository = "https://github.com/rust-lang-nursery/futures-rs"
@@ -15,8 +15,8 @@ std = ["futures-core/std"]
1515
default = ["std"]
1616

1717
[dependencies]
18-
futures-core = { path = "../futures-core", version = "0.2.0", default-features = false }
18+
futures-core = { path = "../futures-core", version = "0.3.0-alpha", default-features = false }
1919

2020
[dev-dependencies]
21-
futures = { path = "../futures", version = "0.2.0", default-features = true }
22-
futures-executor = { path = "../futures-executor", version = "0.2.0", default-features = true }
21+
# futures = { path = "../futures", version = "0.2.0", default-features = true }
22+
# futures-executor = { path = "../futures-executor", version = "0.2.0", default-features = true }

futures-channel/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//! This crate provides channels that can be used to communicate between
44
//! asynchronous tasks.
55
6+
#![feature(pin, arbitrary_self_types)]
7+
68
#![deny(missing_docs, missing_debug_implementations)]
79
#![doc(html_root_url = "https://docs.rs/futures-channel/0.2.0")]
810
#![no_std]

futures-channel/src/mpsc/mod.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
// happens-before semantics required for the acquire / release semantics used
7575
// by the queue structure.
7676

77+
use std::mem::Pin;
78+
use std::marker::Unpin;
7779
use std::fmt;
7880
use std::error::Error;
7981
use std::any::Any;
@@ -84,8 +86,7 @@ use std::thread;
8486
use std::usize;
8587

8688
use futures_core::task::{self, Waker};
87-
use futures_core::{Async, Poll, Stream};
88-
use futures_core::never::Never;
89+
use futures_core::{Poll, Stream};
8990

9091
use mpsc::queue::{Queue, PopResult};
9192

@@ -109,6 +110,9 @@ pub struct Sender<T> {
109110
maybe_parked: bool,
110111
}
111112

113+
// Safe because we treat the `T` opaquely
114+
unsafe impl<T> Unpin for Sender<T> {}
115+
112116
/// The transmission end of an unbounded mpsc channel.
113117
///
114118
/// This value is created by the [`unbounded`](unbounded) function.
@@ -126,12 +130,18 @@ pub struct Receiver<T> {
126130
inner: Arc<Inner<T>>,
127131
}
128132

133+
// Safe because we treat the `T` opaquely
134+
unsafe impl<T> Unpin for Receiver<T> {}
135+
129136
/// The receiving end of an unbounded mpsc channel.
130137
///
131138
/// This value is created by the [`unbounded`](unbounded) function.
132139
#[derive(Debug)]
133140
pub struct UnboundedReceiver<T>(Receiver<T>);
134141

142+
// Safe because we treat the `T` opaquely
143+
unsafe impl<T> Unpin for UnboundedReceiver<T> {}
144+
135145
/// The error type for [`Sender`s](Sender) used as `Sink`s.
136146
#[derive(Clone, Debug, PartialEq, Eq)]
137147
pub struct SendError {
@@ -511,14 +521,14 @@ impl<T> Sender<T> {
511521
Ok(())
512522
}
513523

514-
fn poll_ready_nb(&self) -> Poll<(), SendError> {
524+
fn poll_ready_nb(&self) -> Poll<Result<(), SendError>> {
515525
let state = decode_state(self.inner.state.load(SeqCst));
516526
if state.is_open {
517-
Ok(Async::Ready(()))
527+
Poll::Ready(Ok(()))
518528
} else {
519-
Err(SendError {
529+
Poll::Ready(Err(SendError {
520530
kind: SendErrorKind::Full,
521-
})
531+
}))
522532
}
523533
}
524534

@@ -637,15 +647,15 @@ impl<T> Sender<T> {
637647
/// - `Ok(Async::Pending)` if the channel may not have
638648
/// capacity, in which case the current task is queued to be notified once capacity is available;
639649
/// - `Err(SendError)` if the receiver has been dropped.
640-
pub fn poll_ready(&mut self, cx: &mut task::Context) -> Poll<(), SendError> {
650+
pub fn poll_ready(&mut self, cx: &mut task::Context) -> Poll<Result<(), SendError>> {
641651
let state = decode_state(self.inner.state.load(SeqCst));
642652
if !state.is_open {
643-
return Err(SendError {
653+
return Poll::Ready(Err(SendError {
644654
kind: SendErrorKind::Disconnected,
645-
});
655+
}));
646656
}
647657

648-
Ok(self.poll_unparked(Some(cx)))
658+
self.poll_unparked(Some(cx)).map(Ok)
649659
}
650660

651661
/// Returns whether this channel is closed without needing a context.
@@ -662,7 +672,7 @@ impl<T> Sender<T> {
662672
let _ = self.do_send_nb(None);
663673
}
664674

665-
fn poll_unparked(&mut self, cx: Option<&mut task::Context>) -> Async<()> {
675+
fn poll_unparked(&mut self, cx: Option<&mut task::Context>) -> Poll<()> {
666676
// First check the `maybe_parked` variable. This avoids acquiring the
667677
// lock in most cases
668678
if self.maybe_parked {
@@ -671,7 +681,7 @@ impl<T> Sender<T> {
671681

672682
if !task.is_parked {
673683
self.maybe_parked = false;
674-
return Async::Ready(())
684+
return Poll::Ready(())
675685
}
676686

677687
// At this point, an unpark request is pending, so there will be an
@@ -682,16 +692,16 @@ impl<T> Sender<T> {
682692
// task
683693
task.task = cx.map(|cx| cx.waker().clone());
684694

685-
Async::Pending
695+
Poll::Pending
686696
} else {
687-
Async::Ready(())
697+
Poll::Ready(())
688698
}
689699
}
690700
}
691701

692702
impl<T> UnboundedSender<T> {
693703
/// Check if the channel is ready to receive a message.
694-
pub fn poll_ready(&self, _: &mut task::Context) -> Poll<(), SendError> {
704+
pub fn poll_ready(&self, _: &mut task::Context) -> Poll<Result<(), SendError>> {
695705
self.0.poll_ready_nb()
696706
}
697707

@@ -832,14 +842,14 @@ impl<T> Receiver<T> {
832842
/// no longer empty.
833843
pub fn try_next(&mut self) -> Result<Option<T>, TryRecvError> {
834844
match self.next_message() {
835-
Async::Ready(msg) => {
845+
Poll::Ready(msg) => {
836846
Ok(msg)
837847
},
838-
Async::Pending => Err(TryRecvError { _inner: () }),
848+
Poll::Pending => Err(TryRecvError { _inner: () }),
839849
}
840850
}
841851

842-
fn next_message(&mut self) -> Async<Option<T>> {
852+
fn next_message(&mut self) -> Poll<Option<T>> {
843853
// Pop off a message
844854
loop {
845855
match unsafe { self.inner.message_queue.pop() } {
@@ -851,11 +861,11 @@ impl<T> Receiver<T> {
851861
// Decrement number of messages
852862
self.dec_num_messages();
853863

854-
return Async::Ready(msg);
864+
return Poll::Ready(msg);
855865
}
856866
PopResult::Empty => {
857867
// The queue is empty, return Pending
858-
return Async::Pending;
868+
return Poll::Pending;
859869
}
860870
PopResult::Inconsistent => {
861871
// Inconsistent means that there will be a message to pop
@@ -937,27 +947,26 @@ impl<T> Receiver<T> {
937947

938948
impl<T> Stream for Receiver<T> {
939949
type Item = T;
940-
type Error = Never;
941950

942-
fn poll_next(&mut self, cx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
951+
fn poll_next(mut self: Pin<Self>, cx: &mut task::Context) -> Poll<Option<T>> {
943952
loop {
944953
// Try to read a message off of the message queue.
945954
let msg = match self.next_message() {
946-
Async::Ready(msg) => msg,
947-
Async::Pending => {
955+
Poll::Ready(msg) => msg,
956+
Poll::Pending => {
948957
// There are no messages to read, in this case, attempt to
949958
// park. The act of parking will verify that the channel is
950959
// still empty after the park operation has completed.
951960
match self.try_park(cx) {
952961
TryPark::Parked => {
953962
// The task was parked, and the channel is still
954963
// empty, return Pending.
955-
return Ok(Async::Pending);
964+
return Poll::Pending;
956965
}
957966
TryPark::Closed => {
958967
// The channel is closed, there will be no further
959968
// messages.
960-
return Ok(Async::Ready(None));
969+
return Poll::Ready(None);
961970
}
962971
TryPark::NotEmpty => {
963972
// A message has been sent while attempting to
@@ -969,7 +978,7 @@ impl<T> Stream for Receiver<T> {
969978
}
970979
};
971980
// Return the message
972-
return Ok(Async::Ready(msg));
981+
return Poll::Ready(msg);
973982
}
974983
}
975984
}
@@ -1005,10 +1014,9 @@ impl<T> UnboundedReceiver<T> {
10051014

10061015
impl<T> Stream for UnboundedReceiver<T> {
10071016
type Item = T;
1008-
type Error = Never;
10091017

1010-
fn poll_next(&mut self, cx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
1011-
self.0.poll_next(cx)
1018+
fn poll_next(mut self: Pin<Self>, cx: &mut task::Context) -> Poll<Option<T>> {
1019+
Pin::new(&mut self.0).poll_next(cx)
10121020
}
10131021
}
10141022

futures-channel/src/oneshot.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! A channel for sending a single message between asynchronous tasks.
22
3+
use std::mem::Pin;
34
use std::sync::Arc;
45
use std::sync::atomic::AtomicBool;
56
use std::sync::atomic::Ordering::SeqCst;
67
use std::error::Error;
78
use std::fmt;
89

9-
use futures_core::{Future, Poll, Async};
10+
use futures_core::{Future, Poll};
1011
use futures_core::task::{self, Waker};
11-
use futures_core::never::Never;
1212

1313
use lock::Lock;
1414

@@ -157,13 +157,13 @@ impl<T> Inner<T> {
157157
}
158158
}
159159

160-
fn poll_cancel(&self, cx: &mut task::Context) -> Poll<(), Never> {
160+
fn poll_cancel(&self, cx: &mut task::Context) -> Poll<()> {
161161
// Fast path up first, just read the flag and see if our other half is
162162
// gone. This flag is set both in our destructor and the oneshot
163163
// destructor, but our destructor hasn't run yet so if it's set then the
164164
// oneshot is gone.
165165
if self.complete.load(SeqCst) {
166-
return Ok(Async::Ready(()))
166+
return Poll::Ready(())
167167
}
168168

169169
// If our other half is not gone then we need to park our current task
@@ -182,12 +182,12 @@ impl<T> Inner<T> {
182182
let handle = cx.waker().clone();
183183
match self.tx_task.try_lock() {
184184
Some(mut p) => *p = Some(handle),
185-
None => return Ok(Async::Ready(())),
185+
None => return Poll::Ready(()),
186186
}
187187
if self.complete.load(SeqCst) {
188-
Ok(Async::Ready(()))
188+
Poll::Ready(())
189189
} else {
190-
Ok(Async::Pending)
190+
Poll::Pending
191191
}
192192
}
193193

@@ -252,7 +252,7 @@ impl<T> Inner<T> {
252252
}
253253
}
254254

255-
fn recv(&self, cx: &mut task::Context) -> Poll<T, Canceled> {
255+
fn recv(&self, cx: &mut task::Context) -> Poll<Result<T, Canceled>> {
256256
let mut done = false;
257257

258258
// Check to see if some data has arrived. If it hasn't then we need to
@@ -286,12 +286,12 @@ impl<T> Inner<T> {
286286
// treat the send as a failure.
287287
if let Some(mut slot) = self.data.try_lock() {
288288
if let Some(data) = slot.take() {
289-
return Ok(data.into());
289+
return Poll::Ready(Ok(data));
290290
}
291291
}
292-
Err(Canceled)
292+
Poll::Ready(Err(Canceled))
293293
} else {
294-
Ok(Async::Pending)
294+
Poll::Pending
295295
}
296296
}
297297

@@ -353,7 +353,7 @@ impl<T> Sender<T> {
353353
/// alive and may be able to receive a message if sent. The current task,
354354
/// however, is scheduled to receive a notification if the corresponding
355355
/// `Receiver` goes away.
356-
pub fn poll_cancel(&mut self, cx: &mut task::Context) -> Poll<(), Never> {
356+
pub fn poll_cancel(&mut self, cx: &mut task::Context) -> Poll<()> {
357357
self.inner.poll_cancel(cx)
358358
}
359359

@@ -420,10 +420,9 @@ impl<T> Receiver<T> {
420420
}
421421

422422
impl<T> Future for Receiver<T> {
423-
type Item = T;
424-
type Error = Canceled;
423+
type Output = Result<T, Canceled>;
425424

426-
fn poll(&mut self, cx: &mut task::Context) -> Poll<T, Canceled> {
425+
fn poll(self: Pin<Self>, cx: &mut task::Context) -> Poll<Result<T, Canceled>> {
427426
self.inner.recv(cx)
428427
}
429428
}

0 commit comments

Comments
 (0)