Struct byte_unit::Byte

source ·
pub struct Byte(_);
Expand description

Represent the n-bytes data. Use associated functions: from_unit, from_bytes, from_str, to create the instance.

Implementations§

source§

impl Byte

source

pub fn from_unit(value: f64, unit: ByteUnit) -> Result<Byte, ByteError>

Create a new Byte object from a specified value and a unit. Accuracy should be taken care of.

Examples
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_unit(1500f64, ByteUnit::KB).unwrap();

assert_eq!(1500000, result.get_bytes());
source

pub const fn from_bytes(bytes: u128) -> Byte

Create a new Byte object from bytes.

Examples
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_bytes(1500000);

assert_eq!(1500000, result.get_bytes());
source

pub fn from_str<S: AsRef<str>>(s: S) -> Result<Byte, ByteError>

Create a new Byte object from string. Accuracy should be taken care of.

Examples
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("123KiB").unwrap();

assert_eq!(Byte::from_unit(123f64, ByteUnit::KiB).unwrap(), result);
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("50.84 MB").unwrap();

assert_eq!(Byte::from_unit(50.84f64, ByteUnit::MB).unwrap(), result);
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 B").unwrap(); // 8 bytes

assert_eq!(8, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8").unwrap(); // 8 bytes

assert_eq!(8, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 b").unwrap(); // 8 bytes

assert_eq!(8, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 kb").unwrap(); // 8 kilobytes

assert_eq!(8000, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 kib").unwrap(); // 8 kibibytes

assert_eq!(8192, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 k").unwrap(); // 8 kilobytes

assert_eq!(8000, result.get_bytes());
source§

impl Byte

source

pub const fn get_bytes(&self) -> u128

Get bytes represented by a Byte object.

Examples
use byte_unit::Byte;

let byte = Byte::from_str("123KiB").unwrap();

let result = byte.get_bytes();

assert_eq!(125952, result);
use byte_unit::Byte;

let byte = Byte::from_str("50.84 MB").unwrap();

let result = byte.get_bytes();

assert_eq!(50840000, result);
source

pub fn get_adjusted_unit(&self, unit: ByteUnit) -> AdjustedByte

Adjust the unit and value for Byte object. Accuracy should be taken care of.

Examples
use byte_unit::{Byte, ByteUnit};

let byte = Byte::from_str("123KiB").unwrap();

let adjusted_byte = byte.get_adjusted_unit(ByteUnit::KB);

assert_eq!("125.95 KB", adjusted_byte.to_string());
use byte_unit::{Byte, ByteUnit};

let byte = Byte::from_str("50.84 MB").unwrap();

let adjusted_byte = byte.get_adjusted_unit(ByteUnit::MiB);

assert_eq!("48.48 MiB", adjusted_byte.to_string());
source

pub fn get_appropriate_unit(&self, binary_multiples: bool) -> AdjustedByte

Find the appropriate unit and value for Byte object. Accuracy should be taken care of.

Examples
use byte_unit::Byte;

let byte = Byte::from_str("123KiB").unwrap();

let adjusted_byte = byte.get_appropriate_unit(false);

assert_eq!("125.95 KB", adjusted_byte.to_string());
use byte_unit::Byte;

let byte = Byte::from_str("50.84 MB").unwrap();

let adjusted_byte = byte.get_appropriate_unit(true);

assert_eq!("48.48 MiB", adjusted_byte.to_string());

Trait Implementations§

source§

impl Clone for Byte

source§

fn clone(&self) -> Byte

Returns a copy 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 Debug for Byte

source§

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

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

impl Default for Byte

source§

fn default() -> Byte

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

impl<'de> Deserialize<'de> for Byte

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 Display for Byte

source§

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

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

impl From<AdjustedByte> for Byte

source§

fn from(other: AdjustedByte) -> Byte

Converts to this type from the input type.
source§

impl From<Byte> for u128

source§

fn from(b: Byte) -> u128

Converts to this type from the input type.
source§

impl From<u128> for Byte

source§

fn from(u: u128) -> Self

Converts to this type from the input type.
source§

impl From<u16> for Byte

source§

fn from(u: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for Byte

source§

fn from(u: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Byte

source§

fn from(u: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for Byte

source§

fn from(u: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for Byte

source§

fn from(u: usize) -> Self

Converts to this type from the input type.
source§

impl FromStr for Byte

§

type Err = ByteError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Byte

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 Ord for Byte

source§

fn cmp(&self, other: &Byte) -> Ordering

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<Byte> for Byte

source§

fn eq(&self, other: &Byte) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Byte> for Byte

source§

fn partial_cmp(&self, other: &Byte) -> 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

This method 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

This method 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

This method 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

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

impl Serialize for Byte

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 TryFrom<&str> for Byte

§

type Error = ByteError

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

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for Byte

source§

impl Eq for Byte

source§

impl StructuralEq for Byte

source§

impl StructuralPartialEq for Byte

Auto Trait Implementations§

§

impl RefUnwindSafe for Byte

§

impl Send for Byte

§

impl Sync for Byte

§

impl Unpin for Byte

§

impl UnwindSafe for Byte

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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