mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
rename method, apply minor optimizations
This commit is contained in:
parent
46483d1829
commit
867945ec74
1 changed files with 18 additions and 29 deletions
|
|
@ -10,49 +10,40 @@ use glib::{object::IsA, Bytes, Priority};
|
||||||
/// 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
|
||||||
pub fn move_all_from_stream_async(
|
pub fn from_stream_async(
|
||||||
base_io_stream: impl IsA<IOStream>,
|
io_stream: impl IsA<IOStream>,
|
||||||
file_output_stream: FileOutputStream,
|
file_output_stream: FileOutputStream,
|
||||||
cancellable: Cancellable,
|
cancellable: Cancellable,
|
||||||
priority: Priority,
|
priority: Priority,
|
||||||
bytes: (
|
(chunk, limit, mut total): (
|
||||||
usize, // bytes_in_chunk
|
usize, // bytes_in_chunk
|
||||||
Option<usize>, // bytes_total_limit, `None` to unlimited
|
Option<usize>, // bytes_total_limit, `None` to unlimited
|
||||||
usize, // bytes_total
|
usize, // bytes_total
|
||||||
),
|
),
|
||||||
callback: (
|
(on_chunk, on_complete): (
|
||||||
impl Fn(Bytes, usize) + 'static, // on_chunk
|
impl Fn(Bytes, usize) + 'static, // on_chunk
|
||||||
impl FnOnce(Result<(FileOutputStream, usize), Error>) + 'static, // on_complete
|
impl FnOnce(Result<(FileOutputStream, usize), Error>) + 'static, // on_complete
|
||||||
),
|
),
|
||||||
) {
|
) {
|
||||||
let (on_chunk, on_complete) = callback;
|
io_stream.input_stream().read_bytes_async(
|
||||||
let (bytes_in_chunk, bytes_total_limit, bytes_total) = bytes;
|
chunk,
|
||||||
|
|
||||||
base_io_stream.input_stream().read_bytes_async(
|
|
||||||
bytes_in_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();
|
||||||
let bytes_total = bytes_total + bytes.len();
|
on_chunk(bytes.clone(), total);
|
||||||
|
|
||||||
// Callback chunk function
|
if let Some(limit) = limit {
|
||||||
on_chunk(bytes.clone(), bytes_total);
|
if total > limit {
|
||||||
|
return on_complete(Err(Error::BytesTotal(total, limit)));
|
||||||
// Validate max size
|
|
||||||
if let Some(bytes_total_limit) = bytes_total_limit {
|
|
||||||
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((file_output_stream, bytes_total)));
|
return on_complete(Ok((file_output_stream, total)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write chunk bytes
|
|
||||||
file_output_stream.clone().write_async(
|
file_output_stream.clone().write_async(
|
||||||
bytes.clone(),
|
bytes.clone(),
|
||||||
priority,
|
priority,
|
||||||
|
|
@ -60,13 +51,13 @@ pub fn move_all_from_stream_async(
|
||||||
move |result| {
|
move |result| {
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// Continue
|
// continue read..
|
||||||
move_all_from_stream_async(
|
from_stream_async(
|
||||||
base_io_stream,
|
io_stream,
|
||||||
file_output_stream,
|
file_output_stream,
|
||||||
cancellable,
|
cancellable,
|
||||||
priority,
|
priority,
|
||||||
(bytes_in_chunk, bytes_total_limit, bytes_total),
|
(chunk, limit, total),
|
||||||
(on_chunk, on_complete),
|
(on_chunk, on_complete),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -77,9 +68,7 @@ pub fn move_all_from_stream_async(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => on_complete(Err(Error::InputStream(e))),
|
||||||
on_complete(Err(Error::InputStream(e)));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue