Skip to content

Commit 845e01e

Browse files
committed
PR review: rename the weirdly named ...Specific traits
hopefully this makes it more clear what they are for!
1 parent 6ceacfd commit 845e01e

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

examples/compression.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ mod lz4 {
6060
use async_compression::tokio::{bufread::Lz4Decoder, write::Lz4Encoder};
6161
use iroh::endpoint::VarInt;
6262
use iroh_blobs::util::{
63-
AsyncReadRecvStream, AsyncWriteSendStream, RecvStreamSpecific, SendStreamSpecific,
63+
AsyncReadRecvStream, AsyncReadRecvStreamExtra, AsyncWriteSendStream,
64+
AsyncWriteSendStreamExtra,
6465
};
6566
use tokio::io::{AsyncRead, AsyncWrite, BufReader};
6667

@@ -72,7 +73,7 @@ mod lz4 {
7273
}
7374
}
7475

75-
impl SendStreamSpecific for SendStream {
76+
impl AsyncWriteSendStreamExtra for SendStream {
7677
fn inner(&mut self) -> &mut (impl AsyncWrite + Unpin + Send) {
7778
&mut self.0
7879
}
@@ -84,6 +85,10 @@ mod lz4 {
8485
async fn stopped(&mut self) -> io::Result<Option<VarInt>> {
8586
Ok(self.0.get_mut().stopped().await?)
8687
}
88+
89+
fn id(&self) -> u64 {
90+
self.0.get_ref().id().index()
91+
}
8792
}
8893

8994
struct RecvStream(Lz4Decoder<BufReader<iroh::endpoint::RecvStream>>);
@@ -94,7 +99,7 @@ mod lz4 {
9499
}
95100
}
96101

97-
impl RecvStreamSpecific for RecvStream {
102+
impl AsyncReadRecvStreamExtra for RecvStream {
98103
fn inner(&mut self) -> &mut (impl AsyncRead + Unpin + Send) {
99104
&mut self.0
100105
}

src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub mod connection_pool;
44
mod stream;
55
pub(crate) mod temp_tag;
66
pub use stream::{
7-
AsyncReadRecvStream, AsyncWriteSendStream, RecvStream, RecvStreamAsyncStreamReader,
8-
RecvStreamSpecific, SendStream, SendStreamSpecific,
7+
AsyncReadRecvStream, AsyncReadRecvStreamExtra, AsyncWriteSendStream, AsyncWriteSendStreamExtra,
8+
RecvStream, RecvStreamAsyncStreamReader, SendStream,
99
};
1010
pub(crate) use stream::{RecvStreamExt, SendStreamExt};
1111

src/util/stream.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,22 @@ impl<W: SendStream> SendStream for &mut W {
170170
#[derive(Debug)]
171171
pub struct AsyncReadRecvStream<R>(R);
172172

173+
/// This is a helper trait to work with [`AsyncReadRecvStream`]. If you have an
174+
/// `AsyncRead + Unpin + Send`, you can implement these additional methods and wrap the result
175+
/// in an `AsyncReadRecvStream` to get a `RecvStream` that reads from the underlying `AsyncRead`.
176+
pub trait AsyncReadRecvStreamExtra: Send {
177+
fn inner(&mut self) -> &mut (impl AsyncRead + Unpin + Send);
178+
fn stop(&mut self, code: VarInt) -> io::Result<()>;
179+
fn id(&self) -> u64;
180+
}
181+
173182
impl<R> AsyncReadRecvStream<R> {
174183
pub fn new(inner: R) -> Self {
175184
Self(inner)
176185
}
177186
}
178187

179-
impl<R: RecvStreamSpecific> RecvStream for AsyncReadRecvStream<R> {
188+
impl<R: AsyncReadRecvStreamExtra> RecvStream for AsyncReadRecvStream<R> {
180189
async fn recv_bytes(&mut self, len: usize) -> io::Result<Bytes> {
181190
let mut res = vec![0; len];
182191
let mut n = 0;
@@ -215,18 +224,6 @@ impl<R: RecvStreamSpecific> RecvStream for AsyncReadRecvStream<R> {
215224
}
216225
}
217226

218-
pub trait RecvStreamSpecific: Send {
219-
fn inner(&mut self) -> &mut (impl AsyncRead + Unpin + Send);
220-
fn stop(&mut self, code: VarInt) -> io::Result<()>;
221-
fn id(&self) -> u64;
222-
}
223-
224-
pub trait SendStreamSpecific: Send {
225-
fn inner(&mut self) -> &mut (impl AsyncWrite + Unpin + Send);
226-
fn reset(&mut self, code: VarInt) -> io::Result<()>;
227-
fn stopped(&mut self) -> impl Future<Output = io::Result<Option<VarInt>>> + Send;
228-
}
229-
230227
impl RecvStream for Bytes {
231228
async fn recv_bytes(&mut self, len: usize) -> io::Result<Bytes> {
232229
let n = len.min(self.len());
@@ -267,19 +264,31 @@ impl RecvStream for Bytes {
267264
#[derive(Debug, Clone)]
268265
pub struct AsyncWriteSendStream<W>(W);
269266

270-
impl<W: SendStreamSpecific> AsyncWriteSendStream<W> {
267+
/// This is a helper trait to work with [`AsyncWriteSendStream`].
268+
///
269+
/// If you have an `AsyncWrite + Unpin + Send`, you can implement these additional
270+
/// methods and wrap the result in an `AsyncWriteSendStream` to get a `SendStream`
271+
/// that writes to the underlying `AsyncWrite`.
272+
pub trait AsyncWriteSendStreamExtra: Send {
273+
fn inner(&mut self) -> &mut (impl AsyncWrite + Unpin + Send);
274+
fn reset(&mut self, code: VarInt) -> io::Result<()>;
275+
fn stopped(&mut self) -> impl Future<Output = io::Result<Option<VarInt>>> + Send;
276+
fn id(&self) -> u64;
277+
}
278+
279+
impl<W: AsyncWriteSendStreamExtra> AsyncWriteSendStream<W> {
271280
pub fn new(inner: W) -> Self {
272281
Self(inner)
273282
}
274283
}
275284

276-
impl<W: SendStreamSpecific> AsyncWriteSendStream<W> {
285+
impl<W: AsyncWriteSendStreamExtra> AsyncWriteSendStream<W> {
277286
pub fn into_inner(self) -> W {
278287
self.0
279288
}
280289
}
281290

282-
impl<W: SendStreamSpecific> SendStream for AsyncWriteSendStream<W> {
291+
impl<W: AsyncWriteSendStreamExtra> SendStream for AsyncWriteSendStream<W> {
283292
async fn send_bytes(&mut self, bytes: Bytes) -> io::Result<()> {
284293
self.0.inner().write_all(&bytes).await
285294
}
@@ -303,7 +312,7 @@ impl<W: SendStreamSpecific> SendStream for AsyncWriteSendStream<W> {
303312
}
304313

305314
fn id(&self) -> u64 {
306-
0
315+
self.0.id()
307316
}
308317
}
309318

0 commit comments

Comments
 (0)