Skip to content

Commit

Permalink
Prepare v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Nov 5, 2024
1 parent f42ccc1 commit 168b954
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 4 additions & 10 deletions src/io/destination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,14 @@ where

impl<D, T> Destination for Pin<D>
where
// FIXME: Can relax `Unpin` bounds after
// https://github.com/rust-lang/rust/issues/86918
D: DerefMut<Target = T> + Unpin,
T: Destination + Unpin,
D: DerefMut<Target = T>,
T: Destination,
{
fn poll_send(
mut self: Pin<&mut Self>,
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<LostResult<usize>> {
<D::Target as Destination>::poll_send(
Pin::new(self.as_mut().get_mut()),
cx,
buf,
)
<D::Target as Destination>::poll_send(self.as_deref_mut(), cx, buf)
}
}
4 changes: 1 addition & 3 deletions src/io/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ where

impl<S, T> Seek for Pin<S>
where
// FIXME: Can relax `Unpin` bounds after
// https://github.com/rust-lang/rust/issues/86918
S: DerefMut<Target = T> + Unpin,
S: DerefMut<Target = T>,
T: Seek + Unpin,
{
fn seek(&mut self, pos: u64) {
Expand Down
50 changes: 46 additions & 4 deletions src/io/sender.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<usize> {
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(())
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/io/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ where

impl<S, T> Source for Pin<S>
where
// FIXME: Can relax `Unpin` bounds after
// https://github.com/rust-lang/rust/issues/86918
S: DerefMut<Target = T> + Unpin,
T: Source + Unpin,
S: DerefMut<Target = T>,
T: Source,
{
fn poll_recv(
mut self: Pin<&mut Self>,
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<LostResult<usize>> {
<S::Target as Source>::poll_recv(Pin::new(&mut **self), cx, buf)
<S::Target as Source>::poll_recv(self.as_deref_mut(), cx, buf)
}
}
4 changes: 1 addition & 3 deletions src/io/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ where

impl<T, U> Truncate for Pin<T>
where
// FIXME: Can relax `Unpin` bounds after
// https://github.com/rust-lang/rust/issues/86918
T: DerefMut<Target = U> + Unpin,
T: DerefMut<Target = U>,
U: Truncate + Unpin,
{
fn truncate(&mut self) {
Expand Down

0 comments on commit 168b954

Please sign in to comment.