diff --git a/src/gio/file_output_stream.rs b/src/gio/file_output_stream.rs index c915b2f..a8e5d70 100644 --- a/src/gio/file_output_stream.rs +++ b/src/gio/file_output_stream.rs @@ -1,5 +1,8 @@ pub mod error; +pub mod size; + pub use error::Error; +pub use size::Size; use gio::{ Cancellable, FileOutputStream, IOStream, @@ -7,14 +10,6 @@ use gio::{ }; use glib::{Bytes, Priority, object::IsA}; -/// Mutable bytes count -pub struct Size { - pub chunk: usize, - /// `None` for unlimited - pub limit: Option, - pub total: usize, -} - /// Asynchronously move all bytes from [IOStream](https://docs.gtk.org/gio/class.IOStream.html) /// to [FileOutputStream](https://docs.gtk.org/gio/class.FileOutputStream.html) /// * require `IOStream` reference to keep `Connection` active in async thread diff --git a/src/gio/file_output_stream/size.rs b/src/gio/file_output_stream/size.rs new file mode 100644 index 0000000..285d9f1 --- /dev/null +++ b/src/gio/file_output_stream/size.rs @@ -0,0 +1,7 @@ +/// Mutable bytes count +pub struct Size { + pub chunk: usize, + /// `None` for unlimited + pub limit: Option, + pub total: usize, +} diff --git a/src/gio/memory_input_stream.rs b/src/gio/memory_input_stream.rs index 21c6337..2b1fc39 100644 --- a/src/gio/memory_input_stream.rs +++ b/src/gio/memory_input_stream.rs @@ -1,5 +1,8 @@ pub mod error; +pub mod size; + pub use error::Error; +pub use size::Size; use gio::{ Cancellable, IOStream, MemoryInputStream, @@ -17,7 +20,7 @@ pub fn from_stream_async( io_stream: impl IsA, priority: Priority, cancelable: Cancellable, - (chunk, limit): (usize, usize), + size: Size, (on_chunk, on_complete): ( impl Fn(usize, usize) + 'static, impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static, @@ -28,7 +31,7 @@ pub fn from_stream_async( io_stream, priority, cancelable, - (chunk, limit, 0), + size, (on_chunk, on_complete), ); } @@ -41,14 +44,14 @@ pub fn for_memory_input_stream_async( io_stream: impl IsA, priority: Priority, cancellable: Cancellable, - (chunk, limit, mut total): (usize, usize, usize), + mut size: Size, (on_chunk, on_complete): ( impl Fn(usize, usize) + 'static, impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static, ), ) { io_stream.input_stream().read_bytes_async( - chunk, + size.chunk, priority, Some(&cancellable.clone()), move |result| match result { @@ -57,19 +60,23 @@ pub fn for_memory_input_stream_async( // is end of stream if len == 0 { - return on_complete(Ok((memory_input_stream, total))); + return on_complete(Ok((memory_input_stream, size.total))); } // callback chunk function - total += len; - on_chunk(len, total); + size.total += len; + on_chunk(len, size.total); // push bytes into the memory pool memory_input_stream.add_bytes(&bytes); // prevent memory overflow - if total > limit { - return on_complete(Err(Error::BytesTotal(memory_input_stream, total, limit))); + if size.total > size.limit { + return on_complete(Err(Error::BytesTotal( + memory_input_stream, + size.total, + size.limit, + ))); } // handle next chunk.. @@ -78,7 +85,7 @@ pub fn for_memory_input_stream_async( io_stream, priority, cancellable, - (chunk, limit, total), + size, (on_chunk, on_complete), ) } diff --git a/src/gio/memory_input_stream/size.rs b/src/gio/memory_input_stream/size.rs new file mode 100644 index 0000000..b95ef39 --- /dev/null +++ b/src/gio/memory_input_stream/size.rs @@ -0,0 +1,6 @@ +/// Mutable bytes count +pub struct Size { + pub chunk: usize, + pub limit: usize, + pub total: usize, +}