aboutsummaryrefslogtreecommitdiff
path: root/src/stream/stream/split.rs
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2024-02-01 15:12:47 +0100
committerJeff Vander Stoep <jeffv@google.com>2024-02-01 15:12:48 +0100
commit41e437e55b496ba9779e4e5767abe63c77eb42bf (patch)
tree1d802034c6a30b7b67341a1da3bb5cdbea1b6956 /src/stream/stream/split.rs
parent37d065cc93c720c4c2d100385b6d872106bf5a9e (diff)
downloadfutures-util-41e437e55b496ba9779e4e5767abe63c77eb42bf.tar.gz
Upgrade futures-util to 0.3.30
This project was upgraded with external_updater. Usage: tools/external_updater/updater.sh update external/rust/crates/futures-util For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md Test: TreeHugger Change-Id: I4d01cd43e35cccd70ff58f1669ac3697035e8c60
Diffstat (limited to 'src/stream/stream/split.rs')
-rw-r--r--src/stream/stream/split.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/stream/stream/split.rs b/src/stream/stream/split.rs
index e2034e0..1a7fdcb 100644
--- a/src/stream/stream/split.rs
+++ b/src/stream/stream/split.rs
@@ -15,6 +15,13 @@ pub struct SplitStream<S>(BiLock<S>);
impl<S> Unpin for SplitStream<S> {}
+impl<S> SplitStream<S> {
+ /// Returns `true` if the `SplitStream<S>` and `SplitSink<S>` originate from the same call to `StreamExt::split`.
+ pub fn is_pair_of<Item>(&self, other: &SplitSink<S, Item>) -> bool {
+ other.is_pair_of(&self)
+ }
+}
+
impl<S: Unpin> SplitStream<S> {
/// Attempts to put the two "halves" of a split `Stream + Sink` back
/// together. Succeeds only if the `SplitStream<S>` and `SplitSink<S>` are
@@ -60,6 +67,13 @@ impl<S: Sink<Item> + Unpin, Item> SplitSink<S, Item> {
}
}
+impl<S, Item> SplitSink<S, Item> {
+ /// Returns `true` if the `SplitStream<S>` and `SplitSink<S>` originate from the same call to `StreamExt::split`.
+ pub fn is_pair_of(&self, other: &SplitStream<S>) -> bool {
+ self.lock.is_pair_of(&other.0)
+ }
+}
+
impl<S: Sink<Item>, Item> SplitSink<S, Item> {
fn poll_flush_slot(
mut inner: Pin<&mut S>,
@@ -142,3 +156,69 @@ impl<T, Item> fmt::Display for ReuniteError<T, Item> {
#[cfg(feature = "std")]
impl<T: core::any::Any, Item> std::error::Error for ReuniteError<T, Item> {}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{sink::Sink, stream::StreamExt};
+ use core::marker::PhantomData;
+
+ struct NopStream<Item> {
+ phantom: PhantomData<Item>,
+ }
+
+ impl<Item> Stream for NopStream<Item> {
+ type Item = Item;
+
+ fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
+ todo!()
+ }
+ }
+
+ impl<Item> Sink<Item> for NopStream<Item> {
+ type Error = ();
+
+ fn poll_ready(
+ self: Pin<&mut Self>,
+ _cx: &mut Context<'_>,
+ ) -> Poll<Result<(), Self::Error>> {
+ todo!()
+ }
+
+ fn start_send(self: Pin<&mut Self>, _item: Item) -> Result<(), Self::Error> {
+ todo!()
+ }
+
+ fn poll_flush(
+ self: Pin<&mut Self>,
+ _cx: &mut Context<'_>,
+ ) -> Poll<Result<(), Self::Error>> {
+ todo!()
+ }
+
+ fn poll_close(
+ self: Pin<&mut Self>,
+ _cx: &mut Context<'_>,
+ ) -> Poll<Result<(), Self::Error>> {
+ todo!()
+ }
+ }
+
+ #[test]
+ fn test_pairing() {
+ let s1 = NopStream::<()> { phantom: PhantomData };
+ let (sink1, stream1) = s1.split();
+ assert!(sink1.is_pair_of(&stream1));
+ assert!(stream1.is_pair_of(&sink1));
+
+ let s2 = NopStream::<()> { phantom: PhantomData };
+ let (sink2, stream2) = s2.split();
+ assert!(sink2.is_pair_of(&stream2));
+ assert!(stream2.is_pair_of(&sink2));
+
+ assert!(!sink1.is_pair_of(&stream2));
+ assert!(!stream1.is_pair_of(&sink2));
+ assert!(!sink2.is_pair_of(&stream1));
+ assert!(!stream2.is_pair_of(&sink1));
+ }
+}