pub struct GenericSize<T> { /* private fields */ }Expand description
Generic size value object with type-safe size categories
§Purpose
Type-safe size measurement that provides:
- Compile-time size category enforcement (File vs Memory vs Network)
- Shared arithmetic and conversion operations
- Zero-cost abstractions with phantom types
- Category-specific validation and constraints
§Generic Benefits
- Type Safety: Cannot mix file sizes with memory sizes at compile time
- Code Reuse: Shared implementation for all size types
- Extensibility: Easy to add new size categories
- Zero Cost: Phantom types have no runtime overhead
§Use Cases
- File size management and validation
- Memory allocation tracking
- Network bandwidth measurement
- Storage capacity planning
§Cross-Language Mapping
- Rust:
GenericSize<T>with marker types
§Examples
- Go: Separate types with shared interface
- JSON: Number with unit metadata
- SQLite: INTEGER with category column
Implementations§
Source§impl<T: SizeCategory> GenericSize<T>
impl<T: SizeCategory> GenericSize<T>
Sourcepub fn new(bytes: u64) -> Result<Self, PipelineError>
pub fn new(bytes: u64) -> Result<Self, PipelineError>
Creates a new size with category-specific validation
§Purpose
Creates a type-safe size value with category-specific validation. Uses phantom types to prevent mixing different size categories at compile time.
§Why
Type-safe sizes provide:
- Compile-time prevention of mixing file/memory/network sizes
- Category-specific validation and constraints
- Zero-cost abstractions with phantom types
- Clear API contracts for size requirements
§Arguments
bytes- Size in bytes (validated against category limits)
§Returns
Ok(GenericSize<T>)- Valid size for category TErr(PipelineError::InvalidConfiguration)- Exceeds category maximum
§Errors
Returns error when size exceeds category-specific maximum:
- File sizes: 10 TB maximum
- Memory sizes: 1 TB maximum
- Network sizes: 100 GB per transfer
- Storage sizes: essentially unlimited
§Examples
Sourcepub fn from_kb(kb: u64) -> Result<Self, PipelineError>
pub fn from_kb(kb: u64) -> Result<Self, PipelineError>
Creates size from kilobytes
Sourcepub fn from_mb(mb: u64) -> Result<Self, PipelineError>
pub fn from_mb(mb: u64) -> Result<Self, PipelineError>
Creates size from megabytes
§Purpose
Convenience constructor for creating sizes from megabyte values. Automatically converts to bytes and validates.
§Why
MB-based construction provides:
- Human-readable size specification
- Common unit for file and memory sizes
- Overflow protection during conversion
- Category validation
§Arguments
mb- Size in megabytes (1 MB = 1,048,576 bytes)
§Returns
Ok(GenericSize<T>)- Valid sizeErr(PipelineError)- Overflow or validation error
§Errors
- Multiplication overflow during byte conversion
- Category maximum exceeded
§Examples
Sourcepub fn from_gb(gb: u64) -> Result<Self, PipelineError>
pub fn from_gb(gb: u64) -> Result<Self, PipelineError>
Creates size from gigabytes
Sourcepub fn into_category<U: SizeCategory>(
self,
) -> Result<GenericSize<U>, PipelineError>
pub fn into_category<U: SizeCategory>( self, ) -> Result<GenericSize<U>, PipelineError>
Converts to a different size category (with validation)
Sourcepub fn human_readable(&self) -> String
pub fn human_readable(&self) -> String
Formats as human-readable string
Sourcepub fn checked_add(&self, other: Self) -> Result<Self, PipelineError>
pub fn checked_add(&self, other: Self) -> Result<Self, PipelineError>
Safely adds sizes (checked arithmetic)
Sourcepub fn checked_sub(&self, other: Self) -> Result<Self, PipelineError>
pub fn checked_sub(&self, other: Self) -> Result<Self, PipelineError>
Safely subtracts sizes (checked arithmetic)
Sourcepub fn validate(&self) -> Result<(), PipelineError>
pub fn validate(&self) -> Result<(), PipelineError>
Validates the size
Source§impl GenericSize<FileSizeMarker>
Specialized methods for different size categories
impl GenericSize<FileSizeMarker>
Specialized methods for different size categories
Sourcepub fn is_large_file(&self) -> bool
pub fn is_large_file(&self) -> bool
Checks if this is a large file (> 1 GB)
Sourcepub fn transfer_time_seconds(&self, bandwidth_mbps: f64) -> f64
pub fn transfer_time_seconds(&self, bandwidth_mbps: f64) -> f64
Estimates transfer time for given bandwidth (MB/s)
Source§impl GenericSize<MemorySizeMarker>
impl GenericSize<MemorySizeMarker>
Sourcepub fn is_page_aligned(&self) -> bool
pub fn is_page_aligned(&self) -> bool
Checks if this is aligned to page boundaries (typically 4KB)
Sourcepub fn round_up_to_page(&self) -> MemorySize
pub fn round_up_to_page(&self) -> MemorySize
Rounds up to next page boundary
Sourcepub fn is_reasonable_allocation(&self) -> bool
pub fn is_reasonable_allocation(&self) -> bool
Checks if size is reasonable for allocation
Source§impl GenericSize<NetworkSizeMarker>
impl GenericSize<NetworkSizeMarker>
Sourcepub fn optimal_chunk_size(&self) -> NetworkSize
pub fn optimal_chunk_size(&self) -> NetworkSize
Gets optimal chunk size for network transfer
Sourcepub fn estimated_round_trips(&self, mtu: u64) -> u64
pub fn estimated_round_trips(&self, mtu: u64) -> u64
Estimates number of network round trips
Trait Implementations§
Source§impl<T: SizeCategory> Add for GenericSize<T>
impl<T: SizeCategory> Add for GenericSize<T>
Source§type Output = GenericSize<T>
type Output = GenericSize<T>
+ operator.Source§impl<T: Clone> Clone for GenericSize<T>
impl<T: Clone> Clone for GenericSize<T>
Source§fn clone(&self) -> GenericSize<T>
fn clone(&self) -> GenericSize<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for GenericSize<T>
impl<T: Debug> Debug for GenericSize<T>
Source§impl<T: SizeCategory> Default for GenericSize<T>
impl<T: SizeCategory> Default for GenericSize<T>
Source§impl<'de, T> Deserialize<'de> for GenericSize<T>
impl<'de, T> Deserialize<'de> for GenericSize<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: SizeCategory> Display for GenericSize<T>
impl<T: SizeCategory> Display for GenericSize<T>
Source§impl<T: SizeCategory> Div<u64> for GenericSize<T>
impl<T: SizeCategory> Div<u64> for GenericSize<T>
Source§impl<T> From<GenericSize<T>> for u64
impl<T> From<GenericSize<T>> for u64
Source§fn from(size: GenericSize<T>) -> Self
fn from(size: GenericSize<T>) -> Self
Source§impl<T> From<u64> for GenericSize<T>
impl<T> From<u64> for GenericSize<T>
Source§impl<T: Hash> Hash for GenericSize<T>
impl<T: Hash> Hash for GenericSize<T>
Source§impl<T: SizeCategory> Mul<u64> for GenericSize<T>
impl<T: SizeCategory> Mul<u64> for GenericSize<T>
Source§impl<T: Ord> Ord for GenericSize<T>
impl<T: Ord> Ord for GenericSize<T>
Source§fn cmp(&self, other: &GenericSize<T>) -> Ordering
fn cmp(&self, other: &GenericSize<T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for GenericSize<T>
impl<T: PartialEq> PartialEq for GenericSize<T>
Source§impl<T: PartialOrd> PartialOrd for GenericSize<T>
impl<T: PartialOrd> PartialOrd for GenericSize<T>
Source§impl<T> Serialize for GenericSize<T>
impl<T> Serialize for GenericSize<T>
Source§impl<T: SizeCategory> Sub for GenericSize<T>
impl<T: SizeCategory> Sub for GenericSize<T>
Source§type Output = GenericSize<T>
type Output = GenericSize<T>
- operator.impl<T: Copy> Copy for GenericSize<T>
impl<T: Eq> Eq for GenericSize<T>
impl<T> StructuralPartialEq for GenericSize<T>
Auto Trait Implementations§
impl<T> Freeze for GenericSize<T>
impl<T> RefUnwindSafe for GenericSize<T>where
T: RefUnwindSafe,
impl<T> Send for GenericSize<T>where
T: Send,
impl<T> Sync for GenericSize<T>where
T: Sync,
impl<T> Unpin for GenericSize<T>where
T: Unpin,
impl<T> UnwindSafe for GenericSize<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more