Skip to content

Commit

Permalink
fix examples in doc
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Nov 25, 2023
1 parent 3781c4b commit 12e5a12
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,31 @@ The struct `Byte` 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.
```rust
# #[cfg(feature = "byte")]
# {
use byte_unit::{Byte, Unit};
assert_eq!(15000, Byte::from_u64(15000).as_u64());
assert_eq!(15000, Byte::from_u64_with_unit(15, Unit::KB).unwrap().as_u64());
# }
```
You can also parse a string to create a `Byte` instance.
```rust
# #[cfg(feature = "byte")]
# {
use byte_unit::Byte;
assert_eq!(50840000, Byte::parse_str("50.84 MB", true).unwrap().as_u64());
# }
```
A `Byte` instance can be formatted to string precisely. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `Byte`.
```rust
# #[cfg(feature = "byte")]
# {
use byte_unit::Byte;
let byte = Byte::from_u64(15500);
Expand All @@ -62,13 +70,16 @@ assert_eq!("15500", byte.to_string());
assert_eq!("15.5 KB", format!("{byte:#}"));
assert_eq!("15500 B", format!("{byte:#.0}"));
# }
```
#### Arithmetic
There are `add`, `subtract`, `multiply`, and `divide` methods.
```rust
# #[cfg(feature = "byte")]
# {
use byte_unit::Byte;
let a = Byte::from_u64(15500);
Expand All @@ -79,20 +90,24 @@ assert_eq!(15000, a.subtract(b).unwrap().as_u64());
assert_eq!(31000, a.multiply(2).unwrap().as_u64());
assert_eq!(3100, a.divide(5).unwrap().as_u64());
# }
```
#### Find out an appropriate Unit
The `get_exact_unit` and `get_recoverable_unit` methods is useful if you want to find out a unit that is appropriate for a `Byte` instance.
```rust
# #[cfg(feature = "byte")]
# {
use byte_unit::{Byte, Unit};
let byte = Byte::from_u64(50840000);
assert_eq!((50840, Unit::KB), byte.get_exact_unit(false));
assert_eq!((50.84f64.try_into().unwrap(), Unit::MB), byte.get_recoverable_unit(false, 2));
assert_eq!((50840.into(), Unit::KB), byte.get_recoverable_unit(false, 0));
# }
```
#### AdaptedByte
Expand All @@ -104,6 +119,8 @@ To change the unit of a `Byte` instance, you can use the `get_adjusted_unit` met
An `AdaptedByte` instance can be formatted to string. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `AdaptedByte`.
```rust
# #[cfg(feature = "byte")]
# {
use byte_unit::{Byte, Unit};
let byte = Byte::parse_str("123KiB", true).unwrap();
Expand All @@ -112,18 +129,22 @@ let adjusted_byte = byte.get_adjusted_unit(Unit::KB);
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.
```rust
# #[cfg(feature = "byte")]
# {
use byte_unit::{Byte, Unit, UnitType};
let byte = Byte::from_u64(1500000);
let adjusted_byte = byte.get_appropriate_unit(UnitType::Binary);
assert_eq!("1.43 MiB", format!("{adjusted_byte:.2}"));
# }
```
### Bit
Expand Down

0 comments on commit 12e5a12

Please sign in to comment.