Skip to content

Added value unit/prefix parsing and conversion#1300

Merged
stevenengler merged 2 commits intoshadow:devfrom
stevenengler:unit-parsing
Apr 25, 2021
Merged

Added value unit/prefix parsing and conversion#1300
stevenengler merged 2 commits intoshadow:devfrom
stevenengler:unit-parsing

Conversation

@stevenengler
Copy link
Copy Markdown
Contributor

This code is currently unused, but will be used in the future by the command line / configuration parsing. Part of #1161.

@stevenengler stevenengler added Type: Enhancement New functionality or improved design Component: Main Composing the core Shadow executable labels Apr 22, 2021
@stevenengler stevenengler self-assigned this Apr 22, 2021
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 22, 2021

Codecov Report

Merging #1300 (4930d53) into dev (ce4af27) will decrease coverage by 0.43%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev    #1300      +/-   ##
==========================================
- Coverage   55.70%   55.26%   -0.44%     
==========================================
  Files         142      142              
  Lines       20558    20721     +163     
  Branches     5049     5060      +11     
==========================================
+ Hits        11451    11452       +1     
- Misses       5993     6155     +162     
  Partials     3114     3114              
Flag Coverage Δ
tests 55.26% <ø> (-0.44%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/support/logger/rust_bindings/src/lib.rs 28.00% <0.00%> (-6.90%) ⬇️
src/test/futex/test_futex.c 67.74% <0.00%> (+0.64%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ce4af27...4930d53. Read the comment docs.

@stevenengler stevenengler force-pushed the unit-parsing branch 3 times, most recently from 6adf4b5 to d480215 Compare April 22, 2021 18:22
@stevenengler stevenengler requested review from sporksmith and removed request for sporksmith April 22, 2021 18:27
@stevenengler
Copy link
Copy Markdown
Contributor Author

stevenengler commented Apr 22, 2021

I update the code to take more options (for example "ms" can be specified as "milliseconds"), and bandwidth units have been changed (for example "Mbps" has been changed to "Mbit"). I think this now covers most of the same format as TGen (time, bandwidth), but unlike TGen, is case-sensitive.

Copy link
Copy Markdown
Contributor

@sporksmith sporksmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"n" | "nano" => Ok(Self::Nano),
"u" | "micro" => Ok(Self::Micro),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be kind of neat to also accept, and maybe display, μ. Maybe not worth bringing in unicode where we've previously only dealt with ascii though; up to you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added (for both parsing and display).

}

impl Prefixes for SiPrefixes {
fn relative_magnitude(&self) -> u128 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a little more readable as:

let base = 1_000_000_000;
match self {
    Self::Nano => base / 1_000_000_000,
    Self::Micro => base / 1_000_000,
    ...
    Self::Base => base,
    ...
   Self::Tera => base * 1_000_000_000_000_000,
   Self::Tibi => base * 1 << 40,
}

Same in the other tables below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this base, but also am using pow() now to hopefully make it more readable.


/// Parses a string as bytes-per-second.
#[no_mangle]
pub extern "C" fn parse_bandwidth(s: *const libc::c_char, out: *mut u64) -> bool {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - maybe return an i64 and use -1 to signify errors? In particular this would help prevent a caller forgetting to check the RV going unnoticed (in the case that out already contained a plausible value).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum TimePrefixes {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is a little odd, since seconds, minutes, and hours aren't though of as prefixes. I was going to suggest maybe calling it TimeUnit, but maybe that'd be more confusing below. Maybe worth adding a comment about your reasoning either way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, originally I wasn't going to include minutes and hours since they're different units, but then figured I'd add them even though they don't really fit, which meant adding the suffix to the prefixes. I have an implementation that splits it up into "prefixes", "units", and "values" (values have units, and units may or may not have prefixes), but I feel that it might be too confusing. For now I'll just add comments.

use serde::de::{Deserialize, Deserializer, Visitor};
use serde::ser::{Serialize, Serializer};

pub trait Prefixes: Clone + Copy + Default + PartialEq + FromStr + Display + Debug {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit throughout - it's more conventional to name a trait, type, or enum after a member of that type. e.g. Prefix, SiPrefix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

use serde::de::{Deserialize, Deserializer, Visitor};
use serde::ser::{Serialize, Serializer};

pub trait Prefixes: Clone + Copy + Default + PartialEq + FromStr + Display + Debug {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout - consider adding some doc comments to the pub elements.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more comments.

@stevenengler stevenengler force-pushed the unit-parsing branch 3 times, most recently from 999e6ee to 956eaff Compare April 24, 2021 23:31
@stevenengler stevenengler merged commit 913955b into shadow:dev Apr 25, 2021
@stevenengler stevenengler deleted the unit-parsing branch April 25, 2021 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: Main Composing the core Shadow executable Type: Enhancement New functionality or improved design

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants