mod.rs
This file
Path
src/quantity/mod.rs
69 lines · 1.9 KiB · Rust
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake it
Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 1–2why, one step back1use std::marker::PhantomData;2Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
lines 3–5why, one step back3use serde::ser::SerializeStruct;4use serde::Serializer;5Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 6–11why, one step back6use uom::si;78pub use si::f64::{Information, Ratio, Time};9pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};10pub use si::ratio::ratio;11pub use si::time::{hour, microsecond, millisecond, minute, nanosecond, second};Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 12–24why, one step back12pub use uom::num_traits::Zero;1314pub mod statistics;1516pub const fn const_time_from_seconds(value: f64) -> Time {17 // Quantity::new in uom is not yet const: https://docs.rs/uom/0.36.0/uom/si/struct.Quantity.html18 Time {19 dimension: PhantomData,20 units: PhantomData,21 value,22 }23}24Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
lines 25–44why, one step back25pub fn serialize_time<S>(t: &Time, s: S) -> Result<S::Ok, S::Error>26where27 S: Serializer,28{29 let mut state = s.serialize_struct("Time", 2)?;30 state.serialize_field("value", &t.get::<second>())?;31 state.serialize_field("unit", "second")?;32 state.end()33}3435pub fn serialize_information<S>(i: &Information, s: S) -> Result<S::Ok, S::Error>36where37 S: Serializer,38{39 let mut state = s.serialize_struct("Information", 2)?;40 state.serialize_field("value", &i.get::<byte>())?;41 state.serialize_field("unit", "byte")?;42 state.end()43}44Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 45–69why, one step back45#[test]46fn test_time() {47 let time = Time::new::<millisecond>(123.4);48 assert_eq!(time.get::<millisecond>(), 123.4);4950 let time_s = time.get::<second>();51 approx::assert_relative_eq!(time_s, 0.1234);5253 let time_us = time.get::<microsecond>();54 approx::assert_relative_eq!(time_us, 123400.0);55}5657#[test]58fn test_information() {59 use uom::si::information::pebibyte;6061 let information = Information::new::<kibibyte>(8.);62 assert_eq!(information.get::<byte>(), 8192.);6364 let information_kib = information.get::<kibibyte>();65 assert_eq!(information_kib, 8.);6667 let largest_exactly_representable = Information::new::<byte>(9_007_199_254_740_992.);68 assert_eq!(largest_exactly_representable.get::<pebibyte>(), 8.);69}