1 use crate::stream::Stream; 2 3 use core::fmt; 4 use core::pin::Pin; 5 use core::task::{Context, Poll}; 6 use pin_project_lite::pin_project; 7 8 pin_project! { 9 /// Stream for the [`map`](super::StreamExt::map) method. 10 #[must_use = "streams do nothing unless polled"] 11 pub struct Map<St, F> { 12 #[pin] 13 stream: St, 14 f: F, 15 } 16 } 17 18 impl<St, F> fmt::Debug for Map<St, F> 19 where 20 St: fmt::Debug, 21 { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 23 f.debug_struct("Map").field("stream", &self.stream).finish() 24 } 25 } 26 27 impl<St, F> Map<St, F> { new(stream: St, f: F) -> Self28 pub(super) fn new(stream: St, f: F) -> Self { 29 Map { stream, f } 30 } 31 } 32 33 impl<St, F, T> Stream for Map<St, F> 34 where 35 St: Stream, 36 F: FnMut(St::Item) -> T, 37 { 38 type Item = T; 39 poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>>40 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> { 41 self.as_mut() 42 .project() 43 .stream 44 .poll_next(cx) 45 .map(|opt| opt.map(|x| (self.as_mut().project().f)(x))) 46 } 47 size_hint(&self) -> (usize, Option<usize>)48 fn size_hint(&self) -> (usize, Option<usize>) { 49 self.stream.size_hint() 50 } 51 } 52