1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! # Overview
//!
//! A crate to complete futures via a remote handle.
//!
//! Although it's design shouldn't restrain `future_handles` use cases, this crate was primarily
//! conceived to bridge asynchronous functions running callbacks at competition with rust's
//! async/await paradigm.
//!
//! # Features
//! - No locking overhead for single-threaded environments.
//! - Optional thread-safety with spin-locks.
//! - Futures always complete with an error if the handle is dropped.
//! - Both channel-like and scoped APIs.
//!
//! ## Examples
//!
//! Channel-like API:
//! ```rust
//! # use future_handles::unsync;
//! async fn func() -> Option<u32> {
//! let (future, handle) = unsync::create();
//!
//! func_with_callback(|res| {
//! handle.complete(res);
//! });
//!
//! future.await.ok()
//! }
//! # fn func_with_callback<F>(func: F)
//! # where F: FnOnce(u32) {
//! # func(1);
//! # }
//! ```
//!
//! Scoped API:
//! ```rust
//! # use future_handles::unsync;
//! async fn func() -> Option<u32> {
//! let future = unsync::scoped(|handle| {
//! func_with_callback(|res| {
//! handle.complete(res);
//! });
//! });
//!
//! future.await.ok()
//! }
//! # fn func_with_callback<F>(func: F)
//! # where F: FnOnce(u32) {
//! # func(1);
//! # }
//! ```
//!
//! ## Thread safety
//!
//! This crate comes a non thread-safe [`unsync`] implementation by default. To make the [`sync`]
//! thread-safe implementation available, enable the `sync` feature.
//!
//! [`unsync`]: unsync
//! [`sync`]: sync
//!
//! # Danger!
//!
//! Do **NOT** do this!
//! ```rust
//! # use future_handles::unsync;
//! async fn func() {
//! let (future, handle) = unsync::create();
//!
//! // Start awaiting here...
//! future.await.unwrap();
//! // Now we'll never be able set the result!
//! handle.complete(1);
//! }
//! ```
//! Awaiting a `CompletableFuture` before setting the result or dropping the associated
//! `CompleteHandle` will cause a **deadlock**!
// Enable nightly feature doc_cfg.
use Error;
/// The error returned by `CompletableFuture`s.
/// A convenience type to wrap a [`HandleError`] in a [`Result`].
///
/// [`HandleError`]: crate::HandleError
/// [`Result`]: std::result::Result
pub type HandleResult<T> = ;