pub struct Size(/* private fields */);Expand description
A non-negative byte count.
Size is the type used for budgets. It is parsed from human strings such
as "280 KB", "2.5 MB" or "10700 B", and it renders back losslessly
so that a config file survives a load/save round trip byte for byte.
§Examples
use wasmcheck::Size;
let size: Size = "280 KB".parse()?;
assert_eq!(size.bytes(), 280 * 1024);
assert_eq!(size.to_string(), "280 KB");
// An exact file measurement keeps its exact value.
let odd = Size::from_bytes(10_700);
assert_eq!(odd.to_string(), "10700 B");
assert_eq!("10700 B".parse::<Size>()?, odd);Implementations§
Source§impl Size
impl Size
Sourcepub const fn from_bytes(bytes: u64) -> Self
pub const fn from_bytes(bytes: u64) -> Self
Wraps a raw byte count.
Sourcepub const fn from_kib_ceil(bytes: u64) -> Self
pub const fn from_kib_ceil(bytes: u64) -> Self
The smallest whole-KiB size that is greater than or equal to bytes.
This is how init derives a starting budget: rounding up guarantees
the freshly written config does not fail its own first check.
§Examples
use wasmcheck::Size;
assert_eq!(Size::from_kib_ceil(10_700).bytes(), 11 * 1024);
assert_eq!(Size::from_kib_ceil(10_240).bytes(), 10 * 1024);
assert_eq!(Size::from_kib_ceil(0).bytes(), 0);Trait Implementations§
impl Copy for Size
Source§impl<'de> Deserialize<'de> for Size
impl<'de> Deserialize<'de> for Size
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>,
impl Eq for Size
Source§impl FromStr for Size
impl FromStr for Size
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses a size such as "280 KB", "2.5 MB", "512" or "10700 B".
Units are case-insensitive and "B", "KB"/"KiB" and "MB"/"MiB"
are understood. Negative, non-finite and out-of-range values are
rejected instead of silently saturating.
§Errors
Returns WasmCheckError::InvalidSize when the string is not a
non-negative number followed by an optional known unit.
§Examples
use wasmcheck::Size;
assert_eq!("1.5 KB".parse::<Size>()?.bytes(), 1536);
assert!("-1 KB".parse::<Size>().is_err());
assert!("10 GB".parse::<Size>().is_err());
assert!("nan".parse::<Size>().is_err());Source§type Err = WasmCheckError
type Err = WasmCheckError
Source§impl Ord for Size
impl Ord for Size
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Size
impl PartialOrd for Size
impl StructuralPartialEq for Size
Auto Trait Implementations§
impl Forget for Size
impl Freeze for Size
impl RefUnwindSafe for Size
impl Send for Size
impl Sync for Size
impl Unpin for Size
impl UnsafeUnpin for Size
impl UnwindSafe for Size
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.