{"id":21586133,"url":"https://github.com/magiclen/byte-unit","last_synced_at":"2025-04-06T07:15:32.577Z","repository":{"id":32793159,"uuid":"142774954","full_name":"magiclen/Byte-Unit","owner":"magiclen","description":"A library for interaction with units of bytes.","archived":false,"fork":false,"pushed_at":"2024-01-23T12:14:14.000Z","size":165,"stargazers_count":49,"open_issues_count":5,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-03T04:47:46.897Z","etag":null,"topics":["byte-units","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magiclen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-29T16:00:55.000Z","updated_at":"2024-06-19T01:37:33.669Z","dependencies_parsed_at":"2023-11-25T08:21:13.318Z","dependency_job_id":"650478eb-104e-4433-9826-8fc25bd7481c","html_url":"https://github.com/magiclen/Byte-Unit","commit_stats":{"total_commits":71,"total_committers":7,"mean_commits":"10.142857142857142","dds":"0.12676056338028174","last_synced_commit":"915f1dcdb34e51caa9d2a5d3aa2efe261f0eed3a"},"previous_names":[],"tags_count":53,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2FByte-Unit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2FByte-Unit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2FByte-Unit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2FByte-Unit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/Byte-Unit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445682,"owners_count":20939961,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["byte-units","rust"],"created_at":"2024-11-24T15:12:46.066Z","updated_at":"2025-04-06T07:15:32.542Z","avatar_url":"https://github.com/magiclen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"Byte Unit\n====================\n\n[![CI](https://github.com/magiclen/Byte-Unit/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/Byte-Unit/actions/workflows/ci.yml)\n\nA library for interaction with units of bytes.\n\nThe units are **B** for 1 byte, **KB** for 1000 bytes, **MiB** for 1048576 bytes, **GB** for 1000000000 bytes, etc, and up to **E** or **Y** (if the `u128` feature is enabled).\n\n## Usage\n\nThe data types for storing the size in bits/bytes are `u64` by default, meaning the highest supported unit is up to **E**. If the `u128` feature is enabled, the data types will use `u128`, increasing the highest supported unit up to **Y**.\n\n### Unit\n\nThe enum `Unit` can be used for representing the unit of bits/bytes.\n\n```rust\nuse byte_unit::Unit;\n\nassert_eq!(\"KB\", Unit::KB.as_str());\nassert_eq!(\"MiB\", Unit::MiB.as_str());\n\nassert_eq!(Unit::KB, Unit::parse_str(\"K\", true, true).unwrap());\nassert_eq!(Unit::Kbit, Unit::parse_str(\"K\", true, false).unwrap());\n\nassert_eq!(Unit::KB, Unit::parse_str(\"KB\", true, true).unwrap());\nassert_eq!(Unit::KB, Unit::parse_str(\"Kb\", true, true).unwrap());\nassert_eq!(Unit::Kbit, Unit::parse_str(\"Kbit\", true, true).unwrap());\n\nassert_eq!(Unit::KB, Unit::parse_str(\"KB\", false, true).unwrap());\nassert_eq!(Unit::Kbit, Unit::parse_str(\"Kb\", false, true).unwrap());\n```\n\n### Byte\n\nThe `Byte` struct can be used for representing a size in bytes.\n\nThe `from_*` associated functions can be used to create a `Byte` instance from different data types.  The `as_*` methods can retrieve the size as a primitive type.\n\n```rust\nuse byte_unit::{Byte, Unit};\n\nassert_eq!(15000, Byte::from_u64(15000).as_u64());\nassert_eq!(15000, Byte::from_u64_with_unit(15, Unit::KB).unwrap().as_u64());\n```\n\nYou can also parse a string to create a `Byte` instance.\n\n```rust\nuse byte_unit::Byte;\n\nassert_eq!(50840000, Byte::parse_str(\"50.84 MB\", true).unwrap().as_u64());\n```\n\nA `Byte` instance can be formatted to string precisely. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `Byte`.\n\n```rust\nuse byte_unit::Byte;\n\nlet byte = Byte::from_u64(15500);\n\nassert_eq!(\"15500\", byte.to_string());\n\nassert_eq!(\"15.5 KB\", format!(\"{byte:#}\"));\nassert_eq!(\"15500 B\", format!(\"{byte:#.0}\"));\n```\n\n#### Arithmetic\n\nThere are `add`, `subtract`, `multiply`, and `divide` methods.\n\n```rust\nuse byte_unit::Byte;\n\nlet a = Byte::from_u64(15500);\nlet b = Byte::from_u64(500);\n\nassert_eq!(16000, a.add(b).unwrap().as_u64());\nassert_eq!(15000, a.subtract(b).unwrap().as_u64());\n\nassert_eq!(31000, a.multiply(2).unwrap().as_u64());\nassert_eq!(3100, a.divide(5).unwrap().as_u64());\n```\n\n#### Find Out an Appropriate Unit\n\nThe `get_exact_unit` and `get_recoverable_unit` methods is useful if you want to find out a unit that is appropriate for a `Byte` instance.\n\n```rust\nuse byte_unit::{Byte, Unit};\n\nlet byte = Byte::from_u64(50840000);\n\nassert_eq!((50840, Unit::KB), byte.get_exact_unit(false));\nassert_eq!((50.84f64.try_into().unwrap(), Unit::MB), byte.get_recoverable_unit(false, 2));\nassert_eq!((50840.into(), Unit::KB), byte.get_recoverable_unit(false, 0));\n```\n\n#### AdjustedByte\n\nThe `AdjustedByte` struct can be used for roughly representing a size of bytes with a unit.\n\nTo change the unit of a `Byte` instance, you can use the `get_adjusted_unit` method.\n\nAn `AdjustedByte` instance can be formatted to string. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `AdjustedByte`.\n\n```rust\nuse byte_unit::{Byte, Unit};\n\nlet byte = Byte::parse_str(\"123KiB\", true).unwrap();\n\nlet adjusted_byte = byte.get_adjusted_unit(Unit::KB);\n\nassert_eq!(\"125.952 KB\", adjusted_byte.to_string());\nassert_eq!(\"125.95 KB\", format!(\"{adjusted_byte:.2}\"));\n```\n\nThe `get_appropriate_unit` method can be used to automatically find an appropriate unit for creating an `AdjustedByte` instance.\n\n```rust\nuse byte_unit::{Byte, Unit, UnitType};\n\nlet byte = Byte::from_u64(1500000);\n\nlet adjusted_byte = byte.get_appropriate_unit(UnitType::Binary);\n\nassert_eq!(\"1.43 MiB\", format!(\"{adjusted_byte:.2}\"));\n```\n\n### Bit\n\nThe `Bit` struct can be used for representing a size in bits.\n\nThe `bit` feature must be enabled.\n\nUsage of the `Bit` struct and the `Byte` struct is very similar. Also, There is the `AdjustedBit` struct. The difference lies in the fact that the `parse_str` method of the `Bit` struct cannot be configured to ignore case; it always does not ignore case.\n\n```rust\nuse byte_unit::{Bit, Unit};\n\nlet bit = Bit::parse_str(\"123Kib\").unwrap();\n\nlet adjusted_bit = bit.get_adjusted_unit(Unit::Kbit);\n\nassert_eq!(\"125.952 Kb\", adjusted_bit.to_string());\nassert_eq!(\"125.95 Kb\", format!(\"{adjusted_bit:.2}\"));\n```\n\n## No Std\n\nDisable the default features to compile this crate without std.\n\n```toml\n[dependencies.byte-unit]\nversion = \"*\"\ndefault-features = false\nfeatures = [\"byte\"]\n```\n\n## Serde Support\n\nEnable the `serde` feature to support the serde framework.\n\n```toml\n[dependencies.byte-unit]\nversion = \"*\"\nfeatures = [\"serde\"]\n```\n\n## Rocket Support\n\nEnable the `rocket` feature to support the Rocket framework.\n\n```toml\n[dependencies.byte-unit]\nversion = \"*\"\nfeatures = [\"rocket\"]\n```\n\n## Crates.io\n\nhttps://crates.io/crates/byte-unit\n\n## Documentation\n\nhttps://docs.rs/byte-unit\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fbyte-unit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Fbyte-unit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fbyte-unit/lists"}