Skip to content

Commit 3792240

Browse files
committed
use pin_project
1 parent 7d2282d commit 3792240

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/stream/stream/count.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
use std::future::Future;
12
use std::pin::Pin;
23

3-
use crate::future::Future;
4+
use pin_project_lite::pin_project;
5+
46
use crate::stream::Stream;
57
use crate::task::{Context, Poll};
68

7-
#[doc(hidden)]
8-
#[allow(missing_debug_implementations)]
9-
pub struct CountFuture<S> {
10-
stream: S,
11-
count: usize,
9+
pin_project! {
10+
#[doc(hidden)]
11+
#[allow(missing_debug_implementations)]
12+
pub struct CountFuture<S> {
13+
#[pin]
14+
stream: S,
15+
count: usize,
16+
}
1217
}
1318

1419
impl<S> CountFuture<S> {
15-
pin_utils::unsafe_pinned!(stream: S);
16-
pin_utils::unsafe_unpinned!(count: usize);
17-
1820
pub(crate) fn new(stream: S) -> Self {
1921
CountFuture { stream, count: 0 }
2022
}
@@ -26,16 +28,17 @@ where
2628
{
2729
type Output = usize;
2830

29-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
30-
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
31+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
32+
let this = self.project();
33+
let next = futures_core::ready!(this.stream.poll_next(cx));
3134

3235
match next {
3336
Some(_) => {
3437
cx.waker().wake_by_ref();
35-
*self.as_mut().count() += 1;
38+
*this.count += 1;
3639
Poll::Pending
3740
}
38-
None => Poll::Ready(self.count),
41+
None => Poll::Ready(*this.count),
3942
}
4043
}
4144
}

src/stream/stream/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,10 +1813,10 @@ extension_trait! {
18131813
# fn main() { async_std::task::block_on(async {
18141814
#
18151815
use async_std::prelude::*;
1816-
use std::collections::VecDeque;
1816+
use async_std::stream;
18171817
1818-
let s1 = VecDeque::from(vec![0]);
1819-
let s2 = VecDeque::from(vec![1, 2, 3]);
1818+
let s1 = stream::from_iter(vec![0]);
1819+
let s2 = stream::from_iter(vec![1, 2, 3]);
18201820
18211821
assert_eq!(s1.count().await, 1);
18221822
assert_eq!(s2.count().await, 3);

0 commit comments

Comments
 (0)