From e267e59aafe4e0c316bd08ae5b9e6ef932dd938d Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Thu, 28 Sep 2023 11:00:21 +0200 Subject: [PATCH] Add Size::new --- packages/vm/benches/main.rs | 4 ++-- packages/vm/src/size.rs | 13 +++++++++---- packages/vm/src/wasm_backend/engine.rs | 8 ++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/vm/benches/main.rs b/packages/vm/benches/main.rs index ce5846fc74..da4aec5bc6 100644 --- a/packages/vm/benches/main.rs +++ b/packages/vm/benches/main.rs @@ -177,7 +177,7 @@ fn bench_cache(c: &mut Criterion) { let non_memcache = CacheOptions { base_dir: TempDir::new().unwrap().into_path(), available_capabilities: capabilities_from_csv("iterator,staking"), - memory_cache_size: Size::kilo(0), + memory_cache_size: Size::new(0), instance_memory_limit: DEFAULT_MEMORY_LIMIT, }; let cache: Cache = @@ -199,7 +199,7 @@ fn bench_cache(c: &mut Criterion) { let non_memcache = CacheOptions { base_dir: TempDir::new().unwrap().into_path(), available_capabilities: capabilities_from_csv("iterator,staking"), - memory_cache_size: Size::kilo(0), + memory_cache_size: Size::new(0), instance_memory_limit: DEFAULT_MEMORY_LIMIT, }; let mut cache: Cache = diff --git a/packages/vm/src/size.rs b/packages/vm/src/size.rs index c6f4c88520..c43a3a1a6b 100644 --- a/packages/vm/src/size.rs +++ b/packages/vm/src/size.rs @@ -2,6 +2,11 @@ pub struct Size(pub(crate) usize); impl Size { + /// Creates a size of `n` + pub const fn new(n: usize) -> Self { + Size(n) + } + /// Creates a size of `n` kilo pub const fn kilo(n: usize) -> Self { Size(n * 1000) @@ -39,8 +44,8 @@ mod tests { #[test] fn constructors_work() { - assert_eq!(Size(0).0, 0); - assert_eq!(Size(3).0, 3); + assert_eq!(Size::new(0).0, Size(0).0); + assert_eq!(Size::new(3).0, Size(3).0); assert_eq!(Size::kilo(0).0, 0); assert_eq!(Size::kilo(3).0, 3000); @@ -63,8 +68,8 @@ mod tests { #[test] fn implements_debug() { - assert_eq!(format!("{:?}", Size(0)), "Size(0)"); - assert_eq!(format!("{:?}", Size(123)), "Size(123)"); + assert_eq!(format!("{:?}", Size::new(0)), "Size(0)"); + assert_eq!(format!("{:?}", Size::new(123)), "Size(123)"); assert_eq!(format!("{:?}", Size::kibi(2)), "Size(2048)"); assert_eq!(format!("{:?}", Size::mebi(1)), "Size(1048576)"); } diff --git a/packages/vm/src/wasm_backend/engine.rs b/packages/vm/src/wasm_backend/engine.rs index 575950c275..b5b67e660a 100644 --- a/packages/vm/src/wasm_backend/engine.rs +++ b/packages/vm/src/wasm_backend/engine.rs @@ -87,16 +87,16 @@ mod tests { #[test] fn limit_to_pages_works() { // rounds down - assert_eq!(limit_to_pages(Size::kilo(0)), Pages(0)); - assert_eq!(limit_to_pages(Size(1)), Pages(0)); + assert_eq!(limit_to_pages(Size::new(0)), Pages(0)); + assert_eq!(limit_to_pages(Size::new(1)), Pages(0)); assert_eq!(limit_to_pages(Size::kibi(63)), Pages(0)); assert_eq!(limit_to_pages(Size::kibi(64)), Pages(1)); assert_eq!(limit_to_pages(Size::kibi(65)), Pages(1)); - assert_eq!(limit_to_pages(Size(u32::MAX as usize)), Pages(65535)); + assert_eq!(limit_to_pages(Size::new(u32::MAX as usize)), Pages(65535)); // caps at 4 GiB assert_eq!(limit_to_pages(Size::gibi(3)), Pages(49152)); assert_eq!(limit_to_pages(Size::gibi(4)), Pages(65536)); assert_eq!(limit_to_pages(Size::gibi(5)), Pages(65536)); - assert_eq!(limit_to_pages(Size(usize::MAX)), Pages(65536)); + assert_eq!(limit_to_pages(Size::new(usize::MAX)), Pages(65536)); } }