From ae3d8a1a0003428a146670400227ebfb1ffba6f8 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 26 May 2019 20:48:31 +0900 Subject: [PATCH] Try to use destruct/enum support --- futures-util/src/io/lines.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/futures-util/src/io/lines.rs b/futures-util/src/io/lines.rs index b2933fcb66..8043e80dcd 100644 --- a/futures-util/src/io/lines.rs +++ b/futures-util/src/io/lines.rs @@ -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 { + #[pin] reader: R, buf: String, bytes: Vec, read: usize, } -impl Unpin for Lines {} - impl Lines { pub(super) fn new(reader: R) -> Self { Self { @@ -32,9 +33,10 @@ impl Lines { impl Stream for Lines { type Item = io::Result; + #[pin_project(self)] fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - 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)