-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor the whole project, add new feature:
std
, supported units u…
…p to ZiB, and the data type used for storing bytes can be changed to u64 optionally now
- Loading branch information
Showing
13 changed files
with
1,435 additions
and
882 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "byte-unit" | ||
version = "3.0.3" | ||
version = "3.1.0" | ||
authors = ["Magic Len <[email protected]>"] | ||
edition = "2018" | ||
repository = "https://github.com/magiclen/byte-unit" | ||
|
@@ -13,4 +13,9 @@ license = "MIT" | |
|
||
[badges.travis-ci] | ||
repository = "magiclen/Byte-Unit" | ||
branch = "master" | ||
branch = "master" | ||
|
||
[features] | ||
std = [] | ||
u128 = [] | ||
default = ["u128"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
use core::cmp::Ordering; | ||
|
||
use alloc::fmt::{self, Display, Formatter}; | ||
use alloc::string::String; | ||
|
||
use crate::{get_bytes, Byte, ByteUnit}; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
/// Generated from the `get_appropriate_unit` and `get_adjusted_unit` methods of a `Byte` object. | ||
pub struct AdjustedByte { | ||
pub(crate) value: f64, | ||
pub(crate) unit: ByteUnit, | ||
} | ||
|
||
impl AdjustedByte { | ||
/// Format the `AdjustedByte` object to string. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// extern crate byte_unit; | ||
/// | ||
/// use byte_unit::{Byte, ByteUnit}; | ||
/// | ||
/// let byte = Byte::from_unit(1555f64, ByteUnit::KB).unwrap(); | ||
/// | ||
/// let result = byte.get_appropriate_unit(false).format(3); | ||
/// | ||
/// assert_eq!("1.555 MB", result); | ||
/// ``` | ||
#[inline] | ||
pub fn format(&self, fractional_digits: usize) -> String { | ||
format!("{:.*} {}", fractional_digits, self.value, self.unit) | ||
} | ||
|
||
#[inline] | ||
pub fn get_value(&self) -> f64 { | ||
self.value | ||
} | ||
|
||
#[inline] | ||
pub fn get_unit(&self) -> ByteUnit { | ||
self.unit | ||
} | ||
|
||
/// Create a new `Byte` object from this `AdjustedByte` object. **Accuracy** should be taken care of. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ``` | ||
/// extern crate byte_unit; | ||
/// | ||
/// use byte_unit::{Byte, ByteUnit}; | ||
/// | ||
/// let byte = Byte::from_str("123456789123456").unwrap(); | ||
/// let adjusted_byte = byte.get_adjusted_unit(ByteUnit::GB); | ||
/// | ||
/// assert_eq!(123456.789123456, adjusted_byte.get_value()); | ||
/// | ||
/// let byte = adjusted_byte.get_byte(); | ||
/// let adjusted_byte = byte.get_adjusted_unit(ByteUnit::GB); | ||
/// | ||
/// assert_eq!(123456.789123, adjusted_byte.get_value()); | ||
/// ``` | ||
#[inline] | ||
pub fn get_byte(&self) -> Byte { | ||
let bytes = get_bytes(self.value, self.unit); | ||
|
||
Byte::from_bytes(bytes) | ||
} | ||
} | ||
|
||
impl Display for AdjustedByte { | ||
#[inline] | ||
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { | ||
f.write_fmt(format_args!("{:.2} ", self.value))?; | ||
Display::fmt(&self.unit, f) | ||
} | ||
} | ||
|
||
impl PartialEq for AdjustedByte { | ||
/// Deal with the logical numeric equivalent. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// extern crate byte_unit; | ||
/// | ||
/// use byte_unit::{Byte, ByteUnit}; | ||
/// | ||
/// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); | ||
/// let byte2 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); | ||
/// | ||
/// assert_eq!(byte1.get_appropriate_unit(false), byte2.get_appropriate_unit(true)); | ||
/// ``` | ||
/// | ||
/// ``` | ||
/// extern crate byte_unit; | ||
/// | ||
/// use byte_unit::{Byte, ByteUnit}; | ||
/// | ||
/// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); | ||
/// let byte2 = Byte::from_unit(1f64, ByteUnit::MiB).unwrap(); | ||
/// | ||
/// assert_eq!(byte1.get_appropriate_unit(true), byte2.get_appropriate_unit(false)); | ||
/// ``` | ||
#[inline] | ||
fn eq(&self, other: &AdjustedByte) -> bool { | ||
let s = self.get_byte(); | ||
let o = other.get_byte(); | ||
|
||
s.eq(&o) | ||
} | ||
} | ||
|
||
impl Eq for AdjustedByte {} | ||
|
||
impl PartialOrd for AdjustedByte { | ||
#[inline] | ||
fn partial_cmp(&self, other: &AdjustedByte) -> Option<Ordering> { | ||
let s = self.get_byte(); | ||
let o = other.get_byte(); | ||
|
||
s.partial_cmp(&o) | ||
} | ||
} | ||
|
||
impl Ord for AdjustedByte { | ||
/// Deal with the logical numeric comparation. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// extern crate byte_unit; | ||
/// | ||
/// use byte_unit::{Byte, ByteUnit}; | ||
/// | ||
/// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); | ||
/// let byte2 = Byte::from_unit(1025f64, ByteUnit::KiB).unwrap(); | ||
/// | ||
/// assert!(byte1.get_appropriate_unit(false) < byte2.get_appropriate_unit(true)); | ||
/// ``` | ||
/// | ||
/// ``` | ||
/// extern crate byte_unit; | ||
/// | ||
/// use byte_unit::{Byte, ByteUnit}; | ||
/// | ||
/// let byte1 = Byte::from_unit(1024f64, ByteUnit::KiB).unwrap(); | ||
/// let byte2 = Byte::from_unit(1.01f64, ByteUnit::MiB).unwrap(); | ||
/// | ||
/// assert!(byte1.get_appropriate_unit(true) < byte2.get_appropriate_unit(false)); | ||
/// ``` | ||
#[inline] | ||
fn cmp(&self, other: &AdjustedByte) -> Ordering { | ||
let s = self.get_byte(); | ||
let o = other.get_byte(); | ||
|
||
s.cmp(&o) | ||
} | ||
} |
Oops, something went wrong.