Struct size::Size

source · []
pub struct Size { /* private fields */ }
Expand description

Size is the core type exposed by this crate and allows the developer to express a file size (or the general concept of a “size”) as a strongly-typed, convertible type that can be used for textual formatting (“pretty printing”) and mathematical operations.

A size can be created in terms of any supported unit and an associated numeric value of any type.

use size::Size;

// Identical sizes expressed in different units with different primitive types:
assert_eq!(Size::from_kibibytes(2_u8), Size::from_kilobytes(2.048_f64));

Implementations

Initialize a Size from the provided value, in bytes. This is a constant function and may be used in a const context.

Unlike the other “from” functions (e.g. from_kilobytes()), it is not generic because a) trait methods (required to use a generic type) may not be declared as const, and b) it’s always safe to use as i64 on whatever type you’re actually passing into from_bytes() without any (additional) loss of precision as compared to passing in an arbitrary numeric type, since there is no math required to calculate the equivalent size in bytes.

To further illustrate this point, let’s look at this hypothetical initialization of a Size from a floating-point literal: let s = Size::from_kib(2.5); - when the conversion from “2.5 KiB” to “bytes” happens internally, the result is equivalent to (2.5 * 1024.0) as i64 and yields the correct result of 2560 bytes. But if from_kib weren’t generic and you needed to use as i64 (i.e. Size::from_kib(2.5 as i64)), the calculated size in bytes would start from an already-truncated 2_i64 and yield an incorrect answer of 2048 bytes ((2.5 as i64) * 1024). However, with from_bytes(), there can be no loss of precision (or, pedantically, even truncation) when as i64 is used since the file size, expressed in bytes, must always be a whole number; this means it is safe to perform the integer conversion/rounding at the call site itself and Size::from_bytes(float_val as i64) would necessarily always yield the same result as a hypothetically generic/type-agnostic Size::from_bytes::<f64>(float_val).

Express a size in kilobytes. Actual size is 10^3 * the value.

Express a size in megabytes. Actual size is 10^6 * the value.

Express a size in gigabytes. Actual size is 10^9 * the value.

Express a size in terabytes. Actual size is 10^12 * the value.

Express a size in petabytes. Actual size is 10^15 * the value.

Express a size in exabytes. Actual size is 10^18 * the value.

Express a size in kilobytes, as a shortcut for using Size::from_kilobytes().

Express a size in megabytes, as a shortcut for using Size::from_megabytes().

Express a size in gigabytes, as a shortcut for using Size::from_gigabytes().

Express a size in terabytes, as a shortcut for using Size::from_terabytes().

Express a size in petabytes, as a shortcut for using Size::from_petabytes().

Express a size in exabytes, as a shortcut for using Size::from_exabytes().

Express a size in kibibytes. Actual size is 2^10 * the value.

Express a size in mebibytes. Actual size is 2^20 * the value.

Express a size in gibibytes. Actual size is 2^30 * the value.

Express a size in tebibytes. Actual size is 2^40 * the value.

Express a size in pebibytes. Actual size is 2^50 * the value.

Express a size in exbibytes. Actual size is 2^60 * the value.

Express a size in kibibytes, as a shortcut for using Size::from_kibibytes().

Express a size in mebibytes, as a shortcut for using Size::from_mebibytes().

Express a size in gibibytes, as a shortcut for using Size::from_gibibytes().

Express a size in tebibytes, as a shortcut for using Size::from_tebibytes().

Express a size in pebibytes, as a shortcut for using Size::from_pebibytes().

Express a size in exbibytes, as a shortcut for using Size::from_exbibytes().

Returns the effective size in bytes of the type, useful for obtaining a plain/scalar representation of the full size represented by a Size object. This always returns an i64 regardless of the underlying type originally used, to avoid (or at least mitigate) issues with integer overflow (e.g. when trying to retrieve Size::from_tb(16_i32).bytes()).

Example:

use size::Size;
assert_eq!(Size::from_mib(4_u8).bytes(), 4_194_304 as i64);

Returns a textual representation of the Size for display purposes, giving control over the returned representation’s base (see Base::Base2 and Base::Base10) and the style used to express the determined unit (see Style).

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Defined to allow multiplying an untyped integral literal by a Size, because multiplication should be commutative.

The resulting type after applying the * operator.

Performs the * operation. Read more

Defined to allow multiplying an untyped floating scalar by a Size, because multiplication should be commutative.

The resulting type after applying the * operator.

Performs the * operation. Read more

Defined to allow multiplying an untyped integral literal by a Size, because multiplication should be commutative.

The resulting type after applying the * operator.

Performs the * operation. Read more

Defined to allow multiplying an untyped floating scalar by a Size, because multiplication should be commutative.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.