Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ version.workspace = true
futures = "0.3"
futures-channel = "0.3"
futures-util = "0.3"
glib.workspace = true
libc = "0.2"
glib = { workspace = true, features = ["futures"] }
gio.workspace = true

[dependencies.async-tls]
Expand Down
29 changes: 21 additions & 8 deletions examples/gio_futures_await/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
use std::str;

use futures::prelude::*;
use gio::prelude::*;
use glib::clone;

fn main() {
let c = glib::MainContext::default();
let l = glib::MainLoop::new(Some(&c), false);

let file = gio::File::for_path("Cargo.toml");
let cancellable = gio::Cancellable::new();

let future = clone!(
#[strong]
l,
cancellable,
async move {
match read_file(file).await {
Ok(()) => (),
Err(err) => eprintln!("Got error: {err}"),
}
l.quit();
cancellable.cancel();
}
);

c.spawn_local(future);
#[cfg(unix)]
let cancel_future = clone!(
#[strong]
cancellable,
async move {
glib::unix_signal_future(libc::SIGINT).await;
eprintln!("Ctrl+C pressed, operation will be stopped!");
cancellable.cancel();
}
);
#[cfg(not(unix))]
let cancel_future = async move {};

l.run();
let _ = c
.block_on(c.spawn_local(gio::CancellableFuture::new(
futures_util::future::join(future, cancel_future),
cancellable,
)))
.expect("futures must be executed");
}

/// Throughout our chained futures, we convert all errors to strings
Expand Down
12 changes: 4 additions & 8 deletions gio/src/cancellable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,11 @@ mod tests {
fn cancellable_future_delayed() {
let ctx = glib::MainContext::new();
let c = Cancellable::new();
let (tx, rx) = oneshot::channel();
{
let future = {
let c = c.clone();
ctx.spawn_local(async move {
c.future().await;
tx.send(()).unwrap();
});
}
ctx.spawn_local(c.future())
};
std::thread::spawn(move || c.cancel()).join().unwrap();
ctx.block_on(rx).unwrap();
ctx.block_on(future).unwrap();
}
}
Loading
Loading