@@ -73,120 +73,151 @@ const LN_KIB: f64 = 6.931_471_805_599_453;
7373/// `ln(1000) ~= 6.908`
7474const LN_KB : f64 = 6.907_755_278_982_137 ;
7575
76+ /// Formatting style.
7677#[ derive( Debug , Clone , Default ) ]
7778pub enum Format {
79+ /// IEC (binary) representation.
80+ ///
81+ /// E.g., "1.0 MiB"
7882 #[ default]
7983 IEC ,
84+
85+ /// SI (decimal) representation.
86+ ///
87+ /// E.g., "1.02 MB"
8088 SI ,
8189}
8290
83- pub fn kb < V : Into < u64 > > ( size : V ) -> u64 {
91+ /// Converts a quantity of kilobytes to bytes.
92+ pub fn kb ( size : impl Into < u64 > ) -> u64 {
8493 size. into ( ) * KB
8594}
8695
96+ /// Converts a quantity of kibibytes to bytes.
8797pub fn kib < V : Into < u64 > > ( size : V ) -> u64 {
8898 size. into ( ) * KIB
8999}
90100
101+ /// Converts a quantity of megabytes to bytes.
91102pub fn mb < V : Into < u64 > > ( size : V ) -> u64 {
92103 size. into ( ) * MB
93104}
94105
106+ /// Converts a quantity of mebibytes to bytes.
95107pub fn mib < V : Into < u64 > > ( size : V ) -> u64 {
96108 size. into ( ) * MIB
97109}
98110
111+ /// Converts a quantity of gigabytes to bytes.
99112pub fn gb < V : Into < u64 > > ( size : V ) -> u64 {
100113 size. into ( ) * GB
101114}
102115
116+ /// Converts a quantity of gibibytes to bytes.
103117pub fn gib < V : Into < u64 > > ( size : V ) -> u64 {
104118 size. into ( ) * GIB
105119}
106120
121+ /// Converts a quantity of terabytes to bytes.
107122pub fn tb < V : Into < u64 > > ( size : V ) -> u64 {
108123 size. into ( ) * TB
109124}
110125
126+ /// Converts a quantity of tebibytes to bytes.
111127pub fn tib < V : Into < u64 > > ( size : V ) -> u64 {
112128 size. into ( ) * TIB
113129}
114130
131+ /// Converts a quantity of petabytes to bytes.
115132pub fn pb < V : Into < u64 > > ( size : V ) -> u64 {
116133 size. into ( ) * PB
117134}
118135
136+ /// Converts a quantity of pebibytes to bytes.
119137pub fn pib < V : Into < u64 > > ( size : V ) -> u64 {
120138 size. into ( ) * PIB
121139}
122140
123- /// Byte size representation
141+ /// Byte size representation.
124142#[ derive( Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Default ) ]
125143#[ cfg_attr( feature = "arbitrary" , derive( arbitrary:: Arbitrary ) ) ]
126144pub struct ByteSize ( pub u64 ) ;
127145
128146impl ByteSize {
147+ /// Constructs a byte size wrapper from a quantity of bytes.
129148 #[ inline( always) ]
130149 pub const fn b ( size : u64 ) -> ByteSize {
131150 ByteSize ( size)
132151 }
133152
153+ /// Constructs a byte size wrapper from a quantity of kilobytes.
134154 #[ inline( always) ]
135155 pub const fn kb ( size : u64 ) -> ByteSize {
136156 ByteSize ( size * KB )
137157 }
138158
159+ /// Constructs a byte size wrapper from a quantity of kibibytes.
139160 #[ inline( always) ]
140161 pub const fn kib ( size : u64 ) -> ByteSize {
141162 ByteSize ( size * KIB )
142163 }
143164
165+ /// Constructs a byte size wrapper from a quantity of megabytes.
144166 #[ inline( always) ]
145167 pub const fn mb ( size : u64 ) -> ByteSize {
146168 ByteSize ( size * MB )
147169 }
148170
171+ /// Constructs a byte size wrapper from a quantity of mebibytes.
149172 #[ inline( always) ]
150173 pub const fn mib ( size : u64 ) -> ByteSize {
151174 ByteSize ( size * MIB )
152175 }
153176
177+ /// Constructs a byte size wrapper from a quantity of gigabytes.
154178 #[ inline( always) ]
155179 pub const fn gb ( size : u64 ) -> ByteSize {
156180 ByteSize ( size * GB )
157181 }
158182
183+ /// Constructs a byte size wrapper from a quantity of gibibytes.
159184 #[ inline( always) ]
160185 pub const fn gib ( size : u64 ) -> ByteSize {
161186 ByteSize ( size * GIB )
162187 }
163188
189+ /// Constructs a byte size wrapper from a quantity of terabytes.
164190 #[ inline( always) ]
165191 pub const fn tb ( size : u64 ) -> ByteSize {
166192 ByteSize ( size * TB )
167193 }
168194
195+ /// Constructs a byte size wrapper from a quantity of tebibytes.
169196 #[ inline( always) ]
170197 pub const fn tib ( size : u64 ) -> ByteSize {
171198 ByteSize ( size * TIB )
172199 }
173200
201+ /// Constructs a byte size wrapper from a quantity of petabytes.
174202 #[ inline( always) ]
175203 pub const fn pb ( size : u64 ) -> ByteSize {
176204 ByteSize ( size * PB )
177205 }
178206
207+ /// Constructs a byte size wrapper from a quantity of pebibytes.
179208 #[ inline( always) ]
180209 pub const fn pib ( size : u64 ) -> ByteSize {
181210 ByteSize ( size * PIB )
182211 }
183212
213+ /// Returns byte count.
184214 #[ inline( always) ]
185215 pub const fn as_u64 ( & self ) -> u64 {
186216 self . 0
187217 }
188218}
189219
220+ /// Constructs human-readable string representation of `bytes` with given `format` style.
190221pub fn to_string_format ( bytes : u64 , format : Format ) -> String {
191222 let unit = match format {
192223 Format :: IEC => KIB ,
0 commit comments