Skip to content

Commit 09d576e

Browse files
committed
gio/cancellable-future: Add docs and examples for the use with GioFuture
1 parent f96e31a commit 09d576e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

gio/src/cancellable_future.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ pin_project! {
1919
// rustdoc-stripper-ignore-next
2020
/// A future which can be cancelled via [`Cancellable`].
2121
///
22+
/// It can be used to cancel one or multiple Gio futures that support
23+
/// internal cancellation via [`Cancellable`] by using this future to
24+
/// execute cancellable promises that are created (implicitly or explicitly)
25+
/// via [`GioFuture`].
26+
///
2227
/// # Examples
2328
///
2429
/// ```
@@ -32,6 +37,42 @@ pin_project! {
3237
/// c.cancel();
3338
///
3439
/// ```
40+
///
41+
/// As said the [`CancellableFuture`] can be used to handle Gio futures,
42+
/// relying on an actual [`Cancellable`] instance of with a new one.
43+
///
44+
/// ```no_run
45+
/// # use futures::FutureExt;
46+
/// # use gio::prelude::*;
47+
/// # use gio::CancellableFuture;
48+
/// # async {
49+
/// CancellableFuture::new(
50+
/// async {
51+
/// let file = gio::File::for_path("/dev/null");
52+
/// let file_info = file
53+
/// .query_info_future(
54+
/// gio::FILE_ATTRIBUTE_STANDARD_NAME,
55+
/// gio::FileQueryInfoFlags::NONE,
56+
/// glib::Priority::default(),
57+
/// )
58+
/// .await?;
59+
///
60+
/// // Sub-cancellable chains are also working as expected.
61+
/// // The new cancellable will have its own scope, but will also be
62+
/// // cancelled when the parent cancellable is cancelled.
63+
/// let io_stream = CancellableFuture::new(
64+
/// file.open_readwrite_future(glib::Priority::default()),
65+
/// gio::Cancellable::new(),
66+
/// )
67+
/// .await??;
68+
/// // [...]
69+
/// Ok::<bool, glib::Error>(true)
70+
/// },
71+
/// gio::Cancellable::new(),
72+
/// )
73+
/// .await
74+
/// # };
75+
/// ```
3576
pub struct CancellableFuture<F> {
3677
#[pin]
3778
future: F,

0 commit comments

Comments
 (0)