Skip to content

Commit 0e5b565

Browse files
authored
Make ConstExpr implementation a bit more concise (#2059)
1 parent 41f183b commit 0e5b565

File tree

1 file changed

+30
-68
lines changed
  • crates/wasm-encoder/src/core

1 file changed

+30
-68
lines changed

crates/wasm-encoder/src/core/code.rs

Lines changed: 30 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,169 +2157,131 @@ impl ConstExpr {
21572157
Self { bytes }
21582158
}
21592159

2160-
fn new(f: impl FnOnce(&mut InstructionSink)) -> Self {
2160+
fn new<F>(f: F) -> Self
2161+
where
2162+
for<'a, 'b> F: FnOnce(&'a mut InstructionSink<'b>) -> &'a mut InstructionSink<'b>,
2163+
{
21612164
let mut bytes = vec![];
21622165
f(&mut InstructionSink::new(&mut bytes));
21632166
Self { bytes }
21642167
}
21652168

2166-
fn with(mut self, f: impl FnOnce(&mut InstructionSink)) -> Self {
2169+
fn with<F>(mut self, f: F) -> Self
2170+
where
2171+
for<'a, 'b> F: FnOnce(&'a mut InstructionSink<'b>) -> &'a mut InstructionSink<'b>,
2172+
{
21672173
f(&mut InstructionSink::new(&mut self.bytes));
21682174
self
21692175
}
21702176

21712177
/// Create a constant expression containing a single `global.get` instruction.
21722178
pub fn global_get(index: u32) -> Self {
2173-
Self::new(|insn| {
2174-
insn.global_get(index);
2175-
})
2179+
Self::new(|insn| insn.global_get(index))
21762180
}
21772181

21782182
/// Create a constant expression containing a single `ref.null` instruction.
21792183
pub fn ref_null(ty: HeapType) -> Self {
2180-
Self::new(|insn| {
2181-
insn.ref_null(ty);
2182-
})
2184+
Self::new(|insn| insn.ref_null(ty))
21832185
}
21842186

21852187
/// Create a constant expression containing a single `ref.func` instruction.
21862188
pub fn ref_func(func: u32) -> Self {
2187-
Self::new(|insn| {
2188-
insn.ref_func(func);
2189-
})
2189+
Self::new(|insn| insn.ref_func(func))
21902190
}
21912191

21922192
/// Create a constant expression containing a single `i32.const` instruction.
21932193
pub fn i32_const(value: i32) -> Self {
2194-
Self::new(|insn| {
2195-
insn.i32_const(value);
2196-
})
2194+
Self::new(|insn| insn.i32_const(value))
21972195
}
21982196

21992197
/// Create a constant expression containing a single `i64.const` instruction.
22002198
pub fn i64_const(value: i64) -> Self {
2201-
Self::new(|insn| {
2202-
insn.i64_const(value);
2203-
})
2199+
Self::new(|insn| insn.i64_const(value))
22042200
}
22052201

22062202
/// Create a constant expression containing a single `f32.const` instruction.
22072203
pub fn f32_const(value: f32) -> Self {
2208-
Self::new(|insn| {
2209-
insn.f32_const(value);
2210-
})
2204+
Self::new(|insn| insn.f32_const(value))
22112205
}
22122206

22132207
/// Create a constant expression containing a single `f64.const` instruction.
22142208
pub fn f64_const(value: f64) -> Self {
2215-
Self::new(|insn| {
2216-
insn.f64_const(value);
2217-
})
2209+
Self::new(|insn| insn.f64_const(value))
22182210
}
22192211

22202212
/// Create a constant expression containing a single `v128.const` instruction.
22212213
pub fn v128_const(value: i128) -> Self {
2222-
Self::new(|insn| {
2223-
insn.v128_const(value);
2224-
})
2214+
Self::new(|insn| insn.v128_const(value))
22252215
}
22262216

22272217
/// Add a `global.get` instruction to this constant expression.
22282218
pub fn with_global_get(self, index: u32) -> Self {
2229-
self.with(|insn| {
2230-
insn.global_get(index);
2231-
})
2219+
self.with(|insn| insn.global_get(index))
22322220
}
22332221

22342222
/// Add a `ref.null` instruction to this constant expression.
22352223
pub fn with_ref_null(self, ty: HeapType) -> Self {
2236-
self.with(|insn| {
2237-
insn.ref_null(ty);
2238-
})
2224+
self.with(|insn| insn.ref_null(ty))
22392225
}
22402226

22412227
/// Add a `ref.func` instruction to this constant expression.
22422228
pub fn with_ref_func(self, func: u32) -> Self {
2243-
self.with(|insn| {
2244-
insn.ref_func(func);
2245-
})
2229+
self.with(|insn| insn.ref_func(func))
22462230
}
22472231

22482232
/// Add an `i32.const` instruction to this constant expression.
22492233
pub fn with_i32_const(self, value: i32) -> Self {
2250-
self.with(|insn| {
2251-
insn.i32_const(value);
2252-
})
2234+
self.with(|insn| insn.i32_const(value))
22532235
}
22542236

22552237
/// Add an `i64.const` instruction to this constant expression.
22562238
pub fn with_i64_const(self, value: i64) -> Self {
2257-
self.with(|insn| {
2258-
insn.i64_const(value);
2259-
})
2239+
self.with(|insn| insn.i64_const(value))
22602240
}
22612241

22622242
/// Add a `f32.const` instruction to this constant expression.
22632243
pub fn with_f32_const(self, value: f32) -> Self {
2264-
self.with(|insn| {
2265-
insn.f32_const(value);
2266-
})
2244+
self.with(|insn| insn.f32_const(value))
22672245
}
22682246

22692247
/// Add a `f64.const` instruction to this constant expression.
22702248
pub fn with_f64_const(self, value: f64) -> Self {
2271-
self.with(|insn| {
2272-
insn.f64_const(value);
2273-
})
2249+
self.with(|insn| insn.f64_const(value))
22742250
}
22752251

22762252
/// Add a `v128.const` instruction to this constant expression.
22772253
pub fn with_v128_const(self, value: i128) -> Self {
2278-
self.with(|insn| {
2279-
insn.v128_const(value);
2280-
})
2254+
self.with(|insn| insn.v128_const(value))
22812255
}
22822256

22832257
/// Add an `i32.add` instruction to this constant expression.
22842258
pub fn with_i32_add(self) -> Self {
2285-
self.with(|insn| {
2286-
insn.i32_add();
2287-
})
2259+
self.with(|insn| insn.i32_add())
22882260
}
22892261

22902262
/// Add an `i32.sub` instruction to this constant expression.
22912263
pub fn with_i32_sub(self) -> Self {
2292-
self.with(|insn| {
2293-
insn.i32_sub();
2294-
})
2264+
self.with(|insn| insn.i32_sub())
22952265
}
22962266

22972267
/// Add an `i32.mul` instruction to this constant expression.
22982268
pub fn with_i32_mul(self) -> Self {
2299-
self.with(|insn| {
2300-
insn.i32_mul();
2301-
})
2269+
self.with(|insn| insn.i32_mul())
23022270
}
23032271

23042272
/// Add an `i64.add` instruction to this constant expression.
23052273
pub fn with_i64_add(self) -> Self {
2306-
self.with(|insn| {
2307-
insn.i64_add();
2308-
})
2274+
self.with(|insn| insn.i64_add())
23092275
}
23102276

23112277
/// Add an `i64.sub` instruction to this constant expression.
23122278
pub fn with_i64_sub(self) -> Self {
2313-
self.with(|insn| {
2314-
insn.i64_sub();
2315-
})
2279+
self.with(|insn| insn.i64_sub())
23162280
}
23172281

23182282
/// Add an `i64.mul` instruction to this constant expression.
23192283
pub fn with_i64_mul(self) -> Self {
2320-
self.with(|insn| {
2321-
insn.i64_mul();
2322-
})
2284+
self.with(|insn| insn.i64_mul())
23232285
}
23242286

23252287
/// Returns the function, if any, referenced by this global.

0 commit comments

Comments
 (0)