2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-19 20:13:51 +00:00

fix: Remove Pin API related unsafe code

This commit is contained in:
k-nasa 2019-10-24 08:41:01 +09:00
parent b17af61367
commit feeb3c10df

View file

@ -4,22 +4,24 @@ use std::pin::Pin;
use std::time::Duration; use std::time::Duration;
use futures_timer::Delay; use futures_timer::Delay;
use pin_project_lite::pin_project;
use crate::future::Future; use crate::future::Future;
use crate::stream::Stream; use crate::stream::Stream;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
pin_project! {
/// A stream with timeout time set /// A stream with timeout time set
#[derive(Debug)] #[derive(Debug)]
pub struct Timeout<S: Stream> { pub struct Timeout<S: Stream> {
#[pin]
stream: S, stream: S,
#[pin]
delay: Delay, delay: Delay,
} }
}
impl<S: Stream> Timeout<S> { impl<S: Stream> Timeout<S> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_pinned!(delay: Delay);
pub fn new(stream: S, dur: Duration) -> Timeout<S> { pub fn new(stream: S, dur: Duration) -> Timeout<S> {
let delay = Delay::new(dur); let delay = Delay::new(dur);
@ -30,11 +32,13 @@ impl<S: Stream> Timeout<S> {
impl<S: Stream> Stream for Timeout<S> { impl<S: Stream> Stream for Timeout<S> {
type Item = Result<S::Item, TimeoutError>; type Item = Result<S::Item, TimeoutError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.as_mut().stream().poll_next(cx) { let this = self.project();
match this.stream.poll_next(cx) {
Poll::Ready(Some(v)) => Poll::Ready(Some(Ok(v))), Poll::Ready(Some(v)) => Poll::Ready(Some(Ok(v))),
Poll::Ready(None) => Poll::Ready(None), Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => match self.delay().poll(cx) { Poll::Pending => match this.delay.poll(cx) {
Poll::Ready(_) => Poll::Ready(Some(Err(TimeoutError { _private: () }))), Poll::Ready(_) => Poll::Ready(Some(Err(TimeoutError { _private: () }))),
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,
}, },