mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
rename arguments, use tuple for arguments group
This commit is contained in:
parent
c9d5e5987c
commit
1505b6311c
1 changed files with 22 additions and 27 deletions
|
|
@ -5,7 +5,7 @@ use gio::{
|
|||
prelude::{IOStreamExt, InputStreamExt, MemoryInputStreamExt},
|
||||
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)
|
||||
/// 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
|
||||
/// * calculate bytes processed on chunk load
|
||||
pub fn from_stream_async(
|
||||
base_io_stream: impl IsA<IOStream>,
|
||||
io_stream: impl IsA<IOStream>,
|
||||
cancelable: Cancellable,
|
||||
priority: Priority,
|
||||
bytes_in_chunk: usize,
|
||||
bytes_total_limit: usize,
|
||||
on_chunk: impl Fn(Bytes, usize) + 'static,
|
||||
on_complete: impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static,
|
||||
(chunk, limit): (usize, usize),
|
||||
(on_chunk, on_complete): (
|
||||
impl Fn(usize, usize) + 'static,
|
||||
impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static,
|
||||
),
|
||||
) {
|
||||
move_all_from_stream_async(
|
||||
base_io_stream,
|
||||
io_stream,
|
||||
MemoryInputStream::new(),
|
||||
cancelable,
|
||||
priority,
|
||||
(bytes_in_chunk, bytes_total_limit, 0),
|
||||
(chunk, limit, 0),
|
||||
(on_chunk, on_complete),
|
||||
);
|
||||
}
|
||||
|
|
@ -36,48 +37,42 @@ pub fn from_stream_async(
|
|||
/// to [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
|
||||
/// * require `IOStream` reference to keep `Connection` active in async thread
|
||||
pub fn move_all_from_stream_async(
|
||||
base_io_stream: impl IsA<IOStream>,
|
||||
io_stream: impl IsA<IOStream>,
|
||||
memory_input_stream: MemoryInputStream,
|
||||
cancellable: Cancellable,
|
||||
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): (
|
||||
impl Fn(Bytes, usize) + 'static,
|
||||
impl Fn(usize, usize) + 'static,
|
||||
impl FnOnce(Result<(MemoryInputStream, usize), Error>) + 'static,
|
||||
),
|
||||
) {
|
||||
base_io_stream.input_stream().read_bytes_async(
|
||||
bytes_in_chunk,
|
||||
io_stream.input_stream().read_bytes_async(
|
||||
chunk,
|
||||
priority,
|
||||
Some(&cancellable.clone()),
|
||||
move |result| match result {
|
||||
Ok(bytes) => {
|
||||
// Update bytes total
|
||||
bytes_total += bytes.len();
|
||||
total += bytes.len();
|
||||
on_chunk(bytes.len(), total);
|
||||
|
||||
// Callback chunk function
|
||||
on_chunk(bytes.clone(), bytes_total);
|
||||
|
||||
// Validate max size
|
||||
if bytes_total > bytes_total_limit {
|
||||
return on_complete(Err(Error::BytesTotal(bytes_total, bytes_total_limit)));
|
||||
if total > limit {
|
||||
return on_complete(Err(Error::BytesTotal(total, limit)));
|
||||
}
|
||||
|
||||
// No bytes were read, end of stream
|
||||
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);
|
||||
|
||||
// Continue
|
||||
// continue reading..
|
||||
move_all_from_stream_async(
|
||||
base_io_stream,
|
||||
io_stream,
|
||||
memory_input_stream,
|
||||
cancellable,
|
||||
priority,
|
||||
(bytes_in_chunk, bytes_total_limit, bytes_total),
|
||||
(chunk, limit, total),
|
||||
(on_chunk, on_complete),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue