Skip to main content

ps_promise/
lib.rs

1mod features;
2mod implementations;
3mod methods;
4
5use thiserror::Error;
6
7use std::{future::Future, pin::Pin};
8
9pub type BoxedPromiseFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;
10
11#[must_use = "Promises don't do anything unless you await them!"]
12pub enum Promise<T, E>
13where
14    T: Unpin,
15    E: PromiseRejection,
16{
17    Pending(BoxedPromiseFuture<T, E>),
18    Resolved(T),
19    Rejected(E),
20    Consumed,
21}
22
23pub trait PromiseRejection
24where
25    Self: Send + Unpin + 'static,
26{
27    /// This method should return the error variant representing this [`Promise`] being consumed more than once.
28    fn already_consumed() -> Self;
29}
30
31#[derive(Clone, Debug, Error, Hash, PartialEq, Eq, PartialOrd, Ord)]
32pub enum WrappedPromiseRejection<E>
33where
34    E: Send + Unpin + 'static,
35{
36    #[error("This Promise was consumed already.")]
37    AlreadyConsumed,
38    #[error(transparent)]
39    Rejected(#[from] E),
40}
41
42impl<E> PromiseRejection for WrappedPromiseRejection<E>
43where
44    E: Send + Unpin + 'static,
45{
46    fn already_consumed() -> Self {
47        Self::AlreadyConsumed
48    }
49}
50
51impl<E> PromiseRejection for Vec<E>
52where
53    E: PromiseRejection,
54{
55    fn already_consumed() -> Self {
56        Self::default()
57    }
58}
59
60impl PromiseRejection for () {
61    fn already_consumed() -> Self {}
62}