Does MemoryInputStream::add_bytes make copy of Bytes referenced?

Please explain what’s happening with Bytes after MemoryInputStream::add_bytes

I see this argument referenced (&Bytes), but seems that MemoryInputStream create a copy inside, because I can drop these Bytes later.

Or Bytes is just smart pointer?

Thanks

It just means a pointer to the GBytes data (in C). Not sure about Rust

1 Like

Thanks much for reply!

p.s. if that’s pointer on C level, it’s a pointer for any other wrapper (rust-rs including)

https://docs.gtk.org/gio/method.MemoryInputStream.add_bytes.html (which you linked above) contains the C API, though the doc is structured in a C++ like fashion.

You can refer to it in future.

I’m usually reading docs word-by-word, but not always understand it context, because beginner in C, Rust, and GTK at all. I’m web-dev, so it’s not always simple to understand after those trivial web-tech languages :slight_smile:

Appends bytes to data that can be read from the input stream.

I don’t know how exactly it appends (create new Bytes inside or take the reference to the current data). If that’s pointer, it means that maybe reference counter increasing on C side and I can drop this Bytes pointer on complete without risk to lost the data referenced.

Check the documentation of the Rust bindings:

GLib type: Shared boxed type with reference counted clone semantics.

2 Likes

Thanks for link, for some reasons yet reading docs for C, maybe it’s time to change my mind

I’ve just created related subject on Rust forum about Bytes and GString pointers - because I still not sure that understand subject from documentation

even it’s shame, but maybe it’s better to ask than produce wrong implementations…

to be closer, some function read data from stream and track bytes downloaded. it includes callback function with Bytes until Bytes still in use. I can return reference or just clone.

pub fn read_all_from_socket_connection_async(
    memory_input_stream: MemoryInputStream,
    socket_connection: SocketConnection,
    cancelable: Option<Cancellable>,
    priority: Priority,
    bytes_in_chunk: usize,
    bytes_total_limit: usize,
    bytes_total: usize,
    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,
        priority,
        cancelable.clone().as_ref(),
        move |result| match result {
            Ok(bytes) => {
                // Update bytes total
                let bytes_total = bytes_total + bytes.len();

                // Callback chunk function
                on_chunk((bytes, bytes_total)); // need clone or just &ref

                // some other deals with bytes..

                // Write chunk bytes
                memory_input_stream.add_bytes(&bytes);

doubts to use for on_chunk callback

on_chunk: impl Fn((Bytes, usize))

or

on_chunk: impl Fn((&Bytes, usize))

on_chunk function should return Bytes always (on every iteration), so stuck with that, even can simply return as &ref and forget about.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.