Module utils

Module utils 

Source
Expand description

Utility functions for CHIE Protocol operations.

Modules§

async_utils
Async utility functions.

Structs§

LruCache
A simple LRU (Least Recently Used) cache.
RetryConfig
Configuration for retry logic.
RetryConfigBuilder
Builder for RetryConfig.

Functions§

align_down
Align value down to the previous multiple of alignment - compile-time constant.
align_up
Align value up to the next multiple of alignment - compile-time constant.
average_u64
Calculate average of two u64 values without overflow.
bytes_per_sec_to_mbps
Convert bytes per second to Mbps (approximate).
bytes_to_gb
Convert bytes to GB (compile-time constant).
bytes_to_gb_f64
Convert bytes to GB as floating point (compile-time constant).
bytes_to_human_readable
Convert bytes to human-readable format (KB, MB, GB, TB).
bytes_to_kb
Convert bytes to KB (compile-time constant).
bytes_to_kb_f64
Convert bytes to KB as floating point (compile-time constant).
bytes_to_mb
Convert bytes to MB (compile-time constant).
bytes_to_mb_f64
Convert bytes to MB as floating point (compile-time constant).
bytes_to_tb_f64
Convert bytes to TB as floating point (compile-time constant).
calculate_bandwidth_gbps
Calculate bandwidth in Gbps from bytes and duration.
calculate_bandwidth_mbps
Calculate bandwidth in Mbps from bytes and duration.
calculate_percentage
Calculate percentage with two decimal places.
calculate_percentage_rounded
Calculate percentage as integer (0-100) with rounding.
chunk_size_with_overhead
Calculate chunk size with padding for encryption overhead.
cid_to_path_safe
Convert CID to path-safe format.
clamp_const
Clamp value to a range [min, max] - compile-time constant.
current_timestamp_ms
Get current Unix timestamp in milliseconds.
days_to_duration
Convert days to Duration (compile-time constant).
div_ceil
Ceiling division (divide and round up) - compile-time constant.
estimate_time_remaining
Calculate estimated time remaining based on progress.
exponential_backoff
Calculate exponential backoff delay with optional jitter.
format_duration
Format duration as human-readable string (e.g., “2h 15m 30s”).
format_duration_compact
Format duration as compact string (e.g., “2h15m”, “45s”).
format_number_with_commas
Format number with thousands separators.
gb_to_bytes
Convert GB to bytes (compile-time constant).
gb_to_mb
Calculate megabytes from gigabytes (compile-time constant).
hours_to_duration
Convert hours to Duration (compile-time constant).
is_in_range
Check if value is within range (inclusive).
is_power_of_two
Check if a number is a power of 2 - compile-time constant.
is_timestamp_recent
Check if timestamp is recent (within max_age_ms).
is_valid_cid_format
Check if CID format is valid (enhanced validation).
is_valid_identifier
Check if string is valid ASCII identifier (alphanumeric + underscore + hyphen).
is_valid_peer_id
Validate peer ID format (basic validation).
kb_to_bytes
Convert KB to bytes (compile-time constant).
larger_of
Get the larger of two u64 values (same as max_const but more descriptive name).
max_const
Return the maximum of two values - compile-time constant.
mb_to_bytes
Convert MB to bytes (compile-time constant).
mb_to_kb
Calculate kilobytes from megabytes (compile-time constant).
mbps_to_bytes_per_sec
Convert Mbps to bytes per second.
millis_to_duration
Convert milliseconds to Duration (compile-time constant).
min_const
Return the minimum of two values - compile-time constant.
minutes_to_duration
Convert minutes to Duration (compile-time constant).
normalize_cid
Normalize CID by removing invalid characters.
peer_id_hash
Calculate hash of peer ID for consistent hashing.
percentage_const
Calculate percentage as integer (0-100) - compile-time constant.
round_down_to_multiple
Round down to nearest multiple (compile-time constant).
round_up_to_multiple
Round up to nearest multiple (compile-time constant).
safe_slice
Safe string slice that doesn’t panic on invalid indices.
saturating_add_const
Saturating addition (returns u64::MAX on overflow) - compile-time constant.
saturating_mul_const
Saturating multiplication (returns u64::MAX on overflow) - compile-time constant.
saturating_sub_const
Saturating subtraction (returns 0 on underflow) - compile-time constant.
secs_to_duration
Convert seconds to Duration (compile-time constant).
smaller_of
Get the smaller of two u64 values (same as min_const but more descriptive name).
tb_to_bytes
Convert TB to bytes (compile-time constant).
tb_to_gb
Calculate gigabytes from terabytes (compile-time constant).
timestamp_age
Calculate age of a timestamp in milliseconds.
timestamp_to_duration
Convert Unix timestamp (milliseconds) to Duration since UNIX_EPOCH.
timestamp_to_systemtime
Convert timestamp to SystemTime.
truncate_string
Truncate string to maximum length with ellipsis.
truncate_with_ellipsis
Truncate string to max length and add ellipsis if needed.
validate_peer_id
Validate peer ID format and return Result.

Type Aliases§

StorageResult
Shorthand for storage operation results.
ValidationResult
Shorthand for validation results.