Skip to content

Commit 76975a4

Browse files
committed
Merge branch 'master' into fix_clippy
2 parents 7d616c6 + 355e2ed commit 76975a4

File tree

5 files changed

+139
-11
lines changed

5 files changed

+139
-11
lines changed

Cargo.toml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,33 @@ std = [
5050
]
5151

5252
[dependencies]
53-
async-attributes = { version = "1.1.0", optional = true }
53+
async-attributes = { version = "1.1.1", optional = true }
5454
async-macros = { version = "2.0.0", optional = true }
5555
async-task = { version = "1.0.0", optional = true }
5656
broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] }
57-
crossbeam-channel = { version = "0.3.9", optional = true }
58-
crossbeam-deque = { version = "0.7.1", optional = true }
59-
crossbeam-utils = { version = "0.6.6", optional = true }
60-
futures-core = { version = "0.3.0", optional = true }
61-
futures-io = { version = "0.3.0", optional = true }
62-
futures-timer = { version = "1.0.2", optional = true }
57+
crossbeam-channel = { version = "0.4.0", optional = true }
58+
crossbeam-deque = { version = "0.7.2", optional = true }
59+
crossbeam-utils = { version = "0.7.0", optional = true }
60+
futures-core = { version = "0.3.1", optional = true }
61+
futures-io = { version = "0.3.1", optional = true }
62+
futures-timer = { version = "2.0.2", optional = true }
6363
kv-log-macro = { version = "1.0.4", optional = true }
6464
log = { version = "0.4.8", features = ["kv_unstable"], optional = true }
6565
memchr = { version = "2.2.1", optional = true }
6666
mio = { version = "0.6.19", optional = true }
6767
mio-uds = { version = "0.6.7", optional = true }
68-
num_cpus = { version = "1.10.1", optional = true }
68+
num_cpus = { version = "1.11.1", optional = true }
6969
once_cell = { version = "1.2.0", optional = true }
70-
pin-project-lite = { version = "0.1", optional = true }
70+
pin-project-lite = { version = "0.1.1", optional = true }
7171
pin-utils = { version = "0.1.0-alpha.4", optional = true }
7272
slab = { version = "0.4.2", optional = true }
7373

7474
[dev-dependencies]
75-
femme = "1.2.0"
75+
femme = "1.3.0"
7676
rand = "0.7.2"
7777
surf = "1.0.3"
7878
tempdir = "0.3.7"
79-
futures = "0.3.0"
79+
futures = "0.3.1"
8080

8181
[[test]]
8282
name = "stream"

src/stream/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ cfg_unstable! {
326326
mod interval;
327327
mod into_stream;
328328
mod product;
329+
mod successors;
329330
mod sum;
330331

331332
pub use double_ended_stream::DoubleEndedStream;
@@ -337,5 +338,6 @@ cfg_unstable! {
337338
pub use into_stream::IntoStream;
338339
pub use product::Product;
339340
pub use stream::Merge;
341+
pub use successors::{successors, Successors};
340342
pub use sum::Sum;
341343
}

src/stream/stream/mod.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,54 @@ extension_trait! {
14461446
}
14471447
}
14481448

1449+
#[doc = r#"
1450+
Borrows an stream, rather than consuming it.
1451+
1452+
This is useful to allow applying stream adaptors while still retaining ownership of the original stream.
1453+
1454+
# Examples
1455+
1456+
```
1457+
# fn main() { async_std::task::block_on(async {
1458+
#
1459+
use async_std::prelude::*;
1460+
use async_std::stream;
1461+
1462+
let a = vec![1isize, 2, 3];
1463+
1464+
let stream = stream::from_iter(a);
1465+
1466+
let sum: isize = stream.take(5).sum().await;
1467+
1468+
assert_eq!(sum, 6);
1469+
1470+
// if we try to use stream again, it won't work. The following line
1471+
// gives "error: use of moved value: `stream`
1472+
// assert_eq!(stream.next(), None);
1473+
1474+
// let's try that again
1475+
let a = vec![1isize, 2, 3];
1476+
1477+
let mut stream = stream::from_iter(a);
1478+
1479+
// instead, we add in a .by_ref()
1480+
let sum: isize = stream.by_ref().take(2).sum().await;
1481+
1482+
assert_eq!(sum, 3);
1483+
1484+
// now this is just fine:
1485+
assert_eq!(stream.next().await, Some(3));
1486+
assert_eq!(stream.next().await, None);
1487+
#
1488+
# }) }
1489+
```
1490+
"#]
1491+
#[cfg(all(feature = "default", feature = "unstable"))]
1492+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
1493+
fn by_ref(&mut self) -> &mut Self {
1494+
self
1495+
}
1496+
14491497
#[doc = r#"
14501498
A stream adaptor similar to [`fold`] that holds internal state and produces a new
14511499
stream.

src/stream/stream/partition.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ where
4545
match next {
4646
Some(v) => {
4747
let res = this.res.as_mut().unwrap();
48+
4849
if (this.f)(&v) {
4950
res.0.extend(Some(v))
5051
} else {

src/stream/successors.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use std::mem;
2+
use std::pin::Pin;
3+
4+
use crate::stream::Stream;
5+
use crate::task::{Context, Poll};
6+
7+
use pin_project_lite::pin_project;
8+
9+
/// Creates a new stream where to produce each new element a closure is called with the previous
10+
/// value.
11+
///
12+
/// # Examples
13+
///
14+
/// ```
15+
/// # fn main() { async_std::task::block_on(async {
16+
/// #
17+
/// use async_std::prelude::*;
18+
/// use async_std::stream;
19+
///
20+
/// let mut s = stream::successors(Some(22), |&val| Some(val + 1));
21+
///
22+
/// assert_eq!(s.next().await, Some(22));
23+
/// assert_eq!(s.next().await, Some(23));
24+
/// assert_eq!(s.next().await, Some(24));
25+
/// assert_eq!(s.next().await, Some(25));
26+
///
27+
/// #
28+
/// # }) }
29+
/// ```
30+
#[cfg(feature = "unstable")]
31+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
32+
pub fn successors<F, T>(first: Option<T>, succ: F) -> Successors<F, T>
33+
where
34+
F: FnMut(&T) -> Option<T>,
35+
{
36+
Successors { succ, slot: first }
37+
}
38+
39+
pin_project! {
40+
/// A stream that yields elements by calling an async closure with the previous value as an
41+
/// argument
42+
///
43+
/// This stream is constructed by [`successors`] function
44+
///
45+
/// [`successors`]: fn.succssors.html
46+
#[cfg(feature = "unstable")]
47+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
48+
#[derive(Debug)]
49+
pub struct Successors<F, T>
50+
where
51+
F: FnMut(&T) -> Option<T>
52+
{
53+
succ: F,
54+
slot: Option<T>,
55+
}
56+
}
57+
58+
impl<F, T> Stream for Successors<F, T>
59+
where
60+
F: FnMut(&T) -> Option<T>,
61+
{
62+
type Item = T;
63+
64+
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
65+
let this = self.project();
66+
67+
if this.slot.is_none() {
68+
return Poll::Ready(None);
69+
}
70+
71+
let mut next = (this.succ)(&this.slot.as_ref().unwrap());
72+
73+
// 'swapping' here means 'slot' will hold the next value and next will be th one from the previous iteration
74+
mem::swap(this.slot, &mut next);
75+
Poll::Ready(next)
76+
}
77+
}

0 commit comments

Comments
 (0)