Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type must satisfy the static lifetime

Tags:

rust

gtk

gtk-rs

I'm trying to increase the structure of a Rust and GTK-RS application, but I cannot figure out how to deal with event connections. I see that the problem is in wrong lifetime, but I do not really understand how it could be fixed.

#[derive(Debug)]
struct CreatingProfileUI {
    window: gtk::MessageDialog,
    profile_name_entry: gtk::Entry,
    add_btn: gtk::Button,
    cancel_btn: gtk::Button,
}

#[derive(Debug)]
struct UI {
    window: gtk::Window,

    // Header
    url_entry: gtk::Entry,
    open_btn: gtk::Button,

    // Body
    add_profile_btn: gtk::Button,
    remove_profile_btn: gtk::Button,
    profiles_textview: gtk::TextView,

    // Creating profile
    creating_profile: CreatingProfileUI,

    // Statusbar
    statusbar: gtk::Statusbar,
}

impl UI {
    fn init(&self) {
        self.add_profile_btn
            .connect_clicked(move |_| { &self.creating_profile.window.run(); });
    }
}

And I get this error:

error[E0477]: the type `[closure@src/main.rs:109:46: 111:6 self:&UI]` does not fulfill the required lifetime
   --> src/main.rs:109:30
    |
109 |         self.add_profile_btn.connect_clicked(move |_| {
    |                              ^^^^^^^^^^^^^^^
    |
    = note: type must satisfy the static lifetime
like image 296
skhalymon Avatar asked Sep 04 '17 14:09

skhalymon


People also ask

What is a static lifetime?

A 'static lifetime is the longest possible lifetime, and lasts for the lifetime of the running program. A 'static lifetime may also be coerced to a shorter lifetime.

Is rust a static type?

Rust is a statically typed language, which helps preventing runtime bugs, and ensures memory safety at compile time, that is with no runtime costs.

What does static mean in Rust?

A static item is similar to a constant, except that it represents a precise memory location in the program. A static is never "inlined" at the usage site, and all references to it refer to the same memory location. Static items have the static lifetime, which outlives all other lifetimes in a Rust program.

What is Anonymous lifetime rust?

'_ , the anonymous lifetime Rust 2018 allows you to explicitly mark where a lifetime is elided, for types where this elision might otherwise be unclear. To do this, you can use the special lifetime '_ much like you can explicitly mark that a type is inferred with the syntax let x: _ = ..; .


1 Answers

You can't move non-static references into GTK callbacks. You need something static or something heap allocated (e.g. in a Box/RefCell/Rc/etc.).

Callbacks are not called from the scope where you connect to the signal, but at some later point from the main loop. It is required that whatever you pass into the closure is still alive then, which would be anything 'static, heap-allocated or allocated on the stack between main and where the main loop runs. The last part can't currently be nicely expressed with Rust/GTK-rs.

See the example at the bottom in the gtk-rs docs for an example. It uses an Rc<RefCell<_>>.

like image 157
Sebastian Dröge Avatar answered Sep 28 '22 01:09

Sebastian Dröge