hyunsik / bytesize Goto Github PK
View Code? Open in Web Editor NEWAn utility that easily makes bytes size representation and helps its arithmetic operations.
License: Apache License 2.0
An utility that easily makes bytes size representation and helps its arithmetic operations.
License: Apache License 2.0
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.
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.
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.
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,
}
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
?
#11 removed the implementation of Sub
for ByteSize
as well as the as_usize
method. The example in the documentation tries to use both:
Line 18 in 9d8e13f
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`
let block_sectors = 1953458176;
let size = ByteSize::kib(block_sectors / 2);
println!("{}", size);
931.48 GiB
1000.2 GB
Lines 63 to 64 in 0282291
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
So that it is possible to use {}
for decimal units (KB, MB, GB), and {:#}
for binary units (KiB, MiB, GiB).
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.
193.0 MiB debug
128.2 MiB rls
1.1 kiB .rustc_info.json
321.3 MiB
Capitalization is different in to_string
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.
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?
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.
Currently, the effect of the si_unit
argument of ByteSize::to_string_as
seems to be reversed. According to Wikipedia, SI units are decimal units, and IEC units are binary 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
).
Given this code:
println!("{}", ByteSize::mib(1907));
println!("{}", ByteSize::mib(1908));
I get this output:
1 GB
2 GB
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.