Giter VIP home page Giter VIP logo

bytesize's People

Contributors

acheronfail avatar chantsune avatar hadrieng2 avatar hyunsik avatar ileixe avatar joeroback avatar johnsonw avatar jrimbault avatar matklad avatar pmnoxx avatar tismith avatar tshepang avatar valpackett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

bytesize's Issues

Feature Request: Bits

I'm using this in a network monitor context, and it's very nice to have automatic formatting into *bytes/sec - which is commonly used in things like download speeds in browsers, etc.

However, it's also very common to measure internet bandwidth in bits, i.e., a "100 megabit connection" or "100 Mbps".

I think it would be really neat if this crate could support a Bit type too, so it could format in bits as well as bytes.

Do you need help to maintain this project?

Hello,

Do you need help to maintain this crate? I noticed little activity regarding reviews, merged PRs, and releases over the last few months. I'm happy to give you a hand.

sort -h friendly output mode

As someone who writes command line apps and likes pipes, I would like to have a sort -h friendly output, as in:

$ my-rust-app
5.5G    foo
2.6G    bar
16G     baz

$ my-rust-app | sort -hk1
2.6G    bar
5.5G    foo
16G     baz

This should work with both SI and IEC.

Serde support in human readable?

Would be nice if the serde support could have human readable bytesize support.

So that I can specify:

foo = "1 MiB"

And get a

#[derive(Deserialize)]
struct T {
    foo: ByteSize,
}

Support `no_std` usage

I'd like to use this with a no_std crate I'm working on, but this crate doesn't seem to support that usage. Would it be possible to add a std feature that's on-by-default that supports all the current functionality and otherwise supporting just stuff allowed by core?

Example in documentation is out of date

#11 removed the implementation of Sub for ByteSize as well as the as_usize method. The example in the documentation tries to use both:

//! let minus = ByteSize::tb(100) - ByteSize::gb(4);

Trying to run this example results in:

error[E0599]: no method named `as_usize` found for type `bytesize::ByteSize` in the current scope
 --> src/main.rs:8:27
  |
8 |   print!("{} bytes", plus.as_usize());
  |                           ^^^^^^^^

error[E0369]: binary operation `-` cannot be applied to type `bytesize::ByteSize`
  --> src/main.rs:10:15
   |
10 |   let minus = ByteSize::tb(100) - ByteSize::gb(4);
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: an implementation of `std::ops::Sub` might be missing for `bytesize::ByteSize`

LN_KIB / LN_KB are backwards

bytesize/src/lib.rs

Lines 63 to 64 in 0282291

static LN_KB: f64 = 6.931471806; // ln 1024
static LN_KIB: f64 = 6.907755279; // ln 1000

The two base values are backwards. This leads to the selection of the wrong unit; values larger than 1000 or lower than 1.0 can be displayed, e.g. "1090.0 GB" or "0.9 TiB".

use bytesize::ByteSize;

fn main() {
    println!("{}", ByteSize::gib(940).to_string_as(true));
    println!("{}", ByteSize::gb(940).to_string_as(false));
    println!("{}", ByteSize::gib(1090).to_string_as(true));
    println!("{}", ByteSize::gb(1090).to_string_as(false));
}

// Output:             Expected output:
// 0.9 TiB             940.0 GiB
// 940.0 GB            940.0 GB
// 1.1 TiB             1.1 TiB
// 1090.0 GB           1.1 TB

Sizes should probably be represented as u64, not usize

There are still a number of 32-bit CPUs in the wild, and the Linux x32 ABI uses 32-bit pointers on 64-bit architectures in order to save some memory bandwidth on programs with small working sets. On such platforms, Rust's usize will map to u32, and thus ByteSize will use an u32 counter of bytes internally. But that leads to a maximum representable size of 4 GiB, which is too small to adequately represent modern storage.

For this reason, and taking into account that u64 support can be efficiently emulated on 32-bit platforms, I would propose that ByteSize use u64 as a byte counter rather than usize.

MiB, but kiB?

 193.0 MiB debug
 128.2 MiB rls
   1.1 kiB .rustc_info.json
 321.3 MiB

Capitalization is different in to_string

Dimensionality ignored

1 KiB * 8 B = 8 KiB^2, not 8 KiB, and so forth. People seldom think in Bytes squared, unlike say meters squared, but the rules work the same.

This makes a bunch of the arithmetic operations numerically correct, but dimensionally incorrect.

Human-readable serde impl loses precision, serializes bytes incorrectly

Hi there, thanks for the crate!

While developing against the crate I found that human-readable serde impl's serializer is incorrect. For example, the following program:

use bytesize::ByteSize;

fn main() {
    let json = serde_json::to_string(&ByteSize::mib(1)).unwrap();
    println!("serialized JSON: {json}");

    let deserialized: ByteSize = serde_json::from_str(&json).unwrap();
    println!("deserialized: {}", deserialized.0);
}

prints out:

serialized JSON: "1048.6 KB"
deserialized: 1048600

Which means that serialization/deserialization does not roundtrip.

I think serialization should either be to an integer, or to exact values (e.g. "1048.576 KB"). What do you think?

Implement `Sub` trait

Trying to subtract two ByteSize, I noticed that Sub is not implemented.

I can convert to u64 and sub, but it would be better if I could subtract directly.

Display for IEC units

By default the Display implementation is using SI units (e.g. KB), which looks weird when printing those:

      --http-server-request-limit <http-server-request-limit>
          The overall request limit [env: HTTP_SERVER_REQUEST_LIMIT=] [default: "262.1 KB"]
      --http-server-json-limit <http-server-json-limit>
          The JSON request limit [env: HTTP_SERVER_JSON_LIMIT=] [default: "2.1 MB"]

It would be great if there would be a newtype for defaulting to "binary format" (powers of 2, e.g. KiB).

1907 MiB is displayed as 1 GB

Given this code:

println!("{}", ByteSize::mib(1907));
println!("{}", ByteSize::mib(1908));

I get this output:

1 GB
2 GB

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.