Skip to content

Commit

Permalink
Try to use destruct/enum support
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jul 2, 2019
1 parent 96b9b49 commit cc32a4e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions futures-util/src/io/lines.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use super::read_line::read_line_internal;
use futures_core::stream::Stream;
use futures_core::task::{Context, Poll};
use futures_io::AsyncBufRead;
use pin_project::{pin_project, unsafe_project};
use std::io;
use std::mem;
use std::pin::Pin;
use super::read_line::read_line_internal;

/// Stream for the [`lines`](super::AsyncBufReadExt::lines) method.
#[unsafe_project(Unpin)]
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct Lines<R> {
#[pin]
reader: R,
buf: String,
bytes: Vec<u8>,
read: usize,
}

impl<R: Unpin> Unpin for Lines<R> {}

impl<R: AsyncBufRead> Lines<R> {
pub(super) fn new(reader: R) -> Self {
Self {
Expand All @@ -32,9 +33,10 @@ impl<R: AsyncBufRead> Lines<R> {
impl<R: AsyncBufRead> Stream for Lines<R> {
type Item = io::Result<String>;

#[pin_project(self)]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let Self { reader, buf, bytes, read } = unsafe { self.get_unchecked_mut() };
let reader = unsafe { Pin::new_unchecked(reader) };
#[project]
let Lines { reader, buf, bytes, read } = self;
let n = ready!(read_line_internal(reader, buf, bytes, read, cx))?;
if n == 0 && buf.is_empty() {
return Poll::Ready(None)
Expand Down

0 comments on commit cc32a4e

Please sign in to comment.