From 17c5b429213f5c0f2b2ceee5c7690399ddb4e194 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 20 Nov 2022 21:44:08 +0100 Subject: [PATCH] lol no futures --- Cargo.lock | 103 --------------------- Cargo.toml | 5 - futures-rs | 1 - src/main.rs | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 258 insertions(+), 112 deletions(-) delete mode 160000 futures-rs diff --git a/Cargo.lock b/Cargo.lock index 393b04b..45b34c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,109 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - -[[package]] -name = "futures" -version = "0.4.0-alpha.0" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-core" -version = "1.0.0-alpha.0" - -[[package]] -name = "futures-macro" -version = "0.4.0-alpha.0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "futures-task" -version = "0.4.0-alpha.0" - -[[package]] -name = "futures-util" -version = "0.4.0-alpha.0" -dependencies = [ - "futures-core", - "futures-macro", - "futures-task", - "pin-project-lite", - "pin-utils", - "slab", -] - [[package]] name = "ice-104649" version = "0.1.0" -dependencies = [ - "futures", -] - -[[package]] -name = "pin-project-lite" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - -[[package]] -name = "proc-macro2" -version = "1.0.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "slab" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef" -dependencies = [ - "autocfg", -] - -[[package]] -name = "syn" -version = "1.0.103" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" diff --git a/Cargo.toml b/Cargo.toml index 7e7d24c..e741d80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,8 +2,3 @@ name = "ice-104649" version = "0.1.0" edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -futures = { path = "./futures-rs/futures" } diff --git a/futures-rs b/futures-rs deleted file mode 160000 index 845a773..0000000 --- a/futures-rs +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 845a77360cdd3af189507eb333835bfefe4703b1 diff --git a/src/main.rs b/src/main.rs index 7905be0..41565ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,6 @@ use std::{ task::{Context, Poll}, }; -use futures::{stream, StreamExt}; - pub struct JoinHandle(T); impl Future for JoinHandle { @@ -20,8 +18,265 @@ pub fn spawn(future: T) -> JoinHandle { loop {} } +use fut::StreamExt; +mod fut { + use std::{ + future::Future, + num::NonZeroUsize, + ops::DerefMut, + pin::Pin, + task::{Context, Poll}, + }; + + #[must_use = "streams do nothing unless polled"] + pub trait Stream { + type Item; + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>; + #[inline] + fn size_hint(&self) -> (usize, Option) { + (0, None) + } + } + + impl Stream for &mut S { + type Item = S::Item; + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + S::poll_next(Pin::new(&mut **self), cx) + } + + fn size_hint(&self) -> (usize, Option) { + (**self).size_hint() + } + } + + impl

Stream for Pin

+ where + P: DerefMut + Unpin, + P::Target: Stream, + { + type Item = ::Item; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.get_mut().as_mut().poll_next(cx) + } + + fn size_hint(&self) -> (usize, Option) { + (**self).size_hint() + } + } + + #[derive(Debug, Clone)] + #[must_use = "streams do nothing unless polled"] + pub struct Iter { + iter: I, + } + + impl Unpin for Iter {} + + pub fn iter(i: I) -> Iter + where + I: IntoIterator, + { + 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 StreamExt for T where T: Stream {} + + fn assert_future(t: T) -> T { + t + } + + pub trait StreamExt: Stream { + fn map(self, f: F) -> Map + where + F: FnMut(Self::Item) -> T, + Self: Sized, + { + Map::new(self, f) + } + + fn for_each(self, f: F) -> ForEach + where + F: FnMut(Self::Item) -> Fut, + Fut: Future, + Self: Sized, + { + assert_future::<(), _>(ForEach::new(self, f)) + } + + fn buffer_unordered(self, n: impl Into>) -> BufferUnordered + where + Self::Item: Future, + Self: Sized, + { + BufferUnordered::new(self, n.into()) + } + } + + #[must_use = "streams do nothing unless polled"] + pub struct Map { + stream: St, + f: F, + } + + impl Map { + pub(crate) fn new(stream: St, f: F) -> Self { + Self { stream, 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 trait FnOnce1 { + type Output; + fn call_once(self, arg: A) -> Self::Output; + } + + impl FnOnce1 for T + where + T: FnOnce(A) -> R, + { + type Output = R; + fn call_once(self, arg: A) -> R { + self(arg) + } + } + + pub trait FnMut1: FnOnce1 { + fn call_mut(&mut self, arg: A) -> Self::Output; + } + + impl FnMut1 for T + where + T: FnMut(A) -> R, + { + fn call_mut(&mut self, arg: A) -> R { + self(arg) + } + } + + impl Stream for &Map + where + St: Stream, + F: FnMut1, + { + 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 ForEach { + stream: St, + f: F, + future: Option, + } + + impl ForEach + where + St: Stream, + F: FnMut(St::Item) -> Fut, + Fut: Future, + { + pub(super) fn new(stream: St, f: F) -> Self { + Self { + stream, + f, + future: None, + } + } + } + + impl Future for ForEach + where + St: Stream, + F: FnMut(St::Item) -> Fut, + Fut: Future, + { + type Output = (); + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { + loop {} + } + } + + pub struct BufferUnordered + where + St: Stream, + { + stream: St, + max: Option, + } + + impl BufferUnordered + where + St: Stream, + St::Item: Future, + { + pub(super) fn new(stream: St, n: Option) -> Self { + Self { + stream: stream, + max: n.and_then(NonZeroUsize::new), + } + } + } + + 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 = stream::iter([]) + let bodies = fut::iter([]) .map(|url: String| spawn(async { Result::Ok(url) })) .buffer_unordered(0);