Skip to content

Commit

Permalink
realm-io: allow underlying buffer without ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 27, 2024
1 parent 19c891a commit 314dea9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions realm_io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
//! // zero copy
//! bidi_zero_copy(&mut left, &mut right).await;
//!
//! // use custom buffer(vector)
//! // use custom buffer(AsMut<[u8]>)
//! let buf1 = CopyBuffer::new(vec![0; 0x2000]);
//! let buf2 = CopyBuffer::new(vec![0; 0x2000]);
//! bidi_copy_buf(&mut left, &mut right, buf1, buf2).await;
//!
//! // use custom buffer(pipe)
//! // use custom buffer(Pipe or &mut Pipe)
//! let buf1 = CopyBuffer::new(Pipe::new().unwrap());
//! let buf2 = CopyBuffer::new(Pipe::new().unwrap());
//! bidi_copy_buf(&mut left, &mut right, buf1, buf2).await;
Expand Down
21 changes: 21 additions & 0 deletions realm_io/src/linux/zero_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ where
}
}

impl<'a, SR, SW> AsyncIOBuf for CopyBuffer<&'a mut Pipe, SR, SW>
where
SR: AsyncRead + AsyncWrite + AsyncRawIO + Unpin,
SW: AsyncRead + AsyncWrite + AsyncRawIO + Unpin,
{
type StreamR = SR;
type StreamW = SW;

fn poll_read_buf(&mut self, cx: &mut Context<'_>, stream: &mut Self::StreamR) -> Poll<Result<usize>> {
stream.poll_read_raw(cx, || splice_n(stream.as_raw_fd(), self.buf.1, usize::MAX))
}

fn poll_write_buf(&mut self, cx: &mut Context<'_>, stream: &mut Self::StreamW) -> Poll<Result<usize>> {
stream.poll_write_raw(cx, || splice_n(self.buf.0, stream.as_raw_fd(), self.cap - self.pos))
}

fn poll_flush_buf(&mut self, cx: &mut Context<'_>, stream: &mut Self::StreamW) -> Poll<Result<()>> {
Pin::new(stream).poll_flush(cx)
}
}

/// Copy data bidirectionally between two streams with pipe.
pub async fn bidi_zero_copy<A, B>(a: &mut A, b: &mut B) -> Result<(u64, u64)>
where
Expand Down

0 comments on commit 314dea9

Please sign in to comment.