Skip to content

Commit 1337d73

Browse files
committed
Make Base tunables generic in LimitingTunables
1 parent 984d478 commit 1337d73

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

examples/tunables_limit_memory.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@ use wasmer::{
55
imports,
66
vm::{self, MemoryError, MemoryStyle, TableStyle, VMMemoryDefinition, VMTableDefinition},
77
wat2wasm, Instance, Memory, MemoryType, Module, Pages, Store, TableType, Target,
8-
Tunables as BaseTunables,
8+
Tunables as ReferenceTunables,
99
};
1010
use wasmer_compiler_cranelift::Cranelift;
1111
use wasmer_engine::Tunables;
1212
use wasmer_engine_jit::JIT;
1313

14-
/// A custom tunables that allows you to set a memory limit
15-
struct LimitingTunables {
14+
/// A custom tunables that allows you to set a memory limit.
15+
///
16+
/// After adjusting the memory limits, it delegates all other logic
17+
/// to the base tunables.
18+
struct LimitingTunables<T: Tunables> {
1619
/// The maxium a linear memory is allowed to be (in Wasm pages, 65 KiB each).
1720
/// Since Wasmer ensures there is only none or one memory, this is practically
1821
/// an upper limit for the guest memory.
1922
max_memory: Pages,
2023
/// The base implementation we delegate all the logic to
21-
base: BaseTunables,
24+
base: T,
2225
}
2326

24-
impl LimitingTunables {
25-
pub fn for_target(target: &Target, limit: Pages) -> Self {
27+
impl<T: Tunables> LimitingTunables<T> {
28+
pub fn new(base: T, limit: Pages) -> Self {
2629
Self {
2730
max_memory: limit,
28-
base: BaseTunables::for_target(target),
31+
base,
2932
}
3033
}
3134

@@ -52,7 +55,7 @@ impl LimitingTunables {
5255
}
5356
}
5457

55-
impl Tunables for LimitingTunables {
58+
impl<T: Tunables> Tunables for LimitingTunables<T> {
5659
/// Construct a `MemoryStyle` for the provided `MemoryType`
5760
///
5861
/// Delegated to base.
@@ -132,8 +135,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
132135

133136
// Here is where the fun begins
134137

135-
let target = Target::default(); // TODO: should this use engine.target(), which is private?
136-
let tunables = LimitingTunables::for_target(&target, Pages(24));
138+
let base = ReferenceTunables::for_target(&Target::default());
139+
let tunables = LimitingTunables::new(base, Pages(24));
137140

138141
// Create a store, that holds the engine and our custom tunables
139142
let store = Store::new_with_tunables(&engine, tunables);

0 commit comments

Comments
 (0)