|
| 1 | +//! Types and constants for handling amounts of data (in octets, or bits). |
| 2 | +
|
| 3 | +use super::measurement::*; |
| 4 | + |
| 5 | +// Constants |
| 6 | +const OCTET_BIT_FACTOR: f64 = 0.125; |
| 7 | + |
| 8 | +// Constants, legacy |
| 9 | +const OCTET_KILOOCTET_FACTOR: f64 = 1000.0; |
| 10 | +const OCTET_MEGAOCTET_FACTOR: f64 = 1000.0 * 1000.0; |
| 11 | +const OCTET_GIGAOCTET_FACTOR: f64 = 1000.0 * 1000.0 * 1000.0; |
| 12 | +const OCTET_TERAOCTET_FACTOR: f64 = 1000.0 * 1000.0 * 1000.0 * 1000.0; |
| 13 | + |
| 14 | +// Constants, SI |
| 15 | +const OCTET_KIBIOCTET_FACTOR: f64 = 1024.0; |
| 16 | +const OCTET_MEBIOCTET_FACTOR: f64 = 1024.0 * 1024.0; |
| 17 | +const OCTET_GIBIOCTET_FACTOR: f64 = 1024.0 * 1024.0 * 1024.0; |
| 18 | +const OCTET_TEBIOCTET_FACTOR: f64 = 1024.0 * 1024.0 * 1024.0 * 1024.0; |
| 19 | + |
| 20 | +/// The `Data` struct can be used to deal with computer information in a common way. |
| 21 | +/// Common legacy and SI units are supported. |
| 22 | +/// |
| 23 | +/// # Example |
| 24 | +/// |
| 25 | +/// ``` |
| 26 | +/// use measurements::Data; |
| 27 | +/// |
| 28 | +/// let file_size = Data::from_mebioctets(2.5); |
| 29 | +/// let octets = file_size.as_octets(); |
| 30 | +/// println!("There are {} octets in that file.", octets); |
| 31 | +/// ``` |
| 32 | +#[derive(Copy, Clone, Debug)] |
| 33 | +pub struct Data { |
| 34 | + octets: f64, |
| 35 | +} |
| 36 | + |
| 37 | +impl Data { |
| 38 | + /// Create new Data from floating point value in Octets |
| 39 | + pub fn from_octets(octets: f64) -> Self { |
| 40 | + Data { octets: octets } |
| 41 | + } |
| 42 | + |
| 43 | + /// Create new Data from floating point value in Bits |
| 44 | + pub fn from_bits(bits: f64) -> Self { |
| 45 | + Self::from_octets(bits * OCTET_BIT_FACTOR) |
| 46 | + } |
| 47 | + |
| 48 | + // Inputs, legacy |
| 49 | + /// Create new Data from floating point value in Kilooctets (1000 octets) |
| 50 | + pub fn from_kilooctets(kilooctets: f64) -> Self { |
| 51 | + Self::from_octets(kilooctets * OCTET_KILOOCTET_FACTOR) |
| 52 | + } |
| 53 | + |
| 54 | + /// Create new Data from floating point value in Megaoctets (1e6 octets) |
| 55 | + pub fn from_megaoctets(megaoctets: f64) -> Self { |
| 56 | + Self::from_octets(megaoctets * OCTET_MEGAOCTET_FACTOR) |
| 57 | + } |
| 58 | + |
| 59 | + /// Create new Data from floating point value in Gigaoctets (1e9 octets) |
| 60 | + pub fn from_gigaoctets(gigaoctets: f64) -> Self { |
| 61 | + Self::from_octets(gigaoctets * OCTET_GIGAOCTET_FACTOR) |
| 62 | + } |
| 63 | + |
| 64 | + /// Create new Data from floating point value in Teraoctets (1e12 octets) |
| 65 | + pub fn from_teraoctets(teraoctets: f64) -> Self { |
| 66 | + Self::from_octets(teraoctets * OCTET_TERAOCTET_FACTOR) |
| 67 | + } |
| 68 | + |
| 69 | + /// Create new Data from floating point value in Kibioctets (1024 octets) |
| 70 | + pub fn from_kibioctets(kibioctets: f64) -> Self { |
| 71 | + Self::from_octets(kibioctets * OCTET_KIBIOCTET_FACTOR) |
| 72 | + } |
| 73 | + |
| 74 | + /// Create new Data from floating point value in Mebioctets (1024**2 octets) |
| 75 | + pub fn from_mebioctets(mebioctets: f64) -> Self { |
| 76 | + Self::from_octets(mebioctets * OCTET_MEBIOCTET_FACTOR) |
| 77 | + } |
| 78 | + |
| 79 | + /// Create new Data from floating point value in Gibioctets (1024**3 octets) |
| 80 | + pub fn from_gibioctets(gibioctets: f64) -> Self { |
| 81 | + Self::from_octets(gibioctets * OCTET_GIBIOCTET_FACTOR) |
| 82 | + } |
| 83 | + |
| 84 | + /// Create new Data from floating point value in Tebioctets (1024**4 octets) |
| 85 | + pub fn from_tebioctets(tebioctets: f64) -> Self { |
| 86 | + Self::from_octets(tebioctets * OCTET_TEBIOCTET_FACTOR) |
| 87 | + } |
| 88 | + |
| 89 | + /// Convert this Data to a floating point value in Octets |
| 90 | + pub fn as_octets(&self) -> f64 { |
| 91 | + self.octets |
| 92 | + } |
| 93 | + |
| 94 | + /// Convert this Data to a floating point value in Bits |
| 95 | + pub fn as_bits(&self) -> f64 { |
| 96 | + self.octets / OCTET_BIT_FACTOR |
| 97 | + } |
| 98 | + |
| 99 | + /// Convert this Data to a floating point value in Kilooctets (1000 octets) |
| 100 | + pub fn as_kilooctets(&self) -> f64 { |
| 101 | + self.octets / OCTET_KILOOCTET_FACTOR |
| 102 | + } |
| 103 | + |
| 104 | + /// Convert this Data to a floating point value in Megaoctets (1e6 octets) |
| 105 | + pub fn as_megaoctets(&self) -> f64 { |
| 106 | + self.octets / OCTET_MEGAOCTET_FACTOR |
| 107 | + } |
| 108 | + |
| 109 | + /// Convert this Data to a floating point value in Gigaoctets (1e9 octets) |
| 110 | + pub fn as_gigaoctets(&self) -> f64 { |
| 111 | + self.octets / OCTET_GIGAOCTET_FACTOR |
| 112 | + } |
| 113 | + |
| 114 | + /// Convert this Data to a floating point value in Teraoctets (1e12 octets) |
| 115 | + pub fn as_teraoctets(&self) -> f64 { |
| 116 | + self.octets / OCTET_TERAOCTET_FACTOR |
| 117 | + } |
| 118 | + |
| 119 | + /// Convert this Data to a floating point value in Kibioctets (1024 octets) |
| 120 | + pub fn as_kibioctets(&self) -> f64 { |
| 121 | + self.octets / OCTET_KIBIOCTET_FACTOR |
| 122 | + } |
| 123 | + |
| 124 | + /// Convert this Data to a floating point value in Mebioctets (1024**2 octets) |
| 125 | + pub fn as_mebioctets(&self) -> f64 { |
| 126 | + self.octets / OCTET_MEBIOCTET_FACTOR |
| 127 | + } |
| 128 | + |
| 129 | + /// Convert this Data to a floating point value in Gibioctets (1024**3 octets) |
| 130 | + pub fn as_gibioctets(&self) -> f64 { |
| 131 | + self.octets / OCTET_GIBIOCTET_FACTOR |
| 132 | + } |
| 133 | + |
| 134 | + /// Convert this Data to a floating point value in Tebioctets (1024**4 octets) |
| 135 | + pub fn as_tebioctets(&self) -> f64 { |
| 136 | + self.octets / OCTET_TEBIOCTET_FACTOR |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl Measurement for Data { |
| 141 | + fn as_base_units(&self) -> f64 { |
| 142 | + self.octets |
| 143 | + } |
| 144 | + |
| 145 | + fn from_base_units(units: f64) -> Self { |
| 146 | + Self::from_octets(units) |
| 147 | + } |
| 148 | + |
| 149 | + fn get_base_units_name(&self) -> &'static str { |
| 150 | + "octets" |
| 151 | + } |
| 152 | + |
| 153 | + fn get_appropriate_units(&self) -> (&'static str, f64) { |
| 154 | + // Smallest to largest |
| 155 | + let list = [ |
| 156 | + ("octets", 1.0), |
| 157 | + ("KiB", 1024.0), |
| 158 | + ("MiB", 1024.0_f64.powi(2)), |
| 159 | + ("GiB", 1024.0_f64.powi(3)), |
| 160 | + ("TiB", 1024.0_f64.powi(4)), |
| 161 | + ("PiB", 1024.0_f64.powi(5)), |
| 162 | + ("EiB", 1024.0_f64.powi(6)), |
| 163 | + ]; |
| 164 | + self.pick_appropriate_units(&list) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +implement_measurement! { Data } |
| 169 | + |
| 170 | +#[cfg(test)] |
| 171 | +mod test { |
| 172 | + use data::*; |
| 173 | + use test_utils::assert_almost_eq; |
| 174 | + |
| 175 | + // Metric |
| 176 | + #[test] |
| 177 | + fn bits() { |
| 178 | + let i1 = Data::from_octets(100.0); |
| 179 | + let r1 = i1.as_bits(); |
| 180 | + |
| 181 | + let i2 = Data::from_bits(100.0); |
| 182 | + let r2 = i2.as_octets(); |
| 183 | + |
| 184 | + assert_almost_eq(r1, 800.0); |
| 185 | + assert_almost_eq(r2, 12.5); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn kilooctet() { |
| 190 | + let i1 = Data::from_octets(100.0); |
| 191 | + let r1 = i1.as_kilooctets(); |
| 192 | + |
| 193 | + let i2 = Data::from_kilooctets(100.0); |
| 194 | + let r2 = i2.as_octets(); |
| 195 | + |
| 196 | + assert_almost_eq(r1, 0.1); |
| 197 | + assert_almost_eq(r2, 1e5); |
| 198 | + } |
| 199 | + |
| 200 | + #[test] |
| 201 | + fn megaoctet() { |
| 202 | + let i1 = Data::from_octets(100.0); |
| 203 | + let r1 = i1.as_megaoctets(); |
| 204 | + |
| 205 | + let i2 = Data::from_megaoctets(100.0); |
| 206 | + let r2 = i2.as_octets(); |
| 207 | + |
| 208 | + assert_almost_eq(r1, 0.0001); |
| 209 | + assert_almost_eq(r2, 1e8); |
| 210 | + } |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn gigaoctet() { |
| 214 | + let i1 = Data::from_octets(100.0); |
| 215 | + let r1 = i1.as_gigaoctets(); |
| 216 | + |
| 217 | + let i2 = Data::from_gigaoctets(100.0); |
| 218 | + let r2 = i2.as_octets(); |
| 219 | + |
| 220 | + assert_almost_eq(r1, 1e-7); |
| 221 | + assert_almost_eq(r2, 1e11); |
| 222 | + } |
| 223 | + |
| 224 | + #[test] |
| 225 | + fn teraoctet() { |
| 226 | + let i1 = Data::from_octets(100.0); |
| 227 | + let r1 = i1.as_teraoctets(); |
| 228 | + |
| 229 | + let i2 = Data::from_teraoctets(100.0); |
| 230 | + let r2 = i2.as_octets(); |
| 231 | + |
| 232 | + assert_almost_eq(r1, 1e-10); |
| 233 | + assert_almost_eq(r2, 1e14); |
| 234 | + } |
| 235 | + |
| 236 | + // Imperial |
| 237 | + #[test] |
| 238 | + fn kibioctet() { |
| 239 | + let i1 = Data::from_octets(100.0); |
| 240 | + let r1 = i1.as_kibioctets(); |
| 241 | + |
| 242 | + let i2 = Data::from_kibioctets(100.0); |
| 243 | + let r2 = i2.as_octets(); |
| 244 | + |
| 245 | + assert_almost_eq(r1, 0.09765625); |
| 246 | + assert_almost_eq(r2, 102400.0); |
| 247 | + } |
| 248 | + |
| 249 | + #[test] |
| 250 | + fn mebioctet() { |
| 251 | + let i1 = Data::from_octets(100.0); |
| 252 | + let r1 = i1.as_mebioctets(); |
| 253 | + |
| 254 | + let i2 = Data::from_mebioctets(100.0); |
| 255 | + let r2 = i2.as_octets(); |
| 256 | + |
| 257 | + assert_almost_eq(r1, 9.536743e-5); |
| 258 | + assert_almost_eq(r2, 104857600.0); |
| 259 | + } |
| 260 | + |
| 261 | + #[test] |
| 262 | + fn gibioctets() { |
| 263 | + let i1 = Data::from_octets(100.0); |
| 264 | + let r1 = i1.as_gibioctets(); |
| 265 | + |
| 266 | + let i2 = Data::from_gibioctets(100.0); |
| 267 | + let r2 = i2.as_octets(); |
| 268 | + |
| 269 | + assert_almost_eq(r1, 9.313226e-8); |
| 270 | + assert_almost_eq(r2, 107374182400.0); |
| 271 | + } |
| 272 | + |
| 273 | + #[test] |
| 274 | + fn tebioctets() { |
| 275 | + let i1 = Data::from_octets(100.0); |
| 276 | + let r1 = i1.as_tebioctets(); |
| 277 | + |
| 278 | + let i2 = Data::from_tebioctets(100.0); |
| 279 | + let r2 = i2.as_octets(); |
| 280 | + |
| 281 | + assert_almost_eq(r1, 9.094947e-11); |
| 282 | + assert_almost_eq(r2, 109951162777600.0); |
| 283 | + } |
| 284 | + |
| 285 | + // Traits |
| 286 | + #[test] |
| 287 | + fn add() { |
| 288 | + let a = Data::from_octets(2.0); |
| 289 | + let b = Data::from_octets(4.0); |
| 290 | + let c = a + b; |
| 291 | + assert_almost_eq(c.as_octets(), 6.0); |
| 292 | + } |
| 293 | + |
| 294 | + #[test] |
| 295 | + fn sub() { |
| 296 | + let a = Data::from_octets(2.0); |
| 297 | + let b = Data::from_octets(4.0); |
| 298 | + let c = a - b; |
| 299 | + assert_almost_eq(c.as_octets(), -2.0); |
| 300 | + } |
| 301 | + |
| 302 | + #[test] |
| 303 | + fn mul() { |
| 304 | + let b = Data::from_octets(4.0); |
| 305 | + let d = b * 2.0; |
| 306 | + assert_almost_eq(d.as_octets(), 8.0); |
| 307 | + } |
| 308 | + |
| 309 | + #[test] |
| 310 | + fn div() { |
| 311 | + let b = Data::from_octets(4.0); |
| 312 | + let d = b / 2.0; |
| 313 | + assert_almost_eq(d.as_octets(), 2.0); |
| 314 | + } |
| 315 | + |
| 316 | + #[test] |
| 317 | + fn eq() { |
| 318 | + let a = Data::from_octets(2.0); |
| 319 | + let b = Data::from_octets(2.0); |
| 320 | + assert_eq!(a == b, true); |
| 321 | + } |
| 322 | + |
| 323 | + #[test] |
| 324 | + fn neq() { |
| 325 | + let a = Data::from_octets(2.0); |
| 326 | + let b = Data::from_octets(4.0); |
| 327 | + assert_eq!(a == b, false); |
| 328 | + } |
| 329 | + |
| 330 | + #[test] |
| 331 | + fn cmp() { |
| 332 | + let a = Data::from_octets(2.0); |
| 333 | + let b = Data::from_octets(4.0); |
| 334 | + assert_eq!(a < b, true); |
| 335 | + assert_eq!(a <= b, true); |
| 336 | + assert_eq!(a > b, false); |
| 337 | + assert_eq!(a >= b, false); |
| 338 | + } |
| 339 | + |
| 340 | +} |
0 commit comments