| Crates.io | hlc_id |
| lib.rs | hlc_id |
| version | 0.1.6 |
| created_at | 2024-12-18 14:50:37.61131+00 |
| updated_at | 2025-07-29 20:57:16.496211+00 |
| description | A Rust library for generating and managing Hybrid Logical Clock (HLC)-based IDs. |
| homepage | https://github.com/MaloWinrhy/hlc_id |
| repository | https://github.com/MaloWinrhy/hlc_id |
| max_upload_size | |
| id | 1487866 |
| size | 25,142 |
Implemented the Display trait for HLCId (pretty base64 output).
Added HLCId::now(&mut clock) to generate an ID with the current timestamp.
Updated examples and tests to use these new features.
HLC ID is a Rust library designed to generate, manage, and encode Hybrid Logical Clock (HLC)-based identifiers. These unique, distributed, and compact identifiers facilitate event ordering in decentralized systems.
Display trait for HLCId: pretty base64 output for easy logging and debugging.HLCId::now(&mut clock): generate an ID with the current UTC timestamp easily.Add hlc_id to your Cargo.toml:
[dependencies]
hlc_id = "0.1.6"
Run cargo build to download and compile the library.
use hlc_id::{clock::HybridLogicalClock, id::HLCId};
fn main() {
let mut clock = HybridLogicalClock::new(42);
// Generate an ID with the current timestamp (UTC, ms)
let hlc_id = HLCId::now(&mut clock);
// Display as base64 (Display trait)
println!("HLC ID (base64): {}", hlc_id);
let encoded = hlc_id.encode_base64();
println!("Encoded Base64: {}", encoded);
let decoded = HLCId::decode_base64(&encoded).unwrap();
println!("Decoded HLC ID: {:?}", decoded);
}
Run the built-in tests:
cargo test
Run the example program:
cargo run --example generate_id
new(node_id: u16) -> HybridLogicalClock: Creates a new logical clock.update(&mut self, external_timestamp: u64): Updates the clock with an external timestamp.process_timestamp(&mut self, received_timestamp: u64): Adjusts the clock based on incoming event timestamps.save_state(&self, path: &str) -> std::io::Result<()>: Saves clock state to a file.load_state(path: &str) -> std::io::Result<Self>: Loads a clock from a saved state.generate(clock: &mut HybridLogicalClock, timestamp: u64) -> HLCId: Generates a new HLC-based identifier.to_u128(&self) -> u128: Converts an ID to a 128-bit integer.from_u128(id: u128) -> HLCId: Reconstructs an ID from a 128-bit integer.encode_base64(&self) -> String: Encodes the ID in Base64.decode_base64(encoded: &str) -> Result<HLCId, String>: Decodes a Base64-encoded ID.is_before(&self, other: &HLCId) -> bool: Compares two IDs to determine event order.Full API documentation is available at docs.rs.
This project is licensed under the MIT License.
Contributions are welcome. Submit issues, pull requests, or suggest improvements.
Developed by Malo Henry
GitHub: MaloHenry