Module std_time

Module std_time 

Source
Expand description

Standard library time source implementation using std::time::Instant.

This module provides StdTimeSource, a TimeSource implementation that uses std::time::Instant for real-time operation. It follows the “elapsed time” model where time is measured relative to when the TimeSource was created.

§Features

  • Real Time: Uses actual system time via std::time::Instant
  • Monotonic: Guaranteed monotonic time progression
  • High Precision: Nanosecond precision from std::time
  • Cross Platform: Works on all platforms supported by std::time

§Usage

This implementation is only available when the std-time feature is enabled. It’s designed for production use where real system time is required.

§Examples

§Basic Usage

use rate_guard::time_source::{TimeSource, StdTimeSource};
use std::time::Duration;

let time_source = StdTimeSource::new();
let start = time_source.now();
 
// Some time passes...
std::thread::sleep(Duration::from_millis(100));
 
let elapsed = time_source.now();
assert!(elapsed >= start + Duration::from_millis(90)); // Allow some tolerance

§Integration with Rate Limiters

use rate_guard::{Nanos, StdTimeSource, RateLimit};
use rate_guard::limits::TokenBucketBuilder;
use std::time::Duration;

let bucket = TokenBucketBuilder::builder()
    .capacity(100)
    .refill_amount(10)
    .refill_every(Duration::from_millis(100))
    .with_time(StdTimeSource::new())
    .with_precision::<Nanos>()
    .build()
    .unwrap();

// Use with real system time
match bucket.try_acquire(10) {
    Ok(()) => println!("Request allowed"),
    Err(e) => println!("Rate limited: {}", e),
}

Structs§

StdTimeSource
Standard library time source using std::time::Instant.