make Size struct for tuple argument, move Size to separated mod

This commit is contained in:
yggverse 2025-03-27 21:33:50 +02:00
parent 4dddbd5f8a
commit 9bbaecf344
4 changed files with 33 additions and 18 deletions

View file

@ -1,5 +1,8 @@
pub mod error; pub mod error;
pub mod size;
pub use error::Error; pub use error::Error;
pub use size::Size;
use gio::{ use gio::{
Cancellable, FileOutputStream, IOStream, Cancellable, FileOutputStream, IOStream,
@ -7,14 +10,6 @@ use gio::{
}; };
use glib::{Bytes, Priority, object::IsA}; use glib::{Bytes, Priority, object::IsA};
/// Mutable bytes count
pub struct Size {
pub chunk: usize,
/// `None` for unlimited
pub limit: Option<usize>,
pub total: usize,
}
/// Asynchronously move all bytes from [IOStream](https://docs.gtk.org/gio/class.IOStream.html) /// Asynchronously move all bytes from [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
/// to [FileOutputStream](https://docs.gtk.org/gio/class.FileOutputStream.html) /// to [FileOutputStream](https://docs.gtk.org/gio/class.FileOutputStream.html)
/// * require `IOStream` reference to keep `Connection` active in async thread /// * require `IOStream` reference to keep `Connection` active in async thread

View file

@ -0,0 +1,7 @@
/// Mutable bytes count
pub struct Size {
pub chunk: usize,
/// `None` for unlimited
pub limit: Option<usize>,
pub total: usize,
}

View file

@ -1,5 +1,8 @@
pub mod error; pub mod error;
pub mod size;
pub use error::Error; pub use error::Error;
pub use size::Size;
use gio::{ use gio::{
Cancellable, IOStream, MemoryInputStream, Cancellable, IOStream, MemoryInputStream,
@ -17,7 +20,7 @@ pub fn from_stream_async(
io_stream: impl IsA<IOStream>, io_stream: impl IsA<IOStream>,
priority: Priority, priority: Priority,
cancelable: Cancellable, cancelable: Cancellable,
(chunk, limit): (usize, usize), size: Size,
(on_chunk, on_complete): ( (on_chunk, on_complete): (
impl Fn(usize, usize) + 'static, impl Fn(usize, usize) + 'static,
impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static, impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static,
@ -28,7 +31,7 @@ pub fn from_stream_async(
io_stream, io_stream,
priority, priority,
cancelable, cancelable,
(chunk, limit, 0), size,
(on_chunk, on_complete), (on_chunk, on_complete),
); );
} }
@ -41,14 +44,14 @@ pub fn for_memory_input_stream_async(
io_stream: impl IsA<IOStream>, io_stream: impl IsA<IOStream>,
priority: Priority, priority: Priority,
cancellable: Cancellable, cancellable: Cancellable,
(chunk, limit, mut total): (usize, usize, usize), mut size: Size,
(on_chunk, on_complete): ( (on_chunk, on_complete): (
impl Fn(usize, usize) + 'static, impl Fn(usize, usize) + 'static,
impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static, impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static,
), ),
) { ) {
io_stream.input_stream().read_bytes_async( io_stream.input_stream().read_bytes_async(
chunk, size.chunk,
priority, priority,
Some(&cancellable.clone()), Some(&cancellable.clone()),
move |result| match result { move |result| match result {
@ -57,19 +60,23 @@ pub fn for_memory_input_stream_async(
// is end of stream // is end of stream
if len == 0 { if len == 0 {
return on_complete(Ok((memory_input_stream, total))); return on_complete(Ok((memory_input_stream, size.total)));
} }
// callback chunk function // callback chunk function
total += len; size.total += len;
on_chunk(len, total); on_chunk(len, size.total);
// push bytes into the memory pool // push bytes into the memory pool
memory_input_stream.add_bytes(&bytes); memory_input_stream.add_bytes(&bytes);
// prevent memory overflow // prevent memory overflow
if total > limit { if size.total > size.limit {
return on_complete(Err(Error::BytesTotal(memory_input_stream, total, limit))); return on_complete(Err(Error::BytesTotal(
memory_input_stream,
size.total,
size.limit,
)));
} }
// handle next chunk.. // handle next chunk..
@ -78,7 +85,7 @@ pub fn for_memory_input_stream_async(
io_stream, io_stream,
priority, priority,
cancellable, cancellable,
(chunk, limit, total), size,
(on_chunk, on_complete), (on_chunk, on_complete),
) )
} }

View file

@ -0,0 +1,6 @@
/// Mutable bytes count
pub struct Size {
pub chunk: usize,
pub limit: usize,
pub total: usize,
}