Using relm on stable Rust

In this whole chapter, we used Rust nightly to be able to use custom attributes, which are currently unstable. The #[widget] attribute provided by relm provides many advantages:

So it would be nice to be able to use a similar syntax on stable that provides the same advantages. And it is possible to do so, by using the relm_widget! macro. We'll rewrite the App widget to use this macro:

relm_widget! {
    impl Widget for App {
        fn init_view(&mut self) {
            self.toolbar.show_all();
        }

        fn model() -> Model {
            Model {
                adjustment: Adjustment::new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
                cover_pixbuf: None,
                cover_visible: false,
                current_duration: 0,
                current_time: 0,
                play_image: new_icon(PLAY_ICON),
                stopped: true,
            }
        }

        fn open(&self) {
            // …
        }

        // …

        fn update(&mut self, event: Msg) {
            // …
        }

        view! {
            #[name="window"]
            gtk::Window {
                title: "Rusic",
                // …
            }
        }
    }
}

As you can see, we moved the external open() method inside the implementation decorated by the relm_widget! macro. This is due to a limitation of this macro, while it allows us to use the nice syntax provided by relm on stable Rust, we cannot access the fields of the model from outside the macro. The rest is exactly the same as the previous versions.