-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT> | ||
|
||
use error::to_std_io_result; | ||
use futures_core::task::{Context, Poll}; | ||
use futures_io::AsyncRead; | ||
use futures_util::future::FutureExt; | ||
use gio_sys; | ||
use glib::object::IsA; | ||
use glib::translate::*; | ||
|
@@ -81,6 +84,16 @@ pub trait InputStreamExtManual: Sized { | |
{ | ||
InputStreamRead(self) | ||
} | ||
|
||
fn into_async_buf_read(self, buffer_size: usize) -> InputStreamAsyncBufRead<Self> | ||
where | ||
Self: IsA<InputStream>, | ||
{ | ||
let mut buffer = Vec::with_capacity(buffer_size); | ||
buffer.resize(buffer_size, 0); | ||
|
||
InputStreamAsyncBufRead(self, buffer) | ||
} | ||
} | ||
|
||
impl<O: IsA<InputStream>> InputStreamExtManual for O { | ||
|
@@ -341,6 +354,44 @@ impl<T: IsA<InputStream> + IsA<Seekable>> io::Seek for InputStreamRead<T> { | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct InputStreamAsyncBufRead<T: IsA<InputStream>>(T, Vec<u8>); | ||
|
||
impl<T: IsA<InputStream>> InputStreamAsyncBufRead<T> { | ||
pub fn into_input_stream(self) -> T { | ||
self.0 | ||
} | ||
|
||
pub fn input_stream(&self) -> &T { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T: IsA<InputStream>> AsyncRead for InputStreamAsyncBufRead<T> { | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context, | ||
buf: &mut [u8], | ||
) -> Poll<io::Result<usize>> { | ||
let stream = Pin::get_ref(self.as_ref()); | ||
let my_buf = vec![0; 10]; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gdesmott
Owner
|
||
let mut fut = stream | ||
.0 | ||
.as_ref() | ||
.read_async_future(my_buf, Priority::default()); | ||
|
||
match dbg!(fut.poll_unpin(cx)) { | ||
This comment has been minimized.
Sorry, something went wrong.
sdroege
|
||
// FIXME: copy to @buf | ||
Poll::Ready(Ok((out_buf, res))) => Poll::Ready(Ok(res)), | ||
Poll::Ready(Err((_, err))) => { | ||
let kind = err.kind::<crate::IOErrorEnum>().unwrap(); | ||
Poll::Ready(Err(io::Error::new(io::ErrorKind::from(kind), err))) | ||
} | ||
Poll::Pending => Poll::Pending, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::prelude::*; | ||
|
The idea would be to store a fixed-size buffer inside the adapter directly, probably a configurable buffer size. And then re-use that for any read requests