gdp_rs 0.0.1

A library for implementing Ghosts-of-departed-proofs pattern in rust
Documentation
use std::{borrow::Borrow, marker::PhantomData, ops::Deref};

use crate::proposition::{Evaluable, Proposition};

/// A struct representing a proven proposition about a subject value.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Proven<S, P> {
    /// Subject value.
    subject: S,

    /// Ghost of departed proof.
    _gdp: PhantomData<P>,
}

impl<S, P> AsRef<S> for Proven<S, P> {
    #[inline]
    fn as_ref(&self) -> &S {
        &self.subject
    }
}

impl<S, P> Deref for Proven<S, P> {
    type Target = S;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.subject
    }
}

impl<S, P> Borrow<S> for Proven<S, P> {
    #[inline]
    fn borrow(&self) -> &S {
        &self.subject
    }
}

impl<S, P> Proven<S, P>
where
    P: Proposition<S>,
{
    /// Convert into proposition subject value.
    #[inline]
    pub fn into_subject(self) -> S {
        self.subject
    }

    /// Get new proven proposition with given subject value, with out checks
    /// .
    /// # Safety
    ///
    /// Callers must ensure that proposition with given subject is true.
    #[inline]
    pub unsafe fn new_unchecked(subject: S) -> Self {
        Self {
            subject,
            _gdp: PhantomData,
        }
    }
}

impl<S, P> Proven<S, P>
where
    P: Proposition<S> + Evaluable<S>,
{
    /// Try to create new proven proposition with given subject value.
    pub fn try_new(subject: S) -> Result<Self, P::EvalError> {
        P::evaluate_for(&subject).map(|_| Self {
            subject,
            _gdp: PhantomData,
        })
    }
}