1use core::pin::Pin;
2
3use pin_project_lite::pin_project;
4
5use crate::stream::Stream;
6use crate::task::{Context, Poll};
7
8#[cfg(feature = "unstable")]
9use crate::stream::DoubleEndedStream;
10
11pub fn once<T>(t: T) -> Once<T> {
29 Once { value: Some(t) }
30}
31
32pin_project! {
33 #[derive(Clone, Debug)]
40 pub struct Once<T> {
41 value: Option<T>,
42 }
43}
44
45impl<T> Stream for Once<T> {
46 type Item = T;
47
48 fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<T>> {
49 Poll::Ready(self.project().value.take())
50 }
51}
52
53#[cfg(feature = "unstable")]
54impl <T> DoubleEndedStream for Once<T> {
55 fn poll_next_back(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
56 Poll::Ready(self.project().value.take())
57 }
58}