From 168b95493c2a397b77b5ee32f60f12c2b2f2a21c Mon Sep 17 00:00:00 2001 From: Jeron Aldaron Lau Date: Tue, 5 Nov 2024 15:39:12 -0600 Subject: [PATCH] Prepare v0.2.0 --- Cargo.toml | 2 +- src/io/destination.rs | 14 ++++-------- src/io/seek.rs | 4 +--- src/io/sender.rs | 50 +++++++++++++++++++++++++++++++++++++++---- src/io/source.rs | 10 ++++----- src/io/truncate.rs | 4 +--- 6 files changed, 57 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2c09497..f7455fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "parsenic" -version = "0.1.0" +version = "0.2.0" edition = "2021" license = "Apache-2.0 OR BSL-1.0 OR MIT" description = "A simple no-std/no-alloc I/O and parsing crate" diff --git a/src/io/destination.rs b/src/io/destination.rs index 91c361a..e18948b 100644 --- a/src/io/destination.rs +++ b/src/io/destination.rs @@ -36,20 +36,14 @@ where impl Destination for Pin where - // FIXME: Can relax `Unpin` bounds after - // https://github.com/rust-lang/rust/issues/86918 - D: DerefMut + Unpin, - T: Destination + Unpin, + D: DerefMut, + T: Destination, { fn poll_send( - mut self: Pin<&mut Self>, + self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { - ::poll_send( - Pin::new(self.as_mut().get_mut()), - cx, - buf, - ) + ::poll_send(self.as_deref_mut(), cx, buf) } } diff --git a/src/io/seek.rs b/src/io/seek.rs index 24809df..762ae13 100644 --- a/src/io/seek.rs +++ b/src/io/seek.rs @@ -57,9 +57,7 @@ where impl Seek for Pin where - // FIXME: Can relax `Unpin` bounds after - // https://github.com/rust-lang/rust/issues/86918 - S: DerefMut + Unpin, + S: DerefMut, T: Seek + Unpin, { fn seek(&mut self, pos: u64) { diff --git a/src/io/sender.rs b/src/io/sender.rs index 5054d8b..d06ff66 100644 --- a/src/io/sender.rs +++ b/src/io/sender.rs @@ -1,6 +1,9 @@ +use core::{future, pin::Pin}; + use crate::{ + error::{FlushError, FullError}, io::{Destination, Seek, Truncate}, - result::FlushResult, + result::{FlushResult, LostResult}, }; /// [`slice`] buffered sender. @@ -38,13 +41,52 @@ where /// /// May not send the full amount of bytes until either the buffer is full or /// [`flush()`](Self::flush()) is called. - pub async fn send(&mut self, bytes: &[u8]) -> FlushResult { - todo!("{bytes:?}") // FIXME + pub async fn send(&mut self, bytes: &[u8]) -> LostResult { + let mut total_sent = 0; + + for byte in bytes.iter().cloned() { + if self.cursor == BUF { + self.cursor = 0; + + let sent = future::poll_fn(|cx| { + Pin::new(&mut self.destination) + .poll_send(cx, self.buffer.as_ref()) + }) + .await?; + + total_sent += sent; + + if sent != BUF { + return Ok(total_sent); + } + } + + self.buffer[self.cursor] = byte; + self.cursor += 1; + } + + Ok(total_sent) } /// Send buffered data with the destination. pub async fn flush(&mut self) -> FlushResult { - todo!() // FIXME + let old_cursor = self.cursor; + let sent = future::poll_fn(|cx| { + Pin::new(&mut self.destination) + .poll_send(cx, &self.buffer[..self.cursor]) + }) + .await?; + + self.cursor -= sent; + + if self.cursor != 0 { + self.buffer.copy_within(sent..old_cursor, 0); + return Err(FlushError::Full(FullError::from_remaining( + self.cursor, + ))); + } + + Ok(()) } } diff --git a/src/io/source.rs b/src/io/source.rs index a38bafc..aefaa27 100644 --- a/src/io/source.rs +++ b/src/io/source.rs @@ -36,16 +36,14 @@ where impl Source for Pin where - // FIXME: Can relax `Unpin` bounds after - // https://github.com/rust-lang/rust/issues/86918 - S: DerefMut + Unpin, - T: Source + Unpin, + S: DerefMut, + T: Source, { fn poll_recv( - mut self: Pin<&mut Self>, + self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll> { - ::poll_recv(Pin::new(&mut **self), cx, buf) + ::poll_recv(self.as_deref_mut(), cx, buf) } } diff --git a/src/io/truncate.rs b/src/io/truncate.rs index 7b3230a..aa4f7d7 100644 --- a/src/io/truncate.rs +++ b/src/io/truncate.rs @@ -25,9 +25,7 @@ where impl Truncate for Pin where - // FIXME: Can relax `Unpin` bounds after - // https://github.com/rust-lang/rust/issues/86918 - T: DerefMut + Unpin, + T: DerefMut, U: Truncate + Unpin, { fn truncate(&mut self) {