Skip to main content

commonware_actor/
lib.rs

1//! Safely coordinate concurrent components.
2//!
3//! # Status
4//!
5//! Stability varies by primitive. See [README](https://github.com/commonwarexyz/monorepo#stability) for details.
6
7#![doc(
8    html_logo_url = "https://commonware.xyz/imgs/rustdoc_logo.svg",
9    html_favicon_url = "https://commonware.xyz/favicon.ico"
10)]
11
12commonware_macros::stability_scope!(BETA {
13    /// Feedback from submitting work to a bounded endpoint.
14    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
15    pub enum Feedback {
16        /// The work was accepted within the configured capacity.
17        Ok,
18        /// The submission exceeded configured capacity but was handled by the overflow policy.
19        Backoff,
20        /// The endpoint is closed.
21        Closed,
22    }
23
24    impl Feedback {
25        /// Returns `true` when the endpoint handled the submission.
26        pub const fn accepted(self) -> bool {
27            matches!(self, Self::Ok | Self::Backoff)
28        }
29    }
30
31    /// Feedback from endpoints that may reject work under backpressure.
32    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
33    pub enum Unreliable<T> {
34        /// Endpoint outcome from the submission attempt.
35        Outcome(T),
36        /// The work was rejected by the endpoint.
37        Rejected,
38    }
39
40    impl<T> Unreliable<T> {
41        /// Wrap an outcome for an operation that may reject work.
42        pub const fn new(outcome: T) -> Self {
43            Self::Outcome(outcome)
44        }
45
46        /// Create a rejected result.
47        pub const fn rejected() -> Self {
48            Self::Rejected
49        }
50
51        /// Returns `true` when the operation was rejected before producing an outcome.
52        pub const fn is_rejected(&self) -> bool {
53            matches!(self, Self::Rejected)
54        }
55
56        /// Returns the outcome when the operation was not rejected.
57        pub fn outcome(self) -> Option<T> {
58            match self {
59                Self::Outcome(outcome) => Some(outcome),
60                Self::Rejected => None,
61            }
62        }
63    }
64
65    impl Unreliable<Feedback> {
66        /// Returns `true` when the endpoint handled the submission.
67        pub const fn accepted(self) -> bool {
68            match self {
69                Self::Outcome(feedback) => feedback.accepted(),
70                Self::Rejected => false,
71            }
72        }
73    }
74
75    pub mod mailbox;
76});