matrix_sdk_common/stream.rs
1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Platform-specific stream utilities.
16//!
17//! This module provides a unified `BoxStream` + `StreamExt` class for working
18//! with boxed streams across different platforms. On native platforms,
19//! streams can be `Send`, but on Wasm they cannot. This module abstracts
20//! over that difference.
21
22#[cfg(not(target_family = "wasm"))]
23mod sys {
24 // On native platforms, just re-export everything from futures_util
25 pub use futures_util::{StreamExt, stream::BoxStream};
26}
27
28#[cfg(target_family = "wasm")]
29mod sys {
30 use futures_core::Stream;
31 // On Wasm, BoxStream is LocalBoxStream
32 pub use futures_util::stream::LocalBoxStream as BoxStream;
33
34 /// Custom `StreamExt` trait for Wasm that provides essential methods
35 /// like `.boxed()` and `.next()` without `Send` requirements.
36 pub trait StreamExt: Stream {
37 /// Box this stream using `LocalBoxStream` (no `Send` requirement).
38 fn boxed<'a>(self) -> BoxStream<'a, Self::Item>
39 where
40 Self: Sized + 'a,
41 {
42 futures_util::StreamExt::boxed_local(self)
43 }
44
45 /// Get the next item from this stream.
46 fn next(&mut self) -> futures_util::stream::Next<'_, Self>
47 where
48 Self: Unpin,
49 {
50 futures_util::StreamExt::next(self)
51 }
52 }
53
54 impl<S: Stream> StreamExt for S {}
55}
56
57pub use sys::*;