Skip to content

Commit dbbd72d

Browse files
jimpoVeykril
authored andcommitted
Run rustfmt
1 parent 9370ebd commit dbbd72d

File tree

4 files changed

+96
-70
lines changed

4 files changed

+96
-70
lines changed

src/function.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::str;
77
use crate::error::{Error, Result};
88
use crate::runtime::Runtime;
99
use crate::utils::cstr_to_str;
10-
use crate::{WasmArgs, WasmType, Module};
10+
use crate::{Module, WasmArgs, WasmType};
1111

1212
/// Calling Context for a host function.
1313
pub struct CallContext<'cc> {
@@ -108,8 +108,7 @@ where
108108
{
109109
fn validate_sig(func: NNM3Function) -> bool {
110110
let num_args = unsafe { ffi::m3_GetArgCount(func.as_ptr()) };
111-
let args = (0..num_args)
112-
.map(|i| unsafe { ffi::m3_GetArgType(func.as_ptr(), i) });
111+
let args = (0..num_args).map(|i| unsafe { ffi::m3_GetArgType(func.as_ptr(), i) });
113112
if !Args::validate_types(args) {
114113
return false;
115114
}
@@ -139,9 +138,8 @@ where
139138

140139
fn call_impl(&self, args: Args) -> Result<Ret> {
141140
let mut argv = args.ptrs_vec();
142-
let result = unsafe {
143-
ffi::m3_Call(self.raw.as_ptr(), argv.len() as u32, argv.as_mut_ptr())
144-
};
141+
let result =
142+
unsafe { ffi::m3_Call(self.raw.as_ptr(), argv.len() as u32, argv.as_mut_ptr()) };
145143
Error::from_ffi_res(result)?;
146144
unsafe {
147145
let mut ret = core::mem::MaybeUninit::<Ret>::uninit();

src/module.rs

+40-27
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ impl ParsedModule {
5050
}
5151

5252
pub(crate) fn take_data(self) -> Box<[u8]> {
53-
let ParsedModule { data, raw, env: _env } = self;
53+
let ParsedModule {
54+
data,
55+
raw,
56+
env: _env,
57+
} = self;
5458
mem::forget(raw);
5559
data
5660
}
@@ -114,7 +118,7 @@ impl<'rt> Module<'rt> {
114118
module_name_cstr.as_ptr(),
115119
function_name_cstr.as_ptr(),
116120
signature.as_ptr(),
117-
Some(f)
121+
Some(f),
118122
)
119123
};
120124
Error::from_ffi_res(result)
@@ -147,10 +151,10 @@ impl<'rt> Module<'rt> {
147151
sp: *mut u64,
148152
_mem: *mut cty::c_void,
149153
) -> *const cty::c_void
150-
where
151-
Args: crate::WasmArgs,
152-
Ret: crate::WasmType,
153-
F: for<'cc> FnMut(CallContext<'cc>, Args) -> core::result::Result<Ret, Trap> + 'static,
154+
where
155+
Args: crate::WasmArgs,
156+
Ret: crate::WasmType,
157+
F: for<'cc> FnMut(CallContext<'cc>, Args) -> core::result::Result<Ret, Trap> + 'static,
154158
{
155159
let runtime = NonNull::new(runtime)
156160
.expect("wasm3 calls imported functions with non-null runtime");
@@ -165,7 +169,7 @@ impl<'rt> Module<'rt> {
165169
Ok(ret) => {
166170
ret.push_on_stack(sp);
167171
ffi::m3Err_none
168-
},
172+
}
169173
Err(trap) => trap.as_ptr(),
170174
};
171175
result as *const cty::c_void
@@ -183,7 +187,7 @@ impl<'rt> Module<'rt> {
183187
function_name_cstr.as_ptr(),
184188
signature.as_ptr(),
185189
Some(trampoline::<Args, Ret, F>),
186-
closure.as_mut().get_unchecked_mut() as *mut F as *const cty::c_void
190+
closure.as_mut().get_unchecked_mut() as *mut F as *const cty::c_void,
187191
)
188192
};
189193
Error::from_ffi_res(result)?;
@@ -226,17 +230,14 @@ impl<'rt> Module<'rt> {
226230

227231
impl<'rt> Module<'rt> {
228232
pub(crate) fn from_raw(rt: &'rt Runtime, raw: ffi::IM3Module) -> Self {
229-
Module {
230-
raw,
231-
rt,
232-
}
233+
Module { raw, rt }
233234
}
234235
}
235236

236237
fn function_signature<Args, Ret>() -> Vec<cty::c_char>
237-
where
238-
Args: crate::WasmArgs,
239-
Ret: crate::WasmType,
238+
where
239+
Args: crate::WasmArgs,
240+
Ret: crate::WasmType,
240241
{
241242
let mut signature = <Vec<cty::c_char>>::new();
242243
signature.push(Ret::SIGNATURE as cty::c_char);
@@ -250,8 +251,8 @@ fn function_signature<Args, Ret>() -> Vec<cty::c_char>
250251
#[cfg(test)]
251252
mod tests {
252253
use super::*;
253-
use crate::make_func_wrapper;
254254
use crate::error::TrappedResult;
255+
use crate::make_func_wrapper;
255256

256257
make_func_wrapper!(mul_u32_and_f32_wrap: mul_u32_and_f32(a: u32, b: f32) -> f64);
257258
fn mul_u32_and_f32(a: u32, b: f32) -> f64 {
@@ -270,11 +271,11 @@ mod tests {
270271
fn module_parse() {
271272
let env = Environment::new().expect("env alloc failure");
272273
let fib32 = [
273-
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01,
274-
0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x66, 0x69, 0x62, 0x00, 0x00, 0x0a,
275-
0x1f, 0x01, 0x1d, 0x00, 0x20, 0x00, 0x41, 0x02, 0x49, 0x04, 0x40, 0x20, 0x00, 0x0f, 0x0b,
276-
0x20, 0x00, 0x41, 0x02, 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x10, 0x00, 0x6a,
277-
0x0f, 0x0b,
274+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f,
275+
0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x66, 0x69, 0x62, 0x00,
276+
0x00, 0x0a, 0x1f, 0x01, 0x1d, 0x00, 0x20, 0x00, 0x41, 0x02, 0x49, 0x04, 0x40, 0x20,
277+
0x00, 0x0f, 0x0b, 0x20, 0x00, 0x41, 0x02, 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01,
278+
0x6b, 0x10, 0x00, 0x6a, 0x0f, 0x0b,
278279
];
279280
let _ = Module::parse(&env, &fib32[..]).unwrap();
280281
}
@@ -284,20 +285,32 @@ mod tests {
284285
let env = Environment::new().expect("env alloc failure");
285286
let runtime = Runtime::new(&env, STACK_SIZE).expect("runtime init failure");
286287
let mut module = runtime.parse_and_load_module(TEST_BIN).unwrap();
287-
module.link_function::<(u32, f32), f64>("env", "mul_u32_and_f32", mul_u32_and_f32_wrap)
288+
module
289+
.link_function::<(u32, f32), f64>("env", "mul_u32_and_f32", mul_u32_and_f32_wrap)
290+
.unwrap();
291+
module
292+
.link_function::<(), ()>("env", "hello", hello_wrap)
288293
.unwrap();
289-
module.link_function::<(), ()>("env", "hello", hello_wrap).unwrap();
290294
}
291295

292296
#[test]
293297
fn test_link_closures() {
294298
let env = Environment::new().expect("env alloc failure");
295299
let runtime = Runtime::new(&env, STACK_SIZE).expect("runtime init failure");
296300
let mut module = runtime.parse_and_load_module(TEST_BIN).unwrap();
297-
module.link_closure("env", "mul_u32_and_f32", |_ctx, args: (u32, f32)| -> TrappedResult<f64> {
298-
Ok(mul_u32_and_f32(args.0, args.1))
299-
}).unwrap();
300-
module.link_closure("env", "hello", |_ctx, _args: ()| -> TrappedResult<()> { hello() })
301+
module
302+
.link_closure(
303+
"env",
304+
"mul_u32_and_f32",
305+
|_ctx, args: (u32, f32)| -> TrappedResult<f64> {
306+
Ok(mul_u32_and_f32(args.0, args.1))
307+
},
308+
)
309+
.unwrap();
310+
module
311+
.link_closure("env", "hello", |_ctx, _args: ()| -> TrappedResult<()> {
312+
hello()
313+
})
301314
.unwrap();
302315
}
303316
}

src/runtime.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::environment::Environment;
88
use crate::error::{Error, Result};
99
use crate::function::Function;
1010
use crate::module::{Module, ParsedModule};
11-
use crate::utils::{str_to_cstr_owned};
11+
use crate::utils::str_to_cstr_owned;
1212

1313
type PinnedAnyClosure = Pin<Box<dyn core::any::Any + 'static>>;
1414

@@ -75,9 +75,9 @@ impl Runtime {
7575
///
7676
/// [`Module::find_function`]: ../module/struct.Module.html#method.find_function
7777
pub fn find_function<ARGS, RET>(&self, name: &str) -> Result<Function<ARGS, RET>>
78-
where
79-
ARGS: crate::WasmArgs,
80-
RET: crate::WasmType,
78+
where
79+
ARGS: crate::WasmArgs,
80+
RET: crate::WasmType,
8181
{
8282
let mut func_raw: ffi::IM3Function = core::ptr::null_mut();
8383
let func_name_cstr = str_to_cstr_owned(name);

src/ty.rs

+48-33
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub trait WasmArgs {
3434
// required for closure linking
3535
unsafe fn pop_from_stack(stack: *mut u64) -> Self;
3636
#[doc(hidden)]
37-
fn validate_types(types: impl Iterator<Item=ffi::M3ValueType::Type>) -> bool;
37+
fn validate_types(types: impl Iterator<Item = ffi::M3ValueType::Type>) -> bool;
3838
#[doc(hidden)]
3939
fn sealed_() -> private::Seal;
4040
#[doc(hidden)]
@@ -198,7 +198,7 @@ impl WasmArgs for () {
198198
#[doc(hidden)]
199199
unsafe fn pop_from_stack(_: *mut u64) -> Self {}
200200
#[doc(hidden)]
201-
fn validate_types(mut types: impl Iterator<Item=ffi::M3ValueType::Type>) -> bool {
201+
fn validate_types(mut types: impl Iterator<Item = ffi::M3ValueType::Type>) -> bool {
202202
types.next().is_none()
203203
}
204204
#[doc(hidden)]
@@ -227,9 +227,8 @@ where
227227
WasmType::pop_from_stack(stack)
228228
}
229229
#[doc(hidden)]
230-
fn validate_types(mut types: impl Iterator<Item=ffi::M3ValueType::Type>) -> bool {
231-
types.next().map(|ty| ty == T::TYPE_INDEX).unwrap_or(false) &&
232-
types.next().is_none()
230+
fn validate_types(mut types: impl Iterator<Item = ffi::M3ValueType::Type>) -> bool {
231+
types.next().map(|ty| ty == T::TYPE_INDEX).unwrap_or(false) && types.next().is_none()
233232
}
234233
#[doc(hidden)]
235234
fn sealed_() -> private::Seal {
@@ -309,53 +308,69 @@ mod tests {
309308
use super::*;
310309
#[test]
311310
fn test_validate_types_single() {
312-
assert!(f64::validate_types([
313-
ffi::M3ValueType::c_m3Type_f64,
314-
].iter().cloned()));
311+
assert!(f64::validate_types(
312+
[ffi::M3ValueType::c_m3Type_f64,].iter().cloned()
313+
));
315314
}
316315

317316
#[test]
318317
fn test_validate_types_single_fail() {
319-
assert!(!f32::validate_types([
320-
ffi::M3ValueType::c_m3Type_f64,
321-
].iter().cloned()));
318+
assert!(!f32::validate_types(
319+
[ffi::M3ValueType::c_m3Type_f64,].iter().cloned()
320+
));
322321
}
323322

324323
#[test]
325324
fn test_validate_types_double() {
326-
assert!(<(f64, u32)>::validate_types([
327-
ffi::M3ValueType::c_m3Type_f64,
328-
ffi::M3ValueType::c_m3Type_i32,
329-
].iter().cloned()));
325+
assert!(<(f64, u32)>::validate_types(
326+
[
327+
ffi::M3ValueType::c_m3Type_f64,
328+
ffi::M3ValueType::c_m3Type_i32,
329+
]
330+
.iter()
331+
.cloned()
332+
));
330333
}
331334

332335
#[test]
333336
fn test_validate_types_double_fail() {
334-
assert!(!<(f32, u64)>::validate_types([
335-
ffi::M3ValueType::c_m3Type_i64,
336-
ffi::M3ValueType::c_m3Type_f32,
337-
].iter().cloned()));
337+
assert!(!<(f32, u64)>::validate_types(
338+
[
339+
ffi::M3ValueType::c_m3Type_i64,
340+
ffi::M3ValueType::c_m3Type_f32,
341+
]
342+
.iter()
343+
.cloned()
344+
));
338345
}
339346

340347
#[test]
341348
fn test_validate_types_quintuple() {
342-
assert!(<(f64, u32, i32, i64, f32)>::validate_types([
343-
ffi::M3ValueType::c_m3Type_f64,
344-
ffi::M3ValueType::c_m3Type_i32,
345-
ffi::M3ValueType::c_m3Type_i32,
346-
ffi::M3ValueType::c_m3Type_i64,
347-
ffi::M3ValueType::c_m3Type_f32,
348-
].iter().cloned()));
349+
assert!(<(f64, u32, i32, i64, f32)>::validate_types(
350+
[
351+
ffi::M3ValueType::c_m3Type_f64,
352+
ffi::M3ValueType::c_m3Type_i32,
353+
ffi::M3ValueType::c_m3Type_i32,
354+
ffi::M3ValueType::c_m3Type_i64,
355+
ffi::M3ValueType::c_m3Type_f32,
356+
]
357+
.iter()
358+
.cloned()
359+
));
349360
}
350361

351362
#[test]
352363
fn test_validate_types_quintuple_fail() {
353-
assert!(!<(f64, u32, i32, i64, f32)>::validate_types([
354-
ffi::M3ValueType::c_m3Type_i32,
355-
ffi::M3ValueType::c_m3Type_i64,
356-
ffi::M3ValueType::c_m3Type_i32,
357-
ffi::M3ValueType::c_m3Type_f32,
358-
ffi::M3ValueType::c_m3Type_f64,
359-
].iter().cloned()));
364+
assert!(!<(f64, u32, i32, i64, f32)>::validate_types(
365+
[
366+
ffi::M3ValueType::c_m3Type_i32,
367+
ffi::M3ValueType::c_m3Type_i64,
368+
ffi::M3ValueType::c_m3Type_i32,
369+
ffi::M3ValueType::c_m3Type_f32,
370+
ffi::M3ValueType::c_m3Type_f64,
371+
]
372+
.iter()
373+
.cloned()
374+
));
360375
}
361376
}

0 commit comments

Comments
 (0)