Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,40 @@ on:
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
rust:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable
with:
submodules: true # Fetch and checkout submodules
components: clippy, rustfmt

# Rust cache
- uses: Swatinem/rust-cache@v2

- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: clippy, rustfmt

- name: Install cargo-sort
run: cargo install cargo-sort

- name: Cancel previous runs
uses: styfle/cancel-workflow-action@0.9.1
uses: taiki-e/install-action@v2
with:
access_token: ${{ github.token }}
tool: cargo-sort

- name: Check formatting
run: cargo fmt -- --check
run: cargo fmt --all -- --check

- name: Check TOML files are sorted
run: cargo sort --workspace --check

- name: Clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features -- -Dclippy::all
run: cargo clippy --workspace --all-features --all-targets -- -Dclippy::all

- name: Test
run: cargo test
run: cargo test --workspace --all-features

- name: Check TOML files are sorted
run: cargo sort --check
- name: Check no_std
run: cargo check -p humanbyte --no-default-features
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[workspace]
members = [
"bytescale",
"humanbyte",
"humanbyte-derive"
]
members = ["bytescale", "humanbyte", "humanbyte-derive"]
resolver = "2"
14 changes: 7 additions & 7 deletions bytescale/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ license = "Apache-2.0"
edition = "2021"
rust-version = "1.65"

[features]
default = ["std", "derive"]
std = ["humanbyte/std"]
derive = []
arbitrary = ["dep:arbitrary", "std"]
serde = ["humanbyte/serde"]

[dependencies]
arbitrary = { version = "1", features = ["derive"], optional = true }
humanbyte = { version = "0.2.1-alpha.0", path = "../humanbyte", features = ["derive"] }
Expand All @@ -18,10 +25,3 @@ humanbyte = { version = "0.2.1-alpha.0", path = "../humanbyte", features = ["der
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["std"] }
toml = "0.8"

[features]
default = ["std", "derive"]
std = ["humanbyte/std"]
derive = []
arbitrary = ["dep:arbitrary", "std"]
serde = ["humanbyte/serde"]
4 changes: 2 additions & 2 deletions bytescale/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod tests {
fn test_arithmetic_primitives() {
let mut x = ByteScale::mb(1);

assert_eq!((x + MB as u64).as_u64(), 2_000_000);
assert_eq!((x + MB).as_u64(), 2_000_000);

assert_eq!((x + MB as u32).as_u64(), 2_000_000);

Expand All @@ -60,7 +60,7 @@ mod tests {

assert_eq!((x - B as u32).as_u64(), 999_999);

x += MB as u64;
x += MB;
x += MB as u32;
x += 10u16;
x += 1u8;
Expand Down
8 changes: 4 additions & 4 deletions humanbyte-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ license = "Apache-2.0"
[lib]
proc-macro = true

[features]
default = []
serde = []

[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }

[features]
default = []
serde = []
8 changes: 4 additions & 4 deletions humanbyte/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ description = "A procedural macro for deriving human readable byte functions"
keywords = ["byte-size", "utility", "human-readable", "no_std"]
license = "Apache-2.0"

[dependencies]
humanbyte-derive = { version = "0.2.1-alpha.0", path = "../humanbyte-derive", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

[features]
default = ["std"]
std = []
derive = ["dep:humanbyte-derive"]
serde = ["dep:serde", "std", "humanbyte-derive/serde"]

[dependencies]
humanbyte-derive = { version = "0.2.1-alpha.0", path = "../humanbyte-derive", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
26 changes: 10 additions & 16 deletions humanbyte/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]

//! Common types and functions for byte size handling
extern crate alloc;
Expand Down Expand Up @@ -52,10 +51,6 @@ const UNITS_IEC: &str = "KMGTPE";
///
/// See <https://en.wikipedia.org/wiki/Kilobyte>.
const UNITS_SI: &str = "kMGTPE";
/// `ln(1024) ~= 6.931`
const LN_KIB: f64 = 6.931_471_805_599_453;
/// `ln(1000) ~= 6.908`
const LN_KB: f64 = 6.907_755_278_982_137;
#[derive(Debug, Clone, Default)]
pub enum Format {
#[default]
Expand All @@ -68,10 +63,6 @@ pub fn to_string(bytes: u64, format: Format) -> String {
Format::IEC => KIB,
Format::SI => KB,
};
let unit_base = match format {
Format::IEC => LN_KIB,
Format::SI => LN_KB,
};
let unit_prefix = match format {
Format::IEC => UNITS_IEC.as_bytes(),
Format::SI => UNITS_SI.as_bytes(),
Expand All @@ -83,15 +74,18 @@ pub fn to_string(bytes: u64, format: Format) -> String {
if bytes < unit {
format!("{} B", bytes)
} else {
let size = bytes as f64;
let exp = match (size.ln() / unit_base) as usize {
0 => 1,
e => e,
};
// Integer log: largest exp such that bytes >= unit^exp.
// (f64::ln is unavailable in core, and this is exact at boundaries.)
let mut exp = 0u32;
let mut n = bytes;
while n >= unit {
n /= unit;
exp += 1;
}
format!(
"{:.1} {}{}",
(size / unit.pow(exp as u32) as f64),
unit_prefix[exp - 1] as char,
(bytes as f64 / unit.pow(exp) as f64),
unit_prefix[(exp - 1) as usize] as char,
unit_suffix
)
}
Expand Down
Loading