fluxion-core 0.8.0

Core traits and types for ordered stream processing
Documentation
// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

use crate::HasTimestamp;

/// A trait for types that have an intrinsic timestamp for stream ordering.
///
/// This trait extends [`HasTimestamp`] with the ability to construct and
/// deconstruct timestamped wrapper types. Use this for wrapper types like
/// `Sequenced<T>` that wrap an inner value with a timestamp.
///
/// For types that only need to provide a timestamp value without wrapping,
/// implement [`HasTimestamp`] instead.
///
/// This trait allows stream operators like `combine_latest` and `ordered_merge`
/// to work with any type that can provide a timestamp value and wraps an inner value.
///
/// The timestamp type is generic, allowing implementations to use different time
/// representations (monotonic counters, wall-clock time, etc.).
///
/// # Type Parameters
/// * `Inner` - The type of the value wrapped by this timestamped type
/// * `Timestamp` - The type representing the timestamp (must be `Ord + Copy`)
///
/// # Examples
///
/// ```
/// use fluxion_core::{Timestamped, HasTimestamp};
///
/// #[derive(Clone, Debug)]
/// struct TimestampedEvent<T> {
///     value: T,
///     timestamp: u64,
/// }
///
/// impl<T: Clone> HasTimestamp for TimestampedEvent<T> {
///     type Timestamp = u64;
///
///     fn timestamp(&self) -> Self::Timestamp {
///         self.timestamp
///     }
/// }
///
/// impl<T: Clone> Timestamped for TimestampedEvent<T> {
///     type Inner = T;
///
///     fn with_timestamp(value: T, timestamp: Self::Timestamp) -> Self {
///         TimestampedEvent { value, timestamp }
///     }
///
///     fn into_inner(self) -> Self::Inner {
///         self.value
///     }
/// }
/// ```
///
/// # Different Timestamp Types
///
/// The `Timestamp` type is generic and can represent various time sources:
///
/// **Monotonic counters** (u64, u128) - For test scenarios and event sourcing:
/// ```
/// use fluxion_core::{Timestamped, HasTimestamp};
///
/// #[derive(Clone, Debug)]
/// struct SequenceNumbered<T> {
///     value: T,
///     seq: u64,
/// }
///
/// impl<T: Clone> HasTimestamp for SequenceNumbered<T> {
///     type Timestamp = u64;
///     fn timestamp(&self) -> u64 { self.seq }
/// }
///
/// impl<T: Clone> Timestamped for SequenceNumbered<T> {
///     type Inner = T;
///     fn with_timestamp(value: T, seq: u64) -> Self { Self { value, seq } }
///     fn into_inner(self) -> T { self.value }
/// }
/// ```
///
/// **Wall-clock time** (Instant, SystemTime) - For real-time systems:
/// ```
/// use fluxion_core::{Timestamped, HasTimestamp};
/// use std::time::Instant;
///
/// #[derive(Clone, Debug)]
/// struct TimedEvent<T> {
///     value: T,
///     time: Instant,
/// }
///
/// impl<T: Clone> HasTimestamp for TimedEvent<T> {
///     type Timestamp = Instant;
///     fn timestamp(&self) -> Instant { self.time }
/// }
///
/// impl<T: Clone> Timestamped for TimedEvent<T> {
///     type Inner = T;
///     fn with_timestamp(value: T, time: Instant) -> Self { Self { value, time } }
///     fn into_inner(self) -> T { self.value }
/// }
/// ```
pub trait Timestamped: HasTimestamp + Clone {
    /// The type of the inner value wrapped by this timestamped type
    type Inner: Clone;

    /// Creates a new instance wrapping the given value with the specified timestamp.
    fn with_timestamp(value: Self::Inner, timestamp: Self::Timestamp) -> Self;

    /// Consumes self and returns the inner value.
    /// For wrapper types like `Sequenced<T>`, this extracts `T`.
    /// For domain types where `Inner = Self`, this typically returns `self`.
    fn into_inner(self) -> Self::Inner;
}