Skip to content

Commit 168b954

Browse files
committed
Prepare v0.2.0
1 parent f42ccc1 commit 168b954

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "parsenic"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "Apache-2.0 OR BSL-1.0 OR MIT"
66
description = "A simple no-std/no-alloc I/O and parsing crate"

src/io/destination.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,14 @@ where
3636

3737
impl<D, T> Destination for Pin<D>
3838
where
39-
// FIXME: Can relax `Unpin` bounds after
40-
// https://github.com/rust-lang/rust/issues/86918
41-
D: DerefMut<Target = T> + Unpin,
42-
T: Destination + Unpin,
39+
D: DerefMut<Target = T>,
40+
T: Destination,
4341
{
4442
fn poll_send(
45-
mut self: Pin<&mut Self>,
43+
self: Pin<&mut Self>,
4644
cx: &mut Context<'_>,
4745
buf: &[u8],
4846
) -> Poll<LostResult<usize>> {
49-
<D::Target as Destination>::poll_send(
50-
Pin::new(self.as_mut().get_mut()),
51-
cx,
52-
buf,
53-
)
47+
<D::Target as Destination>::poll_send(self.as_deref_mut(), cx, buf)
5448
}
5549
}

src/io/seek.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ where
5757

5858
impl<S, T> Seek for Pin<S>
5959
where
60-
// FIXME: Can relax `Unpin` bounds after
61-
// https://github.com/rust-lang/rust/issues/86918
62-
S: DerefMut<Target = T> + Unpin,
60+
S: DerefMut<Target = T>,
6361
T: Seek + Unpin,
6462
{
6563
fn seek(&mut self, pos: u64) {

src/io/sender.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use core::{future, pin::Pin};
2+
13
use crate::{
4+
error::{FlushError, FullError},
25
io::{Destination, Seek, Truncate},
3-
result::FlushResult,
6+
result::{FlushResult, LostResult},
47
};
58

69
/// [`slice`] buffered sender.
@@ -38,13 +41,52 @@ where
3841
///
3942
/// May not send the full amount of bytes until either the buffer is full or
4043
/// [`flush()`](Self::flush()) is called.
41-
pub async fn send(&mut self, bytes: &[u8]) -> FlushResult {
42-
todo!("{bytes:?}") // FIXME
44+
pub async fn send(&mut self, bytes: &[u8]) -> LostResult<usize> {
45+
let mut total_sent = 0;
46+
47+
for byte in bytes.iter().cloned() {
48+
if self.cursor == BUF {
49+
self.cursor = 0;
50+
51+
let sent = future::poll_fn(|cx| {
52+
Pin::new(&mut self.destination)
53+
.poll_send(cx, self.buffer.as_ref())
54+
})
55+
.await?;
56+
57+
total_sent += sent;
58+
59+
if sent != BUF {
60+
return Ok(total_sent);
61+
}
62+
}
63+
64+
self.buffer[self.cursor] = byte;
65+
self.cursor += 1;
66+
}
67+
68+
Ok(total_sent)
4369
}
4470

4571
/// Send buffered data with the destination.
4672
pub async fn flush(&mut self) -> FlushResult {
47-
todo!() // FIXME
73+
let old_cursor = self.cursor;
74+
let sent = future::poll_fn(|cx| {
75+
Pin::new(&mut self.destination)
76+
.poll_send(cx, &self.buffer[..self.cursor])
77+
})
78+
.await?;
79+
80+
self.cursor -= sent;
81+
82+
if self.cursor != 0 {
83+
self.buffer.copy_within(sent..old_cursor, 0);
84+
return Err(FlushError::Full(FullError::from_remaining(
85+
self.cursor,
86+
)));
87+
}
88+
89+
Ok(())
4890
}
4991
}
5092

src/io/source.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,14 @@ where
3636

3737
impl<S, T> Source for Pin<S>
3838
where
39-
// FIXME: Can relax `Unpin` bounds after
40-
// https://github.com/rust-lang/rust/issues/86918
41-
S: DerefMut<Target = T> + Unpin,
42-
T: Source + Unpin,
39+
S: DerefMut<Target = T>,
40+
T: Source,
4341
{
4442
fn poll_recv(
45-
mut self: Pin<&mut Self>,
43+
self: Pin<&mut Self>,
4644
cx: &mut Context<'_>,
4745
buf: &mut [u8],
4846
) -> Poll<LostResult<usize>> {
49-
<S::Target as Source>::poll_recv(Pin::new(&mut **self), cx, buf)
47+
<S::Target as Source>::poll_recv(self.as_deref_mut(), cx, buf)
5048
}
5149
}

src/io/truncate.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ where
2525

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

0 commit comments

Comments
 (0)