mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
make Size struct for tuple argument, move Size to separated mod
This commit is contained in:
parent
4dddbd5f8a
commit
9bbaecf344
4 changed files with 33 additions and 18 deletions
|
|
@ -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<usize>,
|
||||
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
|
||||
|
|
|
|||
7
src/gio/file_output_stream/size.rs
Normal file
7
src/gio/file_output_stream/size.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/// Mutable bytes count
|
||||
pub struct Size {
|
||||
pub chunk: usize,
|
||||
/// `None` for unlimited
|
||||
pub limit: Option<usize>,
|
||||
pub total: usize,
|
||||
}
|
||||
|
|
@ -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<IOStream>,
|
||||
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<IOStream>,
|
||||
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),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
6
src/gio/memory_input_stream/size.rs
Normal file
6
src/gio/memory_input_stream/size.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/// Mutable bytes count
|
||||
pub struct Size {
|
||||
pub chunk: usize,
|
||||
pub limit: usize,
|
||||
pub total: usize,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue