use std::{ future::Future, pin::Pin, task::{Context, Poll}, }; use futures::{stream, StreamExt}; pub struct JoinHandle(T); impl Future for JoinHandle { type Output = Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { loop {} } } pub fn spawn(future: T) -> JoinHandle { loop {} } pub trait Stream { /// Values yielded by the stream. type Item; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>; #[inline] fn size_hint(&self) -> (usize, Option) { (0, None) } } pub struct Iter { iter: I, } impl Unpin for Iter {} pub(crate) fn assert_stream(stream: S) -> S where S: Stream, { stream } pub fn iter(i: I) -> Iter where I: IntoIterator, { assert_stream::(Iter { iter: i.into_iter(), }) } impl Stream for Iter where I: Iterator, { type Item = I::Item; fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { Poll::Ready(self.iter.next()) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl Stream for &S { type Item = S::Item; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { todo!() } } impl Stream for &mut S { type Item = S::Item; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { todo!() } } trait UwuStream: Stream { fn map(self, f: F) -> Map where F: FnMut(Self::Item) -> T, Self: Sized, { loop {} } fn buffer_unordered(self, n: impl Into>) -> BufferUnordered where Self::Item: Future, Self: Sized, { loop {} } } impl UwuStream for S {} pub struct Map { stream: St, f: F, } impl Stream for Map where St: Stream, F: FnMut(St::Item), { type Item = F::Output; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { loop {} } fn size_hint(&self) -> (usize, Option) { self.stream.size_hint() } } pub struct BufferUnordered where St: Stream, { uwu: St, } impl Stream for BufferUnordered where St: Stream, St::Item: Future, { type Item = ::Output; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { loop {} } fn size_hint(&self) -> (usize, Option) { loop {} } } fn main() { let bodies = iter([]) .map(|url: String| spawn(async { Result::Ok(url) })) .buffer_unordered(0); bodies.for_each(|b| async { match b { Ok(Ok(url)) => {} Err(e) => {} Ok(Err(e)) => {} } }); }