rename arguments, use tuple for arguments group

This commit is contained in:
yggverse 2025-02-03 13:38:04 +02:00
parent c9d5e5987c
commit 1505b6311c

View file

@ -5,7 +5,7 @@ use gio::{
prelude::{IOStreamExt, InputStreamExt, MemoryInputStreamExt}, prelude::{IOStreamExt, InputStreamExt, MemoryInputStreamExt},
Cancellable, IOStream, MemoryInputStream, Cancellable, IOStream, MemoryInputStream,
}; };
use glib::{object::IsA, Bytes, Priority}; use glib::{object::IsA, Priority};
/// Asynchronously create new [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html) /// Asynchronously create new [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
/// from [IOStream](https://docs.gtk.org/gio/class.IOStream.html) /// from [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
@ -14,20 +14,21 @@ use glib::{object::IsA, Bytes, Priority};
/// * safe read (of memory overflow) to dynamically allocated buffer, where final size of target data unknown /// * safe read (of memory overflow) to dynamically allocated buffer, where final size of target data unknown
/// * calculate bytes processed on chunk load /// * calculate bytes processed on chunk load
pub fn from_stream_async( pub fn from_stream_async(
base_io_stream: impl IsA<IOStream>, io_stream: impl IsA<IOStream>,
cancelable: Cancellable, cancelable: Cancellable,
priority: Priority, priority: Priority,
bytes_in_chunk: usize, (chunk, limit): (usize, usize),
bytes_total_limit: usize, (on_chunk, on_complete): (
on_chunk: impl Fn(Bytes, usize) + 'static, impl Fn(usize, usize) + 'static,
on_complete: impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static, impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static,
),
) { ) {
move_all_from_stream_async( move_all_from_stream_async(
base_io_stream, io_stream,
MemoryInputStream::new(), MemoryInputStream::new(),
cancelable, cancelable,
priority, priority,
(bytes_in_chunk, bytes_total_limit, 0), (chunk, limit, 0),
(on_chunk, on_complete), (on_chunk, on_complete),
); );
} }
@ -36,48 +37,42 @@ pub fn from_stream_async(
/// to [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html) /// to [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
/// * require `IOStream` reference to keep `Connection` active in async thread /// * require `IOStream` reference to keep `Connection` active in async thread
pub fn move_all_from_stream_async( pub fn move_all_from_stream_async(
base_io_stream: impl IsA<IOStream>, io_stream: impl IsA<IOStream>,
memory_input_stream: MemoryInputStream, memory_input_stream: MemoryInputStream,
cancellable: Cancellable, cancellable: Cancellable,
priority: Priority, priority: Priority,
(bytes_in_chunk, bytes_total_limit, mut bytes_total): (usize, usize, usize), (chunk, limit, mut total): (usize, usize, usize),
(on_chunk, on_complete): ( (on_chunk, on_complete): (
impl Fn(Bytes, usize) + 'static, impl Fn(usize, usize) + 'static,
impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static, impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static,
), ),
) { ) {
base_io_stream.input_stream().read_bytes_async( io_stream.input_stream().read_bytes_async(
bytes_in_chunk, chunk,
priority, priority,
Some(&cancellable.clone()), Some(&cancellable.clone()),
move |result| match result { move |result| match result {
Ok(bytes) => { Ok(bytes) => {
// Update bytes total total += bytes.len();
bytes_total += bytes.len(); on_chunk(bytes.len(), total);
// Callback chunk function if total > limit {
on_chunk(bytes.clone(), bytes_total); return on_complete(Err(Error::BytesTotal(total, limit)));
// Validate max size
if bytes_total > bytes_total_limit {
return on_complete(Err(Error::BytesTotal(bytes_total, bytes_total_limit)));
} }
// No bytes were read, end of stream
if bytes.len() == 0 { if bytes.len() == 0 {
return on_complete(Ok((memory_input_stream, bytes_total))); return on_complete(Ok((memory_input_stream, total)));
} }
// Write chunk bytes
memory_input_stream.add_bytes(&bytes); memory_input_stream.add_bytes(&bytes);
// Continue // continue reading..
move_all_from_stream_async( move_all_from_stream_async(
base_io_stream, io_stream,
memory_input_stream, memory_input_stream,
cancellable, cancellable,
priority, priority,
(bytes_in_chunk, bytes_total_limit, bytes_total), (chunk, limit, total),
(on_chunk, on_complete), (on_chunk, on_complete),
); );
} }