tokio_stream/wrappers/
lines.rs1use crate::Stream;
2use pin_project_lite::pin_project;
3use std::io;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6use tokio::io::{AsyncBufRead, Lines};
7
8pin_project! {
9 #[derive(Debug)]
32 #[cfg_attr(docsrs, doc(cfg(feature = "io-util")))]
33 pub struct LinesStream<R> {
34 #[pin]
35 inner: Lines<R>,
36 }
37}
38
39impl<R> LinesStream<R> {
40 pub fn new(lines: Lines<R>) -> Self {
42 Self { inner: lines }
43 }
44
45 pub fn into_inner(self) -> Lines<R> {
47 self.inner
48 }
49
50 pub fn as_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Lines<R>> {
52 self.project().inner
53 }
54}
55
56impl<R: AsyncBufRead> Stream for LinesStream<R> {
57 type Item = io::Result<String>;
58
59 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
60 self.project()
61 .inner
62 .poll_next_line(cx)
63 .map(Result::transpose)
64 }
65}
66
67impl<R> AsRef<Lines<R>> for LinesStream<R> {
68 fn as_ref(&self) -> &Lines<R> {
69 &self.inner
70 }
71}
72
73impl<R> AsMut<Lines<R>> for LinesStream<R> {
74 fn as_mut(&mut self) -> &mut Lines<R> {
75 &mut self.inner
76 }
77}