stamp.rs

Overview

The stamp.rs file defines the Stamp struct, which represents a discrete point in a sequence or timeline, identified by a unique index and associated with a specific UTC timestamp. The primary functionality of this file is to provide a strongly typed representation of such points with comparison and ordering capabilities based on the index field. This enables the struct to be efficiently used in collections or algorithms that require sorting or equality checks.

Struct: Stamp

Description

Stamp encapsulates two key pieces of data:

Both fields are marked pub(crate), making them accessible throughout the current crate but not publicly outside it.

Derives

Fields

Field

Type

Visibility

Description

index

u64

pub(crate)

Unique identifier for ordering stamps.

timestamp

DateTime<Utc>

pub(crate)

The UTC timestamp associated with the stamp.

Usage Example

use chrono::Utc;

let stamp1 = Stamp {
    index: 1,
    timestamp: Utc::now(),
};

let stamp2 = Stamp {
    index: 2,
    timestamp: Utc::now(),
};

Trait Implementations

The file provides implementations for several standard Rust traits to enable comparison and ordering functionality based on the index field.

PartialEq and Eq

impl PartialEq for Stamp {
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index
    }
}

PartialOrd and Ord

impl Ord for Stamp {
    fn cmp(&self, other: &Self) -> Ordering {
        self.index.cmp(&other.index)
    }
}

This design means the timestamp is not considered in equality or ordering — only the index determines the ordering of Stamp instances.

Practical Impact

Because the Stamp struct implements these traits, it can be used in ordered collections such as binary heaps or sorted vectors, and it can be compared using standard comparison operators (<, >, ==, etc.).

Implementation Details

Interactions with Other Parts of the System

Diagram: Structure of Stamp and Trait Relationships

classDiagram
class Stamp {
-index: u64
-timestamp: DateTime<Utc>
+eq()
+cmp()
+partial_cmp()
}
Stamp <|-- PartialEq
Stamp <|-- Eq
Stamp <|-- PartialOrd
Stamp <|-- Ord

This diagram illustrates the Stamp struct with its key fields and the implemented comparison traits that provide equality and ordering capabilities.