Merge branch 'master' into fs-stream-fold
commit
774550ca99
@ -0,0 +1,32 @@
|
||||
use std::pin::Pin;
|
||||
|
||||
use futures_io::AsyncBufRead;
|
||||
|
||||
use crate::future::Future;
|
||||
use crate::io;
|
||||
use crate::task::{Context, Poll};
|
||||
|
||||
#[doc(hidden)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct FillBufFuture<'a, R: ?Sized> {
|
||||
reader: &'a mut R,
|
||||
}
|
||||
|
||||
impl<'a, R: ?Sized> FillBufFuture<'a, R> {
|
||||
pub(crate) fn new(reader: &'a mut R) -> Self {
|
||||
Self { reader }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, R: AsyncBufRead + Unpin + ?Sized> Future for FillBufFuture<'a, R> {
|
||||
type Output = io::Result<&'a [u8]>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'a [u8]>> {
|
||||
let Self { reader } = &mut *self;
|
||||
let result = Pin::new(reader).poll_fill_buf(cx);
|
||||
// This is safe because:
|
||||
// 1. The buffer is valid for the lifetime of the reader.
|
||||
// 2. Output is unrelated to the wrapper (Self).
|
||||
result.map_ok(|buf| unsafe { std::mem::transmute::<&'_ [u8], &'a [u8]>(buf) })
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
use crate::task::{Context, Poll};
|
||||
use std::marker::PhantomData;
|
||||
use std::pin::Pin;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct FindFuture<'a, S, P, T> {
|
||||
stream: &'a mut S,
|
||||
p: P,
|
||||
__t: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, S, P, T> FindFuture<'a, S, P, T> {
|
||||
pin_utils::unsafe_pinned!(stream: &'a mut S);
|
||||
pin_utils::unsafe_unpinned!(p: P);
|
||||
|
||||
pub(super) fn new(stream: &'a mut S, p: P) -> Self {
|
||||
FindFuture {
|
||||
stream,
|
||||
p,
|
||||
__t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S, P> futures_core::future::Future for FindFuture<'a, S, P, S::Item>
|
||||
where
|
||||
S: futures_core::stream::Stream + Unpin + Sized,
|
||||
P: FnMut(&S::Item) -> bool,
|
||||
{
|
||||
type Output = Option<S::Item>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
use futures_core::stream::Stream;
|
||||
|
||||
let item = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||
|
||||
match item {
|
||||
Some(v) => match (self.as_mut().p())(&v) {
|
||||
true => Poll::Ready(Some(v)),
|
||||
false => {
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
}
|
||||
},
|
||||
None => Poll::Ready(None),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue