From 057bbef5f37a4051615ad23661a0b4853b61162e Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Thu, 19 Jun 2025 00:49:49 -0400 Subject: Support querying event sequences via iterators or streams. These filters are meant to be used with, respectively, `Iterator::filter_map` and `StreamExt::filter_map`. The two operations are conceptually the same - they pass an item from the underlying sequence to a function that returns an option, drops the values for which the function returns `None`, and yields the value inside of `Some` in the resulting sequence. However, `Iterator::filter_map` takes a function from the iterator elements to `Option`. `StreamExt::filter_map` takes a function from the iterator elements to _a `Future` whose output is `Option`_. As such, you can't easily use functions designed for one use case, for the other. You need an adapter - conventionally, `futures::ready`, if you have a non-async function and need an async one. This provides two sets of sequence filters: * `crate::test::fixtures::event` contains functions which return `Option` directly, and which are intended for use with `Iterator::filter_map`. * `crate::test::fixtures::event::stream` contains lifted versions that return a `Future`, and which are intended for use with `StreamExt::filter_map`. The lifting is done purely manually. I spent a lot of time writing clever-er versions before deciding on this; those were fun to write, but hell to read and not meaningfully better, and this is test support code, so we want it to be dumb and obvious. Complexity for the sake of intellectual satisfaction is a huge antifeature in this context. --- src/test/fixtures/event/stream.rs | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/fixtures/event/stream.rs (limited to 'src/test/fixtures/event/stream.rs') diff --git a/src/test/fixtures/event/stream.rs b/src/test/fixtures/event/stream.rs new file mode 100644 index 0000000..6c2a1bf --- /dev/null +++ b/src/test/fixtures/event/stream.rs @@ -0,0 +1,62 @@ +use std::future::{self, Ready}; + +use crate::{event::Event, test::fixtures::event}; + +pub fn channel(event: Event) -> Ready> { + future::ready(event::channel(event)) +} + +pub fn message(event: Event) -> Ready> { + future::ready(event::message(event)) +} + +pub fn user(event: Event) -> Ready> { + future::ready(event::user(event)) +} + +pub mod channel { + use std::future::{self, Ready}; + + use crate::{ + channel::{Event, event}, + test::fixtures::event::channel, + }; + + pub fn created(event: Event) -> Ready> { + future::ready(channel::created(event)) + } + + pub fn deleted(event: Event) -> Ready> { + future::ready(channel::deleted(event)) + } +} + +pub mod message { + use std::future::{self, Ready}; + + use crate::{ + message::{Event, event}, + test::fixtures::event::message, + }; + + pub fn sent(event: Event) -> Ready> { + future::ready(message::sent(event)) + } + + pub fn deleted(event: Event) -> future::Ready> { + future::ready(message::deleted(event)) + } +} + +pub mod user { + use std::future::{self, Ready}; + + use crate::{ + test::fixtures::event::user, + user::{Event, event}, + }; + + pub fn created(event: Event) -> Ready> { + future::ready(user::created(event)) + } +} -- cgit v1.2.3