// use futures::{Stream, AsyncRead, TryStream}; use std::task::{Context, Poll}; use std::pin::Pin; use actix_web::web::Bytes; use std::io::Error; pub struct ToStream(pub T); impl ToStream { fn get_field(self: Pin<&mut Self>) -> Pin<&mut T> { unsafe { self.map_unchecked_mut(|s| &mut s.0) } } } impl Unpin for ToStream{} impl futures::Stream for ToStream{ type Item = Result; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let mut buff: [u8; 1024] = [0; 1024]; let mut t = self.get_field(); 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; } } }