From 314dea901853befedbcee387b3ea77dc967b6775 Mon Sep 17 00:00:00 2001 From: zephyr Date: Sat, 27 Apr 2024 15:03:43 +0900 Subject: [PATCH] realm-io: allow underlying buffer without ownership --- realm_io/src/lib.rs | 4 ++-- realm_io/src/linux/zero_copy.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/realm_io/src/lib.rs b/realm_io/src/lib.rs index f6fb10b5..0167ad2d 100644 --- a/realm_io/src/lib.rs +++ b/realm_io/src/lib.rs @@ -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; diff --git a/realm_io/src/linux/zero_copy.rs b/realm_io/src/linux/zero_copy.rs index 780d727a..dd2c822c 100644 --- a/realm_io/src/linux/zero_copy.rs +++ b/realm_io/src/linux/zero_copy.rs @@ -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> { + 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> { + 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> { + Pin::new(stream).poll_flush(cx) + } +} + /// Copy data bidirectionally between two streams with pipe. pub async fn bidi_zero_copy(a: &mut A, b: &mut B) -> Result<(u64, u64)> where