Skip to main content

oneshot_fused_workaround/
lib.rs

1//! Thin veneer over `futures::channel::oneshot` to fix use with [`select!`](futures::select)
2//!
3//! A bare [`futures::channel::oneshot::Receiver`] doesn't work properly with
4//! `futures::select!`, because it has a broken
5//! [`FusedFuture`](futures::future::FusedFuture)
6//! implementation.
7//! (See [`futures-rs` ticket #2455](https://github.com/rust-lang/futures-rs/issues/2455).)
8//!
9//! Wrapping it up in a [`future::Fuse`](futures::future::Fuse) works around this,
10//! with a minor performance penalty.
11//!
12//! ### Limitations
13//!
14//! The API of this [`Receiver`] is rather more limited.
15//! For example, it lacks `.try_recv()`.
16//
17// The veneer is rather thin and the types from `futures-rs` show through.
18// If we change this in the future, it will be a breaking change.
19#![cfg_attr(docsrs, feature(doc_cfg))]
20#![doc = include_str!("../README.md")]
21// @@ begin lint list maintained by maint/add_warning @@
22#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
23#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
24#![warn(missing_docs)]
25#![warn(noop_method_call)]
26#![warn(unreachable_pub)]
27#![warn(clippy::all)]
28#![deny(clippy::await_holding_lock)]
29#![deny(clippy::cargo_common_metadata)]
30#![deny(clippy::cast_lossless)]
31#![deny(clippy::checked_conversions)]
32#![allow(clippy::cognitive_complexity)] // See arti#2556
33#![deny(clippy::debug_assert_with_mut_call)]
34#![deny(clippy::exhaustive_enums)]
35#![deny(clippy::exhaustive_structs)]
36#![deny(clippy::expl_impl_clone_on_copy)]
37#![deny(clippy::fallible_impl_from)]
38#![deny(clippy::implicit_clone)]
39#![deny(clippy::large_stack_arrays)]
40#![warn(clippy::manual_ok_or)]
41#![deny(clippy::missing_docs_in_private_items)]
42#![warn(clippy::needless_borrow)]
43#![warn(clippy::needless_pass_by_value)]
44#![warn(clippy::option_option)]
45#![warn(clippy::or_fun_call)]
46#![deny(clippy::print_stderr)]
47#![deny(clippy::print_stdout)]
48#![warn(clippy::rc_buffer)]
49#![deny(clippy::ref_option_ref)]
50#![warn(clippy::semicolon_if_nothing_returned)]
51#![warn(clippy::trait_duplication_in_bounds)]
52#![deny(clippy::unchecked_time_subtraction)]
53#![deny(clippy::unnecessary_wraps)]
54#![warn(clippy::unseparated_literal_suffix)]
55#![deny(clippy::unwrap_used)]
56#![deny(clippy::mod_module_files)]
57#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
58#![allow(clippy::uninlined_format_args)]
59#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
60#![allow(clippy::result_large_err)] // temporary workaround for arti#587
61#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
62#![allow(clippy::needless_lifetimes)] // See arti#1765
63#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
64#![allow(clippy::collapsible_if)] // See arti#2342
65#![deny(clippy::unused_async)]
66#![deny(clippy::string_slice)] // See arti#2571
67#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
68//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
69
70// TODO: add to the list maintained by maint/add_warning once it's available on all crates
71#![warn(unnameable_types)]
72
73use futures::FutureExt as _;
74use futures::channel::oneshot as fut_oneshot;
75
76pub use fut_oneshot::Canceled;
77
78/// `oneshot::Sender` type alias
79//
80// This has to be `pub type` rather than `pub use` so that
81// (i) call sites don't trip the "disallowed types" lint
82// (ii) we can apply a fine-grained allow, here.
83#[allow(clippy::disallowed_types)]
84pub type Sender<T> = fut_oneshot::Sender<T>;
85
86/// `oneshot::Receiver` that works properly with [`futures::select!`]
87#[allow(clippy::disallowed_types)]
88pub type Receiver<T> = futures::future::Fuse<fut_oneshot::Receiver<T>>;
89
90/// Return a fresh oneshot channel
91pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
92    #[allow(clippy::disallowed_methods)]
93    let (tx, rx) = fut_oneshot::channel();
94    (tx, rx.fuse())
95}