ps-promise 0.1.0-16

Promise-like owned futures
Documentation
mod features;
mod implementations;
mod methods;

use thiserror::Error;

use std::{future::Future, pin::Pin};

pub type BoxedPromiseFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;

#[must_use = "Promises don't do anything unless you await them!"]
pub enum Promise<T, E>
where
    T: Unpin,
    E: PromiseRejection,
{
    Pending(BoxedPromiseFuture<T, E>),
    Resolved(T),
    Rejected(E),
    Consumed,
}

pub trait PromiseRejection
where
    Self: Send + Unpin + 'static,
{
    /// This method should return the error variant representing this [`Promise`] being consumed more than once.
    fn already_consumed() -> Self;
}

#[derive(Clone, Debug, Error, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum WrappedPromiseRejection<E>
where
    E: Send + Unpin + 'static,
{
    #[error("This Promise was consumed already.")]
    AlreadyConsumed,
    #[error(transparent)]
    Rejected(#[from] E),
}

impl<E> PromiseRejection for WrappedPromiseRejection<E>
where
    E: Send + Unpin + 'static,
{
    fn already_consumed() -> Self {
        Self::AlreadyConsumed
    }
}

impl<E> PromiseRejection for Vec<E>
where
    E: PromiseRejection,
{
    fn already_consumed() -> Self {
        Self::default()
    }
}

impl PromiseRejection for () {
    fn already_consumed() -> Self {}
}