From 39660cae8d2b442c43bd4eda4dca7d0f3ec38170 Mon Sep 17 00:00:00 2001 From: Richard Schneeman Date: Fri, 4 Oct 2024 10:27:35 -0500 Subject: [PATCH] Update byte-unit dependency with breaking changes (#329) Byte unit 4 broke a lot of interfaces that we depended on. This commit uses the new interfaces: - Byte::from_bytes -> Byte::from_ (like Byte::from_u64) - Byte.get_appropriate_unit(bool) -> Byte.get_appropriate_unit(UnitType) - macros such as `n_mib_bytes` are no longer present. This has one (technically) breaking change in the `mib` convenience function provided by commons. However if a literal is being used in the code (which it is in the Ruby main.rs) then it is not affected. Even though lots of things changed internally, the overall interface to AppCache stayed (almost) stable and no code inside of the main buildpack needed to be updated. That indicates a relatively successful encapsulation attempt. Moving forward it would be best to not directly rely on types from external crates but rather to provide new type interfaces for them so we have control over the API and therefore stability. --- Cargo.lock | 285 ++++++++++++++++++++++++++++++++- commons/Cargo.toml | 2 +- commons/src/cache/app_cache.rs | 8 +- commons/src/cache/clean.rs | 29 ++-- commons/src/cache/config.rs | 6 +- 5 files changed, 297 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f905243..13c2357 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,17 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "ahash" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -23,6 +34,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "ascii_table" version = "4.0.4" @@ -66,6 +83,18 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -75,6 +104,30 @@ dependencies = [ "generic-array", ] +[[package]] +name = "borsh" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6362ed55def622cddc70a4746a68554d7b687713770de539e59a739b249f8ed" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3ef8005764f53cd4dca619f5bf64cafd4664dada50ece25e4d81de54c80cc0b" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.76", + "syn_derive", +] + [[package]] name = "bstr" version = "1.9.1" @@ -93,14 +146,43 @@ checksum = "bec939946cfeb7d4ba709fb24f035a14b78b89641cd4a09434a6d317c0ae28a6" [[package]] name = "byte-unit" -version = "4.0.19" +version = "5.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da78b32057b8fdfc352504708feeba7216dcd65a2c9ab02978cbd288d1279b6c" +checksum = "33ac19bdf0b2665407c39d82dbc937e951e7e2001609f0fb32edd0af45a2d63e" dependencies = [ + "rust_decimal", "serde", "utf8-width", ] +[[package]] +name = "bytecheck" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cdc57ce23ac53c931e88a43d06d070a6fd142f2617be5855eb75efc9beb1c2" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db406d29fbcd95542e92559bed4d8ad92636d1ca8b3b72ede10b4bcc010e659" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "bytes" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" + [[package]] name = "camino" version = "1.1.6" @@ -148,6 +230,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "clap" version = "4.5.18" @@ -177,7 +265,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.76", ] [[package]] @@ -412,6 +500,12 @@ dependencies = [ "which_problem", ] +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "generic-array" version = "0.14.7" @@ -452,6 +546,15 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.14.3" @@ -533,7 +636,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.14.3", ] [[package]] @@ -643,7 +746,7 @@ dependencies = [ "cargo_metadata", "fancy-regex", "quote", - "syn", + "syn 2.0.76", ] [[package]] @@ -792,6 +895,38 @@ dependencies = [ "yansi", ] +[[package]] +name = "proc-macro-crate" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -801,6 +936,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "ptr_meta" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" +dependencies = [ + "ptr_meta_derive", +] + +[[package]] +name = "ptr_meta_derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "quote" version = "1.0.37" @@ -810,6 +965,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.8.5" @@ -898,6 +1059,15 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rend" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71fe3824f5629716b1589be05dacd749f6aa084c87e00e016714a8cdfccc997c" +dependencies = [ + "bytecheck", +] + [[package]] name = "ring" version = "0.17.8" @@ -913,6 +1083,51 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rkyv" +version = "0.7.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9008cd6385b9e161d8229e1f6549dd23c3d022f132a2ea37ac3a10ac4935779b" +dependencies = [ + "bitvec", + "bytecheck", + "bytes", + "hashbrown 0.12.3", + "ptr_meta", + "rend", + "rkyv_derive", + "seahash", + "tinyvec", + "uuid", +] + +[[package]] +name = "rkyv_derive" +version = "0.7.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "503d1d27590a2b0a3a4ca4c94755aa2875657196ecbf401a42eff41d7de532c0" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "rust_decimal" +version = "1.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b082d80e3e3cc52b2ed634388d436fe1f4de6af5786cc2de9ba9737527bdf555" +dependencies = [ + "arrayvec", + "borsh", + "bytes", + "num-traits", + "rand", + "rkyv", + "serde", + "serde_json", +] + [[package]] name = "rustix" version = "0.38.37" @@ -973,6 +1188,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "seahash" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" + [[package]] name = "semver" version = "1.0.22" @@ -999,7 +1220,7 @@ checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.76", ] [[package]] @@ -1039,6 +1260,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + [[package]] name = "spin" version = "0.9.8" @@ -1057,6 +1284,17 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.76" @@ -1068,6 +1306,24 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.76", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tar" version = "0.4.42" @@ -1108,7 +1364,7 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.76", ] [[package]] @@ -1241,6 +1497,12 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3" +[[package]] +name = "uuid" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" + [[package]] name = "vcpkg" version = "0.2.15" @@ -1431,6 +1693,15 @@ version = "0.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "yansi" version = "1.0.1" diff --git a/commons/Cargo.toml b/commons/Cargo.toml index 2046624..c1b1fd1 100644 --- a/commons/Cargo.toml +++ b/commons/Cargo.toml @@ -12,7 +12,7 @@ workspace = true [dependencies] ascii_table = { version = "4", features = ["color_codes"] } -byte-unit = "4" +byte-unit = "5" const_format = "0.2" # TODO: Consolidate on either the regex crate or the fancy-regex crate, since this repo currently uses both. fancy-regex = "0.13" diff --git a/commons/src/cache/app_cache.rs b/commons/src/cache/app_cache.rs index 024a624..bf1434b 100644 --- a/commons/src/cache/app_cache.rs +++ b/commons/src/cache/app_cache.rs @@ -1,7 +1,7 @@ use crate::cache::clean::{lru_clean, FilesWithSize}; use crate::cache::in_app_dir_cache_layer::InAppDirCacheLayer; use crate::cache::{CacheConfig, CacheError, KeepPath}; -use byte_unit::{AdjustedByte, Byte}; +use byte_unit::{AdjustedByte, Byte, UnitType}; use fs_extra::dir::CopyOptions; use libcnb::build::BuildContext; use libcnb::data::layer::LayerName; @@ -106,7 +106,7 @@ impl AppCache { /// The value (in adjusted bytes) of the limit for the cached directory #[must_use] pub fn limit(&self) -> AdjustedByte { - self.limit.get_appropriate_unit(true) + self.limit.get_appropriate_unit(UnitType::Binary) } /// The state of the cache directory when the object was created @@ -395,7 +395,7 @@ mod tests { let store = AppCache { path: app_path.clone(), cache: cache_path, - limit: Byte::from_bytes(512), + limit: Byte::from_u64(512), keep_path: KeepPath::Runtime, cache_state: CacheState::NewEmpty, }; @@ -426,7 +426,7 @@ mod tests { let store = AppCache { path: app_path.clone(), cache: cache_path, - limit: Byte::from_bytes(512), + limit: Byte::from_u64(512), keep_path: KeepPath::BuildOnly, cache_state: CacheState::NewEmpty, }; diff --git a/commons/src/cache/clean.rs b/commons/src/cache/clean.rs index a35f43e..fdbeb15 100644 --- a/commons/src/cache/clean.rs +++ b/commons/src/cache/clean.rs @@ -1,5 +1,5 @@ use crate::cache::CacheError; -use byte_unit::{AdjustedByte, Byte}; +use byte_unit::{AdjustedByte, Byte, UnitType}; use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; use std::time::SystemTime; @@ -50,7 +50,7 @@ fn files(cache_path: &Path) -> Result, CacheError> { /// `FilesWithSize`. If later deleted, those values will reduce the total size of the directory /// below the limit. fn lru_files_above_limit(cache_path: &Path, limit: Byte) -> Result { - let max_bytes = limit.get_bytes(); + let max_bytes = limit.as_u128(); let mut files = files(cache_path)?; let bytes = files.iter().map(|p| u128::from(p.size)).sum::(); @@ -87,7 +87,7 @@ pub struct FilesWithSize { impl FilesWithSize { #[must_use] pub fn to_byte(&self) -> Byte { - Byte::from_bytes(self.bytes) + Byte::from_u128(self.bytes).unwrap_or(Byte::MAX) } /// Return byte value with adjusted units. @@ -95,7 +95,7 @@ impl FilesWithSize { /// When formatted the units will be included. #[must_use] pub fn adjusted_bytes(&self) -> AdjustedByte { - self.to_byte().get_appropriate_unit(true) + self.to_byte().get_appropriate_unit(UnitType::Binary) } } @@ -125,6 +125,8 @@ impl MiniPathModSize { #[cfg(test)] mod tests { + use crate::cache::mib; + use super::*; #[test] @@ -144,8 +146,6 @@ mod tests { assert_eq!(out.len(), 1); } - use byte_unit::n_mib_bytes; - fn touch_file(path: &PathBuf, f: impl FnOnce(&PathBuf)) { if let Some(parent) = path.parent() { if !parent.exists() { @@ -164,19 +164,13 @@ mod tests { fs_err::create_dir_all(&dir).unwrap(); - assert_eq!( - lru_files_above_limit(&dir, Byte::from_bytes(n_mib_bytes!(0)),) - .unwrap() - .files - .len(), - 0 - ); + assert_eq!(lru_files_above_limit(&dir, mib(0),).unwrap().files.len(), 0); touch_file(&dir.join("a"), |file| { - let overage = lru_files_above_limit(&dir, Byte::from_bytes(n_mib_bytes!(0))).unwrap(); + let overage = lru_files_above_limit(&dir, mib(0)).unwrap(); assert_eq!(overage.files, vec![file.clone()]); - let overage = lru_files_above_limit(&dir, Byte::from_bytes(n_mib_bytes!(10))).unwrap(); + let overage = lru_files_above_limit(&dir, mib(10)).unwrap(); assert_eq!(overage.files.len(), 0); }); } @@ -191,8 +185,7 @@ mod tests { filetime::set_file_mtime(a, filetime::FileTime::from_unix_time(0, 0)).unwrap(); filetime::set_file_mtime(b, filetime::FileTime::from_unix_time(1, 0)).unwrap(); - let overage = - lru_files_above_limit(&dir, Byte::from_bytes(n_mib_bytes!(0))).unwrap(); + let overage = lru_files_above_limit(&dir, mib(0)).unwrap(); assert_eq!(overage.files, vec![a.clone(), b.clone()]); }); }); @@ -203,7 +196,7 @@ mod tests { let tmpdir = tempfile::tempdir().unwrap(); let dir = tmpdir.path().join(""); fs_err::create_dir_all(dir.join("preservation_society")).unwrap(); - let overage = lru_files_above_limit(&dir, Byte::from_bytes(n_mib_bytes!(0))).unwrap(); + let overage = lru_files_above_limit(&dir, mib(0)).unwrap(); assert_eq!(overage.files, Vec::::new()); } } diff --git a/commons/src/cache/config.rs b/commons/src/cache/config.rs index 364fd38..6ea1e65 100644 --- a/commons/src/cache/config.rs +++ b/commons/src/cache/config.rs @@ -1,4 +1,4 @@ -use byte_unit::{n_mib_bytes, Byte}; +use byte_unit::Byte; use std::path::PathBuf; /// Configure behavior of a cached path @@ -28,6 +28,6 @@ pub enum KeepPath { /// Returns a `Byte` value containing the number /// of mebibytes given. #[must_use] -pub fn mib(n_mebibytes: u128) -> Byte { - Byte::from_bytes(n_mib_bytes!(n_mebibytes)) +pub fn mib(n_mebibytes: usize) -> Byte { + Byte::MEBIBYTE.multiply(n_mebibytes).unwrap_or(Byte::MAX) }