gitust/src/reader.rs

33 lines
1.0 KiB
Rust
Raw Normal View History

2021-07-13 14:24:38 +02:00
// use futures::{Stream, AsyncRead, TryStream};
2021-07-13 07:05:28 +02:00
use std::task::{Context, Poll};
use std::pin::Pin;
use actix_web::web::Bytes;
use std::io::Error;
2021-07-13 14:24:38 +02:00
pub struct ToStream<T>(pub T);
2021-07-13 07:05:28 +02:00
2021-07-13 08:21:30 +02:00
impl<T> ToStream<T> {
fn get_field(self: Pin<&mut Self>) -> Pin<&mut T> {
unsafe { self.map_unchecked_mut(|s| &mut s.0) }
}
}
impl <T : Unpin> Unpin for ToStream<T>{}
2021-07-13 14:24:38 +02:00
impl <T : tokio::io::AsyncRead> futures::Stream for ToStream<T>{
2021-07-13 07:05:28 +02:00
type Item = Result<Bytes, Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut buff: [u8; 1024] = [0; 1024];
2021-07-13 08:21:30 +02:00
let mut t = self.get_field();
2021-07-14 06:31:36 +02:00
let poll = t.poll_read(cx, &mut buff[..]);
if let Poll::Ready(Ok(0)) = poll {
// println!("end of response");
return Poll::Ready(None);
} else {
let res = poll.map_ok(|l| {Bytes::copy_from_slice(&buff[0..l])}).map(|res| Some(res));
// println!("poll_next : {:?}", res);
return res;
}
2021-07-13 07:05:28 +02:00
}
}