-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
2,430 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,51 @@ | ||
use std::mem; | ||
use std::pin::Pin; | ||
|
||
use pin_project_lite::pin_project; | ||
|
||
use super::read_until_internal; | ||
use crate::io::{self, BufRead}; | ||
use crate::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
/// A stream over the contents of an instance of [`BufRead`] split on a particular byte. | ||
/// | ||
/// This stream is created by the [`split`] method on types that implement [`BufRead`]. | ||
/// | ||
/// This type is an async version of [`std::io::Split`]. | ||
/// | ||
/// [`split`]: trait.BufRead.html#method.lines | ||
/// [`BufRead`]: trait.BufRead.html | ||
/// [`std::io::Split`]: https://doc.rust-lang.org/std/io/struct.Split.html | ||
#[derive(Debug)] | ||
pub struct Split<R> { | ||
pub(crate) reader: R, | ||
pub(crate) buf: Vec<u8>, | ||
pub(crate) read: usize, | ||
pub(crate) delim: u8, | ||
pin_project! { | ||
/// A stream over the contents of an instance of [`BufRead`] split on a particular byte. | ||
/// | ||
/// This stream is created by the [`split`] method on types that implement [`BufRead`]. | ||
/// | ||
/// This type is an async version of [`std::io::Split`]. | ||
/// | ||
/// [`split`]: trait.BufRead.html#method.lines | ||
/// [`BufRead`]: trait.BufRead.html | ||
/// [`std::io::Split`]: https://doc.rust-lang.org/std/io/struct.Split.html | ||
#[derive(Debug)] | ||
pub struct Split<R> { | ||
#[pin] | ||
pub(crate) reader: R, | ||
pub(crate) buf: Vec<u8>, | ||
pub(crate) read: usize, | ||
pub(crate) delim: u8, | ||
} | ||
} | ||
|
||
impl<R: BufRead> Stream for Split<R> { | ||
type Item = io::Result<Vec<u8>>; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let Self { | ||
reader, | ||
buf, | ||
read, | ||
delim, | ||
} = unsafe { self.get_unchecked_mut() }; | ||
let reader = unsafe { Pin::new_unchecked(reader) }; | ||
let n = futures_core::ready!(read_until_internal(reader, cx, *delim, buf, read))?; | ||
if n == 0 && buf.is_empty() { | ||
let this = self.project(); | ||
let n = futures_core::ready!(read_until_internal( | ||
this.reader, | ||
cx, | ||
*this.delim, | ||
this.buf, | ||
this.read | ||
))?; | ||
if n == 0 && this.buf.is_empty() { | ||
return Poll::Ready(None); | ||
} | ||
if buf[buf.len() - 1] == *delim { | ||
buf.pop(); | ||
if this.buf[this.buf.len() - 1] == *this.delim { | ||
this.buf.pop(); | ||
} | ||
Poll::Ready(Some(Ok(mem::replace(buf, vec![])))) | ||
Poll::Ready(Some(Ok(mem::replace(this.buf, vec![])))) | ||
} | ||
} |
Oops, something went wrong.