Skip to content

Commit

Permalink
[git-packetline] Custom implementation of read_line future to avoid e…
Browse files Browse the repository at this point in the history
…xtra work…

…and it also showed us we are not actually send. Need to see if that
really is a problem later or not.
  • Loading branch information
Byron committed May 18, 2021
1 parent 8007c65 commit 91c2895
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
35 changes: 18 additions & 17 deletions git-packetline/src/read/sidebands/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,37 @@ where
}
}

// /// Read a packet line as line.
// pub fn read_line<'b>(&'a mut self, buf: &'b mut String) -> ReadLineFuture<'a, 'b, Self> {
// ReadLineFuture { parent: self, buf }
// }
/// Read a packet line as line.
pub fn read_line<'b>(&'b mut self, buf: &'b mut String) -> ReadLineFuture<'b, Self> {
ReadLineFuture { parent: self, buf }
}
}

pub struct ReadLineFuture<'a, 'b, F: Unpin> {
pub struct ReadLineFuture<'a, F: Unpin + ?Sized> {
parent: &'a mut F,
buf: &'b mut String,
buf: &'a mut String,
}

impl<'a, 'b, F> Future for ReadLineFuture<'a, 'b, F>
impl<'a, F> Future for ReadLineFuture<'a, F>
where
F: AsyncBufRead + Unpin + Send,
F: AsyncBufRead + Unpin,
{
type Output = std::io::Result<usize>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// assert_eq!(
// self.cap, 0,
// self.parent.cap, 0,
// "we don't support partial buffers right now - read-line must be used consistently"
// );
// let line = std::str::from_utf8(self.fill_buf()?)
// .map_err(|err| io::Error::new(io::ErrorKind::Other, err))
// .unwrap();
// buf.push_str(line);
// let bytes = line.len();
let Self { buf, parent } = &mut *self;
let line = std::str::from_utf8(ready!(Pin::new(parent).poll_fill_buf(cx))?)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
.unwrap();
buf.clear();
buf.push_str(line);
let bytes = line.len();
// self.cap = 0;
// Ok(bytes)
todo!("poll readline")
Poll::Ready(Ok(bytes))
}
}

Expand Down
17 changes: 0 additions & 17 deletions git-packetline/tests/blocking/read/sideband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,3 @@ fn peek_past_an_actual_eof_is_an_error() -> crate::Result {
);
Ok(())
}

#[test]
fn peek_past_a_delimiter_is_no_error() -> crate::Result {
let input = b"0009hello0000";
let mut rd = git_packetline::StreamingPeekableIter::new(&input[..], &[PacketLine::Flush]);
let mut reader = rd.as_read();
assert_eq!(reader.peek_data_line().expect("one line")??, b"hello");

let mut buf = String::new();
reader.read_line(&mut buf)?;

assert!(
reader.peek_data_line().is_none(),
"peeking past a flush packet is a 'natural' event that shold not cause an error"
);
Ok(())
}
1 change: 1 addition & 0 deletions git-packetline/tests/read/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod sideband;

pub mod streaming_peek_iter {
use bstr::ByteSlice;
use git_packetline::PacketLine;
Expand Down
3 changes: 1 addition & 2 deletions git-packetline/tests/read/sideband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use futures_lite::io::AsyncReadExt;
use git_packetline::PacketLine;
#[cfg(feature = "blocking-io")]
use std::io::Read;
use std::io::{BufRead, Read};

#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
async fn peek_past_a_delimiter_is_no_error() -> crate::Result {
Expand All @@ -13,7 +13,6 @@ async fn peek_past_a_delimiter_is_no_error() -> crate::Result {
assert_eq!(res.expect("one line")??, b"hello");

let mut buf = String::new();
use futures_lite::AsyncBufReadExt;
reader.read_line(&mut buf).await?;

let res = reader.peek_data_line().await;
Expand Down

0 comments on commit 91c2895

Please sign in to comment.