2017-09-04 1 views
2

私はRustとGTK-RSアプリケーションの構造を増やそうとしていますが、イベント接続の処理方法を理解することはできません。私は問題が生涯に間違っているのを見ていますが、どのように修正できるのか本当に分かりません。タイプは静的寿命を満たさなければなりません

#[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(); }); 
    } 
} 

そして、私はこのエラーを取得する:

error[E0477]: the type `[[email protected]/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 
+1

['ButtonExt :: connect_clicked'](https://docs.rs/gtk/0.2.0/gtk/trait.ButtonExt.html#tymethod.connect_clicked)には、' 'static'存続時間の関数が必要です。関連(スレッドの周りは同じですがエラーは同じです):https://stackoverflow.com/a/28661524/1233251 –

答えて

4

あなたはGTKコールバックに非静的参照を移動することはできません。静的なものやヒープの割り当てが必要です(例えば、Box/RefCell/Rc/etc)。

コールバックは、信号に接続するスコープから呼び出されるのではなく、メインループの後のある時点で呼び出されます。クロージャーに渡すものはまだ生きていて、何でも'staticであり、メインとメインループが実行される場所の間でスタックにヒープ割り当てまたは割り当てられている必要があります。最後の部分は現在、Rust/GTK-rsでうまく表現できません。

the example at the bottom in the gtk-rs docs for an exampleを参照してください。それはRc<RefCell<_>>を使用します。

+0

実装の例https://github.com/hfiguiere/gpsami/blob/f4e612dcaa35648d033846027e74903cdf62b7a4/src/mgapplication .rs#L96 – Infernion

関連する問題