remove option wrapper from callbacks

This commit is contained in:
yggverse 2024-10-29 16:51:05 +02:00
parent 4ab83702e9
commit 3e8da1725c

View file

@ -25,7 +25,7 @@ use glib::{Bytes, Priority};
/// * recursively read all bytes from `InputStream` for `SocketConnection` according to `bytes_in_chunk` argument
/// * calculates total bytes length on every chunk iteration, validate sum with `bytes_total_limit` argument
/// * stop reading `InputStream` with `Result` on zero bytes in chunk received
/// * applies optional callback functions:
/// * applies callback functions:
/// * `on_chunk` - return reference to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html) and `bytes_total` collected for every chunk in reading loop
/// * `on_complete` - return `MemoryInputStream` on success or `Error` on failure as `Result`
pub fn from_socket_connection_async(
@ -34,8 +34,8 @@ pub fn from_socket_connection_async(
priority: Priority,
bytes_in_chunk: usize,
bytes_total_limit: usize,
on_chunk: Option<impl Fn((&Bytes, &usize)) + 'static>,
on_complete: Option<impl FnOnce(Result<MemoryInputStream, (Error, Option<&str>)>) + 'static>,
on_chunk: impl Fn((&Bytes, &usize)) + 'static,
on_complete: impl FnOnce(Result<MemoryInputStream, (Error, Option<&str>)>) + 'static,
) {
read_all_from_socket_connection_async(
MemoryInputStream::new(),
@ -56,7 +56,6 @@ pub fn from_socket_connection_async(
/// Asynchronously read [InputStream](https://docs.gtk.org/gio/class.InputStream.html)
/// from [SocketConnection](https://docs.gtk.org/gio/class.SocketConnection.html)
/// to given [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html).
/// Applies optional `on_chunk` and `on_complete` callback functions.
///
/// Useful to create dynamically allocated, memory-safe buffer
/// from remote connections, where final size of target data could not be known by Gemini protocol restrictions.
@ -71,7 +70,7 @@ pub fn from_socket_connection_async(
/// * recursively read all bytes from `InputStream` for `SocketConnection` according to `bytes_in_chunk` argument
/// * calculates total bytes length on every chunk iteration, validate sum with `bytes_total_limit` argument
/// * stop reading `InputStream` with `Result` on zero bytes in chunk received, otherwise continue next chunk request in loop
/// * applies optional callback functions:
/// * applies callback functions:
/// * `on_chunk` - return reference to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html) and `bytes_total` collected for every chunk in reading loop
/// * `on_complete` - return `MemoryInputStream` on success or `Error` on failure as `Result`
pub fn read_all_from_socket_connection_async(
@ -82,8 +81,8 @@ pub fn read_all_from_socket_connection_async(
bytes_in_chunk: usize,
bytes_total_limit: usize,
bytes_total: usize,
on_chunk: Option<impl Fn((&Bytes, &usize)) + 'static>,
on_complete: Option<impl FnOnce(Result<MemoryInputStream, (Error, Option<&str>)>) + 'static>,
on_chunk: impl Fn((&Bytes, &usize)) + 'static,
on_complete: impl FnOnce(Result<MemoryInputStream, (Error, Option<&str>)>) + 'static,
) {
socket_connection.input_stream().read_bytes_async(
bytes_in_chunk,
@ -95,24 +94,16 @@ pub fn read_all_from_socket_connection_async(
let bytes_total = bytes_total + bytes.len();
// Callback chunk function
if let Some(ref callback) = on_chunk {
callback((&bytes, &bytes_total));
}
on_chunk((&bytes, &bytes_total));
// Validate max size
if bytes_total > bytes_total_limit {
if let Some(callback) = on_complete {
callback(Err((Error::BytesTotal, None)));
}
return; // break
return on_complete(Err((Error::BytesTotal, None)));
}
// No bytes were read, end of stream
if bytes.len() == 0 {
if let Some(callback) = on_complete {
callback(Ok(memory_input_stream));
}
return; // break
return on_complete(Ok(memory_input_stream));
}
// Write chunk bytes
@ -132,9 +123,7 @@ pub fn read_all_from_socket_connection_async(
);
}
Err(reason) => {
if let Some(callback) = on_complete {
callback(Err((Error::InputStream, Some(reason.message()))));
}
on_complete(Err((Error::InputStream, Some(reason.message()))));
}
},
);