Skip to content

Commit

Permalink
implement bit
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Nov 25, 2023
1 parent f8a7e26 commit 59fd9cd
Show file tree
Hide file tree
Showing 15 changed files with 2,126 additions and 34 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ assert_eq!(Unit::Kbit, Unit::parse_str("Kb", false, true).unwrap());

### Byte

The struct `Byte` can be used for representing a size in bytes.
The `Byte` struct can be used for representing a size in bytes.

The `from_*` associated functions can be used to create a `Byte` instance from different data types. The `as_*` methods can retrieve the size as a primitive type.

Expand Down Expand Up @@ -97,13 +97,13 @@ assert_eq!((50.84f64.try_into().unwrap(), Unit::MB), byte.get_recoverable_unit(f
assert_eq!((50840.into(), Unit::KB), byte.get_recoverable_unit(false, 0));
```

#### AdaptedByte
#### AdjustedByte

The struct `AdaptedByte` can be used for roughly representing a size of bytes with a unit.
The `AdjustedByte` struct can be used for roughly representing a size of bytes with a unit.

To change the unit of a `Byte` instance, you can use the `get_adjusted_unit` method.

An `AdaptedByte` instance can be formatted to string. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `AdaptedByte`.
An `AdjustedByte` instance can be formatted to string. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `AdjustedByte`.

```rust
use byte_unit::{Byte, Unit};
Expand All @@ -116,7 +116,7 @@ assert_eq!("125.952 KB", adjusted_byte.to_string());
assert_eq!("125.95 KB", format!("{adjusted_byte:.2}"));
```

The `get_appropriate_unit` method can be used to automatically find a appropriate unit for creating an `AdaptedByte` instance.
The `get_appropriate_unit` method can be used to automatically find an appropriate unit for creating an `AdjustedByte` instance.

```rust
use byte_unit::{Byte, Unit, UnitType};
Expand All @@ -130,11 +130,22 @@ assert_eq!("1.43 MiB", format!("{adjusted_byte:.2}"));

### Bit

The struct `Bit` can be used for representing a size in bits.
The `Bit` struct can be used for representing a size in bits.

The `bit` feature must be enabled.

TBD
Usage of the `Bit` struct and the `Byte` struct is very similar. Also, There is the `AdjustedBit` struct. The difference lies in the fact that the `parse_str` method of the `Bit` struct cannot be configured to ignore case; it always does not ignore case.

```rust
use byte_unit::{Bit, Unit};

let byte = Bit::parse_str("123Kib").unwrap();

let adjusted_byte = byte.get_adjusted_unit(Unit::Kbit);

assert_eq!("125.952 Kb", adjusted_byte.to_string());
assert_eq!("125.95 Kb", format!("{adjusted_byte:.2}"));
```

## No Std

Expand Down
43 changes: 43 additions & 0 deletions src/bit/adjusted/built_in_traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use core::str::FromStr;

use super::{AdjustedBit, Bit, Unit, UnitType};
use crate::ParseError;

impl From<Bit> for AdjustedBit {
/// `multiple_option` is set to `UnitType::Both`. See [`Bit::get_appropriate_unit`](./struct.Bit.html#method.get_appropriate_unit).
#[inline]
fn from(value: Bit) -> Self {
value.get_appropriate_unit(UnitType::Both)
}
}

impl From<AdjustedBit> for f64 {
#[inline]
fn from(value: AdjustedBit) -> Self {
value.get_value()
}
}

impl From<AdjustedBit> for Unit {
#[inline]
fn from(value: AdjustedBit) -> Self {
value.get_unit()
}
}

impl From<AdjustedBit> for Bit {
#[inline]
fn from(value: AdjustedBit) -> Self {
value.get_bit()
}
}

impl FromStr for AdjustedBit {
type Err = ParseError;

/// * `multiple_option` is set to `UnitType::Both`. See [`Bit::get_appropriate_unit`](./struct.Bit.html#method.get_appropriate_unit).
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Bit::parse_str(s)?.get_appropriate_unit(UnitType::Both))
}
}
Loading

0 comments on commit 59fd9cd

Please sign in to comment.