2

In The Rust Programming Language, it says something like:

Move semantics

There’s some more subtlety here, though: Rust ensures that there is exactly one binding to any given resource. For example, if we have a vector, we can assign it to another binding:

But I found that I can do this using gtk-rs:

let label1: gtk::Label = builder.get_object("label1").unwrap();
let label1_test: gtk::Label = builder.get_object("label1").unwrap();

Both now point to the same resource "or something happens to me."

Builder::get_object is defined as:

pub fn get_object<T: IsA<Object>>(&self, name: &str) -> Option<T> {
    unsafe {
        Option::<Object>::from_glib_none(
            ffi::gtk_builder_get_object(self.to_glib_none().0, name.to_glib_none().0))
            .and_then(|obj| obj.downcast().ok())
    }
}

Although this is not really something from Rust directly, just from gtk-rs, I was wondering if I am right and how sure is this.

Maybe it could use Rc?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Angel Angel
  • 19,670
  • 29
  • 79
  • 105

1 Answers1

0

GTK/GLib objects (GObject) implement reference counting directly, similar to the Arc type in Rust. You can safely have multiple references to the same object, and once the last one goes out of scope the object will be destroyed.

For mutability, in Rust, gtk-rs uses interior mutability (conceptually). So you can mutate every reference to the same object, even if there are multiple of them. The implementation of the objects has to handle that (and has to anyway because that's how things work in GTK/GLib in C).

Sebastian Dröge
  • 2,063
  • 11
  • 9