Skip to content

Commit

Permalink
Some new utilities (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke authored Jun 13, 2024
1 parent d67e627 commit 40c1445
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 8 deletions.
31 changes: 30 additions & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ parking_lot = { version = "0.12", features = ["deadlock_detection", "serde"] }
serde = { version = "1", features = ["derive", "rc"] }
time = { version = "0.3", features = ["local-offset"] }
colored = '2'
futures = { version = "0.3", features = [] }
async-semaphore = "1.2"
gloo-timers = { version = "0.3", features = ["futures"] }

# Not in default, but randomly useful in features:
strum = { version = "0.25", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
rand = { version = "0.8", optional = true }
futures = { version = "0.3", optional = true }
uuid = { version = "1.6", features = ["v4"], optional = true }
tokio = { version = '1', optional = true }

# FEAT: chrono: (but also sometimes enabled by other features)
chrono = { version = '0.4', optional = true }
Expand Down Expand Up @@ -83,19 +84,19 @@ sysinfo = { version = "0.30", optional = true }
tracing-subscriber-wasm = "0.1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tracing-appender = '0.2' # This includes threading (non-blocking stuff that can't be used in wasm)
tracing-appender = '0.2' # This includes threading (non-blocking stuff that can't be used in wasm)
hostname = "0.3.1"
tokio = { version = '1', features = ["time"] }

[dev-dependencies]
rstest = "0.18"
criterion = { version = "0.3", features = ["html_reports", "async_futures"] }
portpicker = '0.1.1'
tempfile = '3.8'
tokio = { version = '1', features = ["rt-multi-thread", "macros"] }
tokio = { version = '1', features = ["time", "rt-multi-thread", "macros"] }
uuid = { version = "1.6", features = ["v4"] }
regex = "1"
serde_json = "1"
futures = "0.3"

# When adding new benches, they should be added like this with the name of the file in benches/: (obviously uncommented)
# [[bench]]
Expand All @@ -109,14 +110,12 @@ timing = ['dep:comfy-table', 'chrono']
cli = ['dep:normpath', 'dep:conch-parser', 'dep:homedir', 'chrono', 'dep:strum']
system = ['dep:sysinfo']
redis = [
'dep:tokio',
'dep:deadpool-redis',
'dep:redis',
"dep:redis-macros",
'dep:sha1_smol',
'dep:serde_json',
'dep:rand',
'dep:futures',
'chrono',
'dep:uuid',
]
Expand Down
2 changes: 2 additions & 0 deletions rust/bitbazaar/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub mod misc;
#[cfg(feature = "redis")]
/// Redis utilities
pub mod redis;
/// Concurrency/parallelism utilities
pub mod threads;
#[cfg(feature = "timing")]
/// Timing utilities
pub mod timing;
30 changes: 30 additions & 0 deletions rust/bitbazaar/misc/bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Prettify bytes into a string for a user using the 1000 base.
pub fn bytes_to_pretty_1000(bytes: u64) -> String {
static UNITS: [&str; 9] = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
static BASE: f64 = 1000.0;
let mut size = bytes as f64;
let mut unit = 0;
while size >= BASE {
size /= BASE;
unit += 1;
}
format!("{:.1}{}", size, UNITS[unit])
}

/// Prettify bytes into a string for a user using the 1024 base.
pub fn bytes_to_pretty_1024(bytes: u64) -> String {
static UNITS: [&str; 9] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
static BASE: f64 = 1024.0;
let mut size = bytes as f64;
let mut unit = 0;
while size >= BASE {
size /= BASE;
unit += 1;
}
format!("{:.1}{}", size, UNITS[unit])
}

/// Convert bytes to megabits per second.
pub fn bytes_to_mbps(bytes: u64) -> f64 {
bytes as f64 / 1024.0 / 1024.0 * 8.0
}
3 changes: 3 additions & 0 deletions rust/bitbazaar/misc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// Byte manipulation utilities, e.g. transfer speed.
pub mod bytes;

mod in_ci;
mod is_tcp_port_listening;

Expand Down
Loading

0 comments on commit 40c1445

Please sign in to comment.