pub trait StreamDistinctUntilChangedExt: Stream + Sized {
// Provided methods
fn distinct_until_changed(self) -> DistinctUntilChangedStream<Self>
where Self::Item: PartialEq + Clone { ... }
fn distinct_until_changed_by<F>(
self,
compare: F,
) -> DistinctUntilChangedByStream<Self, F>
where Self::Item: Clone,
F: FnMut(&Self::Item, &Self::Item) -> bool { ... }
}Expand description
Extension trait that adds .distinct_until_changed() to streams.
Provided Methods§
Sourcefn distinct_until_changed(self) -> DistinctUntilChangedStream<Self>
fn distinct_until_changed(self) -> DistinctUntilChangedStream<Self>
Wraps this stream so that only values different from the previous value are yielded.
This filters out consecutive duplicate values, only emitting items when they
differ from the immediately preceding item. Uses PartialEq for comparison.
§Example
use futures::StreamExt;
use futures::executor::block_on;
use streamx::StreamDistinctUntilChangedExt;
let mut stream = futures::stream::iter([1, 1, 2, 2, 3, 1, 1]).distinct_until_changed();
assert_eq!(block_on(async { stream.next().await }), Some(1));
assert_eq!(block_on(async { stream.next().await }), Some(2));
assert_eq!(block_on(async { stream.next().await }), Some(3));
assert_eq!(block_on(async { stream.next().await }), Some(1));
assert_eq!(block_on(async { stream.next().await }), None);Sourcefn distinct_until_changed_by<F>(
self,
compare: F,
) -> DistinctUntilChangedByStream<Self, F>
fn distinct_until_changed_by<F>( self, compare: F, ) -> DistinctUntilChangedByStream<Self, F>
Like distinct_until_changed(), but uses a custom comparison function.
The comparison function should return true if two items are considered equal
(and the second should be skipped), or false if they are different (and the
second should be emitted).
§Example
use futures::StreamExt;
use futures::executor::block_on;
use streamx::StreamDistinctUntilChangedExt;
let mut stream = futures::stream::iter([1, 2, 3, 4, 5])
.distinct_until_changed_by(|a, b| a % 2 == b % 2);
// Only emits when parity changes
assert_eq!(block_on(async { stream.next().await }), Some(1));
assert_eq!(block_on(async { stream.next().await }), Some(2));
assert_eq!(block_on(async { stream.next().await }), Some(3));
assert_eq!(block_on(async { stream.next().await }), Some(4));
assert_eq!(block_on(async { stream.next().await }), Some(5));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.