sassi 0.1.0-alpha.2

Typed in-memory pool with composable predicate algebra and cross-runtime trait queries.
Documentation
//! Monotonic watermark contracts for delta-sync refreshes.
//!
//! A watermark is the value a delta source uses to say "everything at
//! or after this point may have changed." Sassi keeps this trait narrow:
//! only types with a clear total order and cheap ownership semantics
//! should implement it.

use crate::Cacheable;

/// Marker trait for values that can safely order delta-sync progress.
///
/// This trait is intentionally not blanket-implemented for every
/// `Ord + Clone` type. Types such as `bool`, `()`, or
/// `std::time::Duration` have an ordering, but they do not carry enough
/// meaning to act as a production delta-sync cursor.
pub trait MonotonicWatermark: Ord + Clone + Send + Sync + 'static {}

macro_rules! impl_numeric_watermark {
    ($($ty:ty),* $(,)?) => {
        $(
            impl MonotonicWatermark for $ty {}
        )*
    };
}

impl_numeric_watermark!(i8, i16, i32, i64, i128, isize);
impl_numeric_watermark!(u8, u16, u32, u64, u128, usize);

impl MonotonicWatermark for std::time::SystemTime {}

impl<A, B> MonotonicWatermark for (A, B)
where
    A: MonotonicWatermark,
    B: MonotonicWatermark,
{
}

impl<A, B, C> MonotonicWatermark for (A, B, C)
where
    A: MonotonicWatermark,
    B: MonotonicWatermark,
    C: MonotonicWatermark,
{
}

impl<A, B, C, D> MonotonicWatermark for (A, B, C, D)
where
    A: MonotonicWatermark,
    B: MonotonicWatermark,
    C: MonotonicWatermark,
    D: MonotonicWatermark,
{
}

#[cfg(feature = "watermark-time")]
mod watermark_time {
    use super::MonotonicWatermark;

    impl MonotonicWatermark for time::Date {}
    impl MonotonicWatermark for time::OffsetDateTime {}
    impl MonotonicWatermark for time::PrimitiveDateTime {}
}

#[cfg(feature = "watermark-chrono")]
mod watermark_chrono {
    use super::MonotonicWatermark;

    impl MonotonicWatermark for chrono::DateTime<chrono::FixedOffset> {}
    impl MonotonicWatermark for chrono::DateTime<chrono::Utc> {}
    impl MonotonicWatermark for chrono::NaiveDate {}
    impl MonotonicWatermark for chrono::NaiveDateTime {}
}

/// Extension of [`Cacheable`] for models that expose a delta-sync cursor.
///
/// `DeltaSyncCacheable` is generated by `#[derive(Cacheable)]` when the
/// model uses `#[cacheable(watermark_field = "...")]`. Hand-written
/// implementations are also supported for models with computed cursors.
pub trait DeltaSyncCacheable: Cacheable {
    /// The monotonic cursor type returned by [`Self::watermark`].
    type Watermark: MonotonicWatermark;

    /// Return the model's current delta-sync cursor.
    fn watermark(&self) -> Self::Watermark;
}