Files
tokio/tokio-stream/src/stream_ext/map.rs
T

52 lines
1.1 KiB
Rust
Raw Normal View History

2020-12-15 23:24:38 -05:00
use crate::Stream;
2019-12-18 22:57:22 +03:00
use core::fmt;
use core::pin::Pin;
use core::task::{Context, Poll};
use pin_project_lite::pin_project;
pin_project! {
/// Stream for the [`map`](super::StreamExt::map) method.
#[must_use = "streams do nothing unless polled"]
pub struct Map<St, F> {
#[pin]
stream: St,
f: F,
}
}
impl<St, F> fmt::Debug for Map<St, F>
where
St: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-12-21 00:54:43 +03:00
f.debug_struct("Map").field("stream", &self.stream).finish()
2019-12-18 22:57:22 +03:00
}
}
2019-12-25 23:48:02 +03:00
impl<St, F> Map<St, F> {
pub(super) fn new(stream: St, f: F) -> Self {
2019-12-18 22:57:22 +03:00
Map { stream, f }
}
}
impl<St, F, T> Stream for Map<St, F>
2019-12-21 00:54:43 +03:00
where
St: Stream,
F: FnMut(St::Item) -> T,
2019-12-18 22:57:22 +03:00
{
type Item = T;
2019-12-21 00:54:43 +03:00
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> {
2019-12-18 22:57:22 +03:00
self.as_mut()
2019-12-21 00:54:43 +03:00
.project()
.stream
2019-12-18 22:57:22 +03:00
.poll_next(cx)
.map(|opt| opt.map(|x| (self.as_mut().project().f)(x)))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.stream.size_hint()
}
}