From d6bf3019f9c3a45dcc8fb95eff65ab071926a2b3 Mon Sep 17 00:00:00 2001 From: Daniel Salinas Date: Fri, 23 May 2025 10:26:29 -0400 Subject: [PATCH] StreamExt that supports wasm platforms The .boxed() method requires a Send trait that makes it unusable on Wasm platforms. This re-exports the existing StreamExt, while providing a new extension for the wasm family of targets. --- crates/matrix-sdk-common/src/lib.rs | 1 + crates/matrix-sdk-common/src/stream.rs | 57 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 crates/matrix-sdk-common/src/stream.rs diff --git a/crates/matrix-sdk-common/src/lib.rs b/crates/matrix-sdk-common/src/lib.rs index 1548d4ebd..9e6696b6f 100644 --- a/crates/matrix-sdk-common/src/lib.rs +++ b/crates/matrix-sdk-common/src/lib.rs @@ -30,6 +30,7 @@ pub mod locks; pub mod ring_buffer; pub mod sleep; pub mod store_locks; +pub mod stream; pub mod timeout; pub mod tracing_timer; pub mod ttl_cache; diff --git a/crates/matrix-sdk-common/src/stream.rs b/crates/matrix-sdk-common/src/stream.rs new file mode 100644 index 000000000..3a613d0be --- /dev/null +++ b/crates/matrix-sdk-common/src/stream.rs @@ -0,0 +1,57 @@ +// Copyright 2025 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Platform-specific stream utilities. +//! +//! This module provides a unified BoxStream + StreamExt class for working +//! with boxed streams across different platforms. On native platforms, +//! streams can be `Send`, but on Wasm they cannot. This module abstracts +//! over that difference. + +#[cfg(not(target_family = "wasm"))] +mod sys { + // On native platforms, just re-export everything from futures_util + pub use futures_util::{stream::BoxStream, StreamExt}; +} + +#[cfg(target_family = "wasm")] +mod sys { + use futures_core::Stream; + // On Wasm, BoxStream is LocalBoxStream + pub use futures_util::stream::LocalBoxStream as BoxStream; + + /// Custom StreamExt trait for Wasm that provides essential methods + /// like `.boxed()` and `.next()` without Send requirements. + pub trait StreamExt: Stream { + /// Box this stream using LocalBoxStream (no Send requirement). + fn boxed<'a>(self) -> BoxStream<'a, Self::Item> + where + Self: Sized + 'a, + { + futures_util::StreamExt::boxed_local(self) + } + + /// Get the next item from this stream. + fn next(&mut self) -> futures_util::stream::Next<'_, Self> + where + Self: Unpin, + { + futures_util::StreamExt::next(self) + } + } + + impl StreamExt for S {} +} + +pub use sys::*;