Skip to content

Commit d8fd40c

Browse files
committed
feat(io): add unsplit
1 parent c5e3497 commit d8fd40c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

compio-io/src/split.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ pub fn split<T: AsyncRead + AsyncWrite>(stream: T) -> (ReadHalf<T>, WriteHalf<T>
1616
#[derive(Debug)]
1717
pub struct ReadHalf<T>(Arc<Mutex<T>>);
1818

19+
impl<T> ReadHalf<T> {
20+
/// Reunites with a previously split [`WriteHalf`].
21+
///
22+
/// # Panics
23+
///
24+
/// If this [`ReadHalf`] and the given [`WriteHalf`] do not originate from
25+
/// the same [`split`] operation this method will panic.
26+
/// This can be checked ahead of time by comparing the stored pointer
27+
/// of the two halves.
28+
#[track_caller]
29+
pub fn unsplit(self, w: WriteHalf<T>) -> T
30+
where
31+
T: Unpin,
32+
{
33+
if Arc::ptr_eq(&self.0, &w.0) {
34+
drop(w);
35+
let inner = Arc::try_unwrap(self.0).expect("`Arc::try_unwrap` failed");
36+
inner.into_inner()
37+
} else {
38+
#[cold]
39+
fn panic_unrelated() -> ! {
40+
panic!("Unrelated `WriteHalf` passed to `ReadHalf::unsplit`.")
41+
}
42+
43+
panic_unrelated()
44+
}
45+
}
46+
}
47+
1948
impl<T: AsyncRead> AsyncRead for ReadHalf<T> {
2049
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
2150
self.0.lock().await.read(buf).await

0 commit comments

Comments
 (0)