Help with FileDialog in Rust

I’m really stuck here trying to find an example of how to use Gtk FileDialog in Rust.

All I’m trying to implement is a simple File/Open action.

The documentation for GtkFileDialog, says use gtk_file_dialog_open and gtk_file_dialog_open_finish, but there appears to be no implementation of gtk_file_dialog_open_finish in the Rust bindings.

If I just use a simple call back such as

    let closure = | result: Result<File, _>| {
        match result {
            Ok(file) => {
                if let Some(path) = file.path() {
                    let s = path.to_str();
                    if let Some(s) = s {
                        &buffer.clear();
                        &buffer.push_str(s);
                    }
                };
            }
            _ => {}
        };
    };

The compiler naturally objects because may outlive borrowed value `buffer` type stuff, which is true.

Is there some example anywhere of how to actually do this. I assume it needs some magical async stuff, but that sort of conflicts with Gtk not actually being thread safe.

Async methods in Rust can either use a callback-based approach or can use native Rust async. See the text_viewer example for the callback style or the video_player example for the async style. Note that async does not actually imply multithreading because futures do not have to implement Send. You can instead spawn “local” futures which are more like a form of green threads. Most futures returned by gio/gtk methods are this way because usually they are set up already to perform some task on another thread and all you need to do is await them from the UI thread.

Thanks @jfrancis, I was able to restructure my code and use the callback style.

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