GenericSize

Struct GenericSize 

Source
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>

Source

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 T
  • Err(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
Source

pub fn zero() -> Self

Creates a zero size

Source

pub fn from_kb(kb: u64) -> Result<Self, PipelineError>

Creates size from kilobytes

Source

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 size
  • Err(PipelineError) - Overflow or validation error
§Errors
  • Multiplication overflow during byte conversion
  • Category maximum exceeded
§Examples
Source

pub fn from_gb(gb: u64) -> Result<Self, PipelineError>

Creates size from gigabytes

Source

pub fn bytes(&self) -> u64

Gets the raw byte count

Source

pub fn category(&self) -> &'static str

Gets the size category name

Source

pub fn into_category<U: SizeCategory>( self, ) -> Result<GenericSize<U>, PipelineError>

Converts to a different size category (with validation)

Source

pub fn as_kb(&self) -> u64

Gets size as kilobytes

Source

pub fn as_mb(&self) -> u64

Gets size as megabytes

Source

pub fn as_gb(&self) -> u64

Gets size as gigabytes

Source

pub fn as_mb_f64(&self) -> f64

Gets size as floating point megabytes

Source

pub fn as_gb_f64(&self) -> f64

Gets size as floating point gigabytes

Source

pub fn is_zero(&self) -> bool

Checks if size is zero

Source

pub fn human_readable(&self) -> String

Formats as human-readable string

Source

pub fn checked_add(&self, other: Self) -> Result<Self, PipelineError>

Safely adds sizes (checked arithmetic)

Source

pub fn checked_sub(&self, other: Self) -> Result<Self, PipelineError>

Safely subtracts sizes (checked arithmetic)

Source

pub fn validate(&self) -> Result<(), PipelineError>

Validates the size

Source§

impl GenericSize<FileSizeMarker>

Specialized methods for different size categories

Source

pub fn is_large_file(&self) -> bool

Checks if this is a large file (> 1 GB)

Source

pub fn transfer_time_seconds(&self, bandwidth_mbps: f64) -> f64

Estimates transfer time for given bandwidth (MB/s)

Source§

impl GenericSize<MemorySizeMarker>

Source

pub fn is_page_aligned(&self) -> bool

Checks if this is aligned to page boundaries (typically 4KB)

Source

pub fn round_up_to_page(&self) -> MemorySize

Rounds up to next page boundary

Source

pub fn is_reasonable_allocation(&self) -> bool

Checks if size is reasonable for allocation

Source§

impl GenericSize<NetworkSizeMarker>

Source

pub fn optimal_chunk_size(&self) -> NetworkSize

Gets optimal chunk size for network transfer

Source

pub fn estimated_round_trips(&self, mtu: u64) -> u64

Estimates number of network round trips

Source§

impl GenericSize<StorageSizeMarker>

Source

pub fn as_tb(&self) -> u64

Converts to storage units (TB, PB)

Source

pub fn monthly_cost(&self, cost_per_gb: f64) -> f64

Gets storage cost estimate ($/month at given rate per GB)

Trait Implementations§

Source§

impl<T: SizeCategory> Add for GenericSize<T>

Source§

type Output = GenericSize<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: GenericSize<T>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for GenericSize<T>

Source§

fn clone(&self) -> GenericSize<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for GenericSize<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: SizeCategory> Default for GenericSize<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, T> Deserialize<'de> for GenericSize<T>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: SizeCategory> Display for GenericSize<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: SizeCategory> Div<u64> for GenericSize<T>

Source§

type Output = GenericSize<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u64) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> From<GenericSize<T>> for u64

Source§

fn from(size: GenericSize<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<u64> for GenericSize<T>

Source§

fn from(bytes: u64) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for GenericSize<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: SizeCategory> Mul<u64> for GenericSize<T>

Source§

type Output = GenericSize<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u64) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Ord> Ord for GenericSize<T>

Source§

fn cmp(&self, other: &GenericSize<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for GenericSize<T>

Source§

fn eq(&self, other: &GenericSize<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for GenericSize<T>

Source§

fn partial_cmp(&self, other: &GenericSize<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Serialize for GenericSize<T>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: SizeCategory> Sub for GenericSize<T>

Source§

type Output = GenericSize<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: GenericSize<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy> Copy for GenericSize<T>

Source§

impl<T: Eq> Eq for GenericSize<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,