Skip to content

Commit

Permalink
Add Size::new
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Nov 3, 2023
1 parent e137f68 commit 463e72a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/vm/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn bench_cache(c: &mut Criterion) {
let non_memcache = CacheOptions::new(
TempDir::new().unwrap().into_path(),
capabilities_from_csv("iterator,staking"),
Size::kilo(0),
Size::new(0),
DEFAULT_MEMORY_LIMIT,
);
let cache: Cache<MockApi, MockStorage, MockQuerier> =
Expand All @@ -199,7 +199,7 @@ fn bench_cache(c: &mut Criterion) {
let non_memcache = CacheOptions::new(
TempDir::new().unwrap().into_path(),
capabilities_from_csv("iterator,staking"),
Size::kilo(0),
Size::new(0),
DEFAULT_MEMORY_LIMIT,
);
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
Expand Down
13 changes: 9 additions & 4 deletions packages/vm/src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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)");
}
Expand Down
8 changes: 4 additions & 4 deletions packages/vm/src/wasm_backend/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 463e72a

Please sign in to comment.