Skip to content

Commit b4c6916

Browse files
robjtedeAndrew15-5
andauthored
docs: document all public items (#73)
* fix: swap values of `LN_KIB` & `LN_KB` * fix: invert SI/IEC conditions - Conditions that check whether the SI or IEC units must be used were inverted, i.e., when `si_prefix == true` it would use `"iB"` instead of `"B"`. - KB & kiB were used instead of kB & KiB. - Switches (true/false) in tests are also fixed. * fix: format with IEC (binary) by default * docs: doc units_* statics * refactor: move statics to consts * refactor: introduce format enum * test: use to_string_format in tests * chore: expand debug impl * fix: increase precision of LN_ constants * chore: remove to_string_as method * docs: document all public items * chore: remove useless B constant --------- Co-authored-by: Andrew Voynov <[email protected]>
1 parent b8242ef commit b4c6916

File tree

4 files changed

+60
-36
lines changed

4 files changed

+60
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
- Reject parsing non-unit characters after whitespace.
1212
- Remove `ByteSize::to_string_as()` method.
1313
- Remove top-level `to_string()` method.
14+
- Remove top-level `B` constant.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ quickcheck = "1"
2828
serde = { version = "1", features = ["derive"] }
2929
serde_json = "1"
3030
toml = "0.8"
31+
32+
[lints.rust]
33+
missing-docs = "warn"

src/lib.rs

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,26 @@ mod serde;
4848
use std::fmt::{self, Debug, Display, Formatter};
4949
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
5050

51-
/// byte size for 1 byte
52-
pub const B: u64 = 1;
53-
/// bytes size for 1 kilobyte
51+
/// Number of bytes in 1 kilobyte.
5452
pub const KB: u64 = 1_000;
55-
/// bytes size for 1 megabyte
53+
/// Number of bytes in 1 megabyte.
5654
pub const MB: u64 = 1_000_000;
57-
/// bytes size for 1 gigabyte
55+
/// Number of bytes in 1 gigabyte.
5856
pub const GB: u64 = 1_000_000_000;
59-
/// bytes size for 1 terabyte
57+
/// Number of bytes in 1 terabyte.
6058
pub const TB: u64 = 1_000_000_000_000;
61-
/// bytes size for 1 petabyte
59+
/// Number of bytes in 1 petabyte.
6260
pub const PB: u64 = 1_000_000_000_000_000;
6361

64-
/// bytes size for 1 kibibyte
62+
/// Number of bytes in 1 kibibyte.
6563
pub const KIB: u64 = 1_024;
66-
/// bytes size for 1 mebibyte
64+
/// Number of bytes in 1 mebibyte.
6765
pub const MIB: u64 = 1_048_576;
68-
/// bytes size for 1 gibibyte
66+
/// Number of bytes in 1 gibibyte.
6967
pub const GIB: u64 = 1_073_741_824;
70-
/// bytes size for 1 tebibyte
68+
/// Number of bytes in 1 tebibyte.
7169
pub const TIB: u64 = 1_099_511_627_776;
72-
/// bytes size for 1 pebibyte
70+
/// Number of bytes in 1 pebibyte.
7371
pub const PIB: u64 = 1_125_899_906_842_624;
7472

7573
/// IEC (binary) units.
@@ -88,119 +86,150 @@ const LN_KIB: f64 = 6.931_471_805_599_453;
8886
/// `ln(1000) ~= 6.908`
8987
const LN_KB: f64 = 6.907_755_278_982_137;
9088

89+
/// Formatting style.
9190
#[derive(Debug, Clone, Default)]
9291
pub enum Format {
92+
/// IEC (binary) representation.
93+
///
94+
/// E.g., "1.0 MiB"
9395
#[default]
9496
IEC,
97+
98+
/// SI (decimal) representation.
99+
///
100+
/// E.g., "1.02 MB"
95101
SI,
96102
}
97103

98-
pub fn kb<V: Into<u64>>(size: V) -> u64 {
104+
/// Converts a quantity of kilobytes to bytes.
105+
pub fn kb(size: impl Into<u64>) -> u64 {
99106
size.into() * KB
100107
}
101108

109+
/// Converts a quantity of kibibytes to bytes.
102110
pub fn kib<V: Into<u64>>(size: V) -> u64 {
103111
size.into() * KIB
104112
}
105113

114+
/// Converts a quantity of megabytes to bytes.
106115
pub fn mb<V: Into<u64>>(size: V) -> u64 {
107116
size.into() * MB
108117
}
109118

119+
/// Converts a quantity of mebibytes to bytes.
110120
pub fn mib<V: Into<u64>>(size: V) -> u64 {
111121
size.into() * MIB
112122
}
113123

124+
/// Converts a quantity of gigabytes to bytes.
114125
pub fn gb<V: Into<u64>>(size: V) -> u64 {
115126
size.into() * GB
116127
}
117128

129+
/// Converts a quantity of gibibytes to bytes.
118130
pub fn gib<V: Into<u64>>(size: V) -> u64 {
119131
size.into() * GIB
120132
}
121133

134+
/// Converts a quantity of terabytes to bytes.
122135
pub fn tb<V: Into<u64>>(size: V) -> u64 {
123136
size.into() * TB
124137
}
125138

139+
/// Converts a quantity of tebibytes to bytes.
126140
pub fn tib<V: Into<u64>>(size: V) -> u64 {
127141
size.into() * TIB
128142
}
129143

144+
/// Converts a quantity of petabytes to bytes.
130145
pub fn pb<V: Into<u64>>(size: V) -> u64 {
131146
size.into() * PB
132147
}
133148

149+
/// Converts a quantity of pebibytes to bytes.
134150
pub fn pib<V: Into<u64>>(size: V) -> u64 {
135151
size.into() * PIB
136152
}
137153

138-
/// Byte size representation
154+
/// Byte size representation.
139155
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
140156
pub struct ByteSize(pub u64);
141157

142158
impl ByteSize {
159+
/// Constructs a byte size wrapper from a quantity of bytes.
143160
#[inline(always)]
144161
pub const fn b(size: u64) -> ByteSize {
145162
ByteSize(size)
146163
}
147164

165+
/// Constructs a byte size wrapper from a quantity of kilobytes.
148166
#[inline(always)]
149167
pub const fn kb(size: u64) -> ByteSize {
150168
ByteSize(size * KB)
151169
}
152170

171+
/// Constructs a byte size wrapper from a quantity of kibibytes.
153172
#[inline(always)]
154173
pub const fn kib(size: u64) -> ByteSize {
155174
ByteSize(size * KIB)
156175
}
157176

177+
/// Constructs a byte size wrapper from a quantity of megabytes.
158178
#[inline(always)]
159179
pub const fn mb(size: u64) -> ByteSize {
160180
ByteSize(size * MB)
161181
}
162182

183+
/// Constructs a byte size wrapper from a quantity of mebibytes.
163184
#[inline(always)]
164185
pub const fn mib(size: u64) -> ByteSize {
165186
ByteSize(size * MIB)
166187
}
167188

189+
/// Constructs a byte size wrapper from a quantity of gigabytes.
168190
#[inline(always)]
169191
pub const fn gb(size: u64) -> ByteSize {
170192
ByteSize(size * GB)
171193
}
172194

195+
/// Constructs a byte size wrapper from a quantity of gibibytes.
173196
#[inline(always)]
174197
pub const fn gib(size: u64) -> ByteSize {
175198
ByteSize(size * GIB)
176199
}
177200

201+
/// Constructs a byte size wrapper from a quantity of terabytes.
178202
#[inline(always)]
179203
pub const fn tb(size: u64) -> ByteSize {
180204
ByteSize(size * TB)
181205
}
182206

207+
/// Constructs a byte size wrapper from a quantity of tebibytes.
183208
#[inline(always)]
184209
pub const fn tib(size: u64) -> ByteSize {
185210
ByteSize(size * TIB)
186211
}
187212

213+
/// Constructs a byte size wrapper from a quantity of petabytes.
188214
#[inline(always)]
189215
pub const fn pb(size: u64) -> ByteSize {
190216
ByteSize(size * PB)
191217
}
192218

219+
/// Constructs a byte size wrapper from a quantity of pebibytes.
193220
#[inline(always)]
194221
pub const fn pib(size: u64) -> ByteSize {
195222
ByteSize(size * PIB)
196223
}
197224

225+
/// Returns byte count.
198226
#[inline(always)]
199227
pub const fn as_u64(&self) -> u64 {
200228
self.0
201229
}
202230
}
203231

232+
/// Constructs human-readable string representation of `bytes` with given `format` style.
204233
pub fn to_string_format(bytes: u64, format: Format) -> String {
205234
let unit = match format {
206235
Format::IEC => KIB,
@@ -430,21 +459,12 @@ mod tests {
430459
let mut x = ByteSize::mb(1);
431460

432461
assert_eq!((x + MB as u64).as_u64(), 2_000_000);
433-
434462
assert_eq!((x + MB as u32).as_u64(), 2_000_000);
435-
436463
assert_eq!((x + KB as u16).as_u64(), 1_001_000);
437-
438-
assert_eq!((x + B as u8).as_u64(), 1_000_001);
439-
440464
assert_eq!((x - MB as u64).as_u64(), 0);
441-
442465
assert_eq!((x - MB as u32).as_u64(), 0);
443-
444466
assert_eq!((x - KB as u32).as_u64(), 999_000);
445467

446-
assert_eq!((x - B as u32).as_u64(), 999_999);
447-
448468
x += MB as u64;
449469
x += MB as u32;
450470
x += 10u16;

src/parse.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ enum Unit {
7070
impl Unit {
7171
fn factor(&self) -> u64 {
7272
match self {
73-
Self::Byte => super::B,
74-
// power of tens
75-
Self::KiloByte => super::KB,
76-
Self::MegaByte => super::MB,
77-
Self::GigaByte => super::GB,
78-
Self::TeraByte => super::TB,
79-
Self::PetaByte => super::PB,
80-
// power of twos
81-
Self::KibiByte => super::KIB,
82-
Self::MebiByte => super::MIB,
83-
Self::GibiByte => super::GIB,
84-
Self::TebiByte => super::TIB,
85-
Self::PebiByte => super::PIB,
73+
Self::Byte => 1,
74+
// decimal units
75+
Self::KiloByte => crate::KB,
76+
Self::MegaByte => crate::MB,
77+
Self::GigaByte => crate::GB,
78+
Self::TeraByte => crate::TB,
79+
Self::PetaByte => crate::PB,
80+
// binary units
81+
Self::KibiByte => crate::KIB,
82+
Self::MebiByte => crate::MIB,
83+
Self::GibiByte => crate::GIB,
84+
Self::TebiByte => crate::TIB,
85+
Self::PebiByte => crate::PIB,
8686
}
8787
}
8888
}

0 commit comments

Comments
 (0)