cargo fmt

This commit is contained in:
Bo Yao
2021-04-21 15:42:47 -07:00
parent 2eac442646
commit 24bcc9349e
224 changed files with 2778 additions and 7749 deletions

View File

@@ -435,9 +435,7 @@ pub extern "C" fn wasm_engine_new_with_config(
where
M: ToString,
{
update_last_error(CApiError {
msg: msg.to_string(),
});
update_last_error(CApiError { msg: msg.to_string() });
return None;
}

View File

@@ -18,10 +18,7 @@ pub struct wasm_func_t {
impl wasm_func_t {
pub(crate) fn new(function: Function) -> Self {
Self {
tag: CApiExternTag::Function,
inner: Box::new(function),
}
Self { tag: CApiExternTag::Function, inner: Box::new(function) }
}
}
@@ -171,10 +168,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
let function = Function::new_with_env(
&store.inner,
func_sig,
WrapperEnv {
env,
env_finalizer: Arc::new(env_finalizer),
},
WrapperEnv { env, env_finalizer: Arc::new(env_finalizer) },
trampoline,
);

View File

@@ -16,10 +16,7 @@ pub struct wasm_global_t {
impl wasm_global_t {
pub(crate) fn new(global: Global) -> Self {
Self {
tag: CApiExternTag::Global,
inner: Box::new(global),
}
Self { tag: CApiExternTag::Global, inner: Box::new(global) }
}
}

View File

@@ -14,10 +14,7 @@ pub struct wasm_memory_t {
impl wasm_memory_t {
pub(crate) fn new(memory: Memory) -> Self {
Self {
tag: CApiExternTag::Memory,
inner: Box::new(memory),
}
Self { tag: CApiExternTag::Memory, inner: Box::new(memory) }
}
}

View File

@@ -99,25 +99,17 @@ impl Clone for wasm_extern_t {
fn clone(&self) -> Self {
match self.get_tag() {
CApiExternTag::Function => Self {
inner: wasm_extern_inner {
function: unsafe { self.inner.function.clone() },
},
},
CApiExternTag::Memory => Self {
inner: wasm_extern_inner {
memory: unsafe { self.inner.memory.clone() },
},
},
CApiExternTag::Global => Self {
inner: wasm_extern_inner {
global: unsafe { self.inner.global.clone() },
},
},
CApiExternTag::Table => Self {
inner: wasm_extern_inner {
table: unsafe { self.inner.table.clone() },
},
inner: wasm_extern_inner { function: unsafe { self.inner.function.clone() } },
},
CApiExternTag::Memory => {
Self { inner: wasm_extern_inner { memory: unsafe { self.inner.memory.clone() } } }
}
CApiExternTag::Global => {
Self { inner: wasm_extern_inner { global: unsafe { self.inner.global.clone() } } }
}
CApiExternTag::Table => {
Self { inner: wasm_extern_inner { table: unsafe { self.inner.table.clone() } } }
}
}
}
}

View File

@@ -13,10 +13,7 @@ pub struct wasm_table_t {
impl wasm_table_t {
pub(crate) fn new(table: Table) -> Self {
Self {
tag: CApiExternTag::Table,
inner: Box::new(table),
}
Self { tag: CApiExternTag::Table, inner: Box::new(table) }
}
}

View File

@@ -39,9 +39,7 @@ pub unsafe extern "C" fn wasm_module_new(
let bytes = bytes.into_slice()?;
let module = c_try!(Module::from_binary(&store.inner, bytes));
Some(Box::new(wasm_module_t {
inner: Arc::new(module),
}))
Some(Box::new(wasm_module_t { inner: Arc::new(module) }))
}
/// Deletes a WebAssembly module.
@@ -488,11 +486,7 @@ pub unsafe extern "C" fn wasm_module_deserialize(
let module = c_try!(Module::deserialize(&store.inner, byte_slice));
Some(NonNull::new_unchecked(Box::into_raw(Box::new(
wasm_module_t {
inner: Arc::new(module),
},
))))
Some(NonNull::new_unchecked(Box::into_raw(Box::new(wasm_module_t { inner: Arc::new(module) }))))
}
/// Serializes a module into a binary representation that the

View File

@@ -16,10 +16,7 @@ pub extern "C" fn wasm_exporttype_new(
extern_type: Option<Box<wasm_externtype_t>>,
) -> Option<Box<wasm_exporttype_t>> {
let name = unsafe { owned_wasm_name_t::new(name?) };
Some(Box::new(wasm_exporttype_t {
name,
extern_type: extern_type?,
}))
Some(Box::new(wasm_exporttype_t { name, extern_type: extern_type? }))
}
#[no_mangle]

View File

@@ -13,11 +13,7 @@ impl WasmFunctionType {
let params: Box<wasm_valtype_vec_t> = Box::new(function_type.params().into());
let results: Box<wasm_valtype_vec_t> = Box::new(function_type.results().into());
Self {
function_type,
params,
results,
}
Self { function_type, params, results }
}
}
@@ -36,9 +32,7 @@ pub struct wasm_functype_t {
impl wasm_functype_t {
pub(crate) fn new(function_type: FunctionType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Function(function_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Function(function_type)) }
}
pub(crate) fn inner(&self) -> &WasmFunctionType {
@@ -61,24 +55,15 @@ pub unsafe extern "C" fn wasm_functype_new(
let params = params?;
let results = results?;
let params_as_valtype: Vec<ValType> = params
.into_slice()?
.into_iter()
.map(|val| val.as_ref().into())
.collect::<Vec<_>>();
let results_as_valtype: Vec<ValType> = results
.into_slice()?
.iter()
.map(|val| val.as_ref().into())
.collect::<Vec<_>>();
let params_as_valtype: Vec<ValType> =
params.into_slice()?.into_iter().map(|val| val.as_ref().into()).collect::<Vec<_>>();
let results_as_valtype: Vec<ValType> =
results.into_slice()?.iter().map(|val| val.as_ref().into()).collect::<Vec<_>>();
wasm_valtype_vec_delete(Some(params));
wasm_valtype_vec_delete(Some(results));
Some(Box::new(wasm_functype_t::new(FunctionType::new(
params_as_valtype,
results_as_valtype,
))))
Some(Box::new(wasm_functype_t::new(FunctionType::new(params_as_valtype, results_as_valtype))))
}
#[no_mangle]

View File

@@ -15,10 +15,7 @@ impl WasmGlobalType {
pub(crate) fn new(global_type: GlobalType) -> Self {
let content = Box::new(global_type.ty.into());
Self {
global_type,
content,
}
Self { global_type, content }
}
}
@@ -31,9 +28,7 @@ pub struct wasm_globaltype_t {
impl wasm_globaltype_t {
pub(crate) fn new(global_type: GlobalType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Global(global_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Global(global_type)) }
}
pub(crate) fn inner(&self) -> &WasmGlobalType {
@@ -55,10 +50,8 @@ pub unsafe extern "C" fn wasm_globaltype_new(
) -> Option<Box<wasm_globaltype_t>> {
let valtype = valtype?;
let mutability: wasm_mutability_enum = mutability.try_into().ok()?;
let global_type = Box::new(wasm_globaltype_t::new(GlobalType::new(
(*valtype).into(),
mutability.into(),
)));
let global_type =
Box::new(wasm_globaltype_t::new(GlobalType::new((*valtype).into(), mutability.into())));
wasm_valtype_delete(Some(valtype));

View File

@@ -18,17 +18,9 @@ pub extern "C" fn wasm_importtype_new(
name: Option<&wasm_name_t>,
extern_type: Option<Box<wasm_externtype_t>>,
) -> Option<Box<wasm_importtype_t>> {
let (module, name) = unsafe {
(
owned_wasm_name_t::new(module?),
owned_wasm_name_t::new(name?),
)
};
Some(Box::new(wasm_importtype_t {
name,
module,
extern_type: extern_type?,
}))
let (module, name) =
unsafe { (owned_wasm_name_t::new(module?), owned_wasm_name_t::new(name?)) };
Some(Box::new(wasm_importtype_t { name, module, extern_type: extern_type? }))
}
#[no_mangle]
@@ -61,10 +53,6 @@ impl From<&ImportType> for wasm_importtype_t {
let name: owned_wasm_name_t = other.name().to_string().into();
let extern_type: Box<wasm_externtype_t> = Box::new(other.ty().into());
wasm_importtype_t {
module,
name,
extern_type,
}
wasm_importtype_t { module, name, extern_type }
}
}

View File

@@ -11,16 +11,10 @@ impl WasmMemoryType {
pub(crate) fn new(memory_type: MemoryType) -> Self {
let limits = Box::new(wasm_limits_t {
min: memory_type.minimum.0 as _,
max: memory_type
.maximum
.map(|max| max.0 as _)
.unwrap_or(LIMITS_MAX_SENTINEL),
max: memory_type.maximum.map(|max| max.0 as _).unwrap_or(LIMITS_MAX_SENTINEL),
});
Self {
memory_type,
limits,
}
Self { memory_type, limits }
}
}
@@ -33,9 +27,7 @@ pub struct wasm_memorytype_t {
impl wasm_memorytype_t {
pub(crate) fn new(memory_type: MemoryType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Memory(memory_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Memory(memory_type)) }
}
pub(crate) fn inner(&self) -> &WasmMemoryType {
@@ -53,15 +45,10 @@ wasm_declare_boxed_vec!(memorytype);
#[no_mangle]
pub unsafe extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm_memorytype_t> {
let min_pages = Pages(limits.min as _);
let max_pages = if limits.max == LIMITS_MAX_SENTINEL {
None
} else {
Some(Pages(limits.max as _))
};
let max_pages =
if limits.max == LIMITS_MAX_SENTINEL { None } else { Some(Pages(limits.max as _)) };
Box::new(wasm_memorytype_t::new(MemoryType::new(
min_pages, max_pages, false,
)))
Box::new(wasm_memorytype_t::new(MemoryType::new(min_pages, max_pages, false)))
}
#[no_mangle]

View File

@@ -50,10 +50,7 @@ impl owned_wasm_name_t {
/// You must ensure that the data pointed to by `wasm_name_t` is valid and
/// that it is not owned by anyone else.
pub unsafe fn new(name: &wasm_name_t) -> Self {
Self(wasm_name_t {
size: name.size,
data: name.data,
})
Self(wasm_name_t { size: name.size, data: name.data })
}
}

View File

@@ -23,11 +23,7 @@ impl WasmTableType {
});
let content = Box::new(table_type.ty.into());
Self {
table_type,
limits,
content,
}
Self { table_type, limits, content }
}
}
@@ -40,9 +36,7 @@ pub struct wasm_tabletype_t {
impl wasm_tabletype_t {
pub(crate) fn new(table_type: TableType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Table(table_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Table(table_type)) }
}
pub(crate) fn inner(&self) -> &WasmTableType {
@@ -61,11 +55,7 @@ pub unsafe extern "C" fn wasm_tabletype_new(
limits: &wasm_limits_t,
) -> Option<Box<wasm_tabletype_t>> {
let valtype = valtype?;
let max_elements = if limits.max == LIMITS_MAX_SENTINEL {
None
} else {
Some(limits.max as _)
};
let max_elements = if limits.max == LIMITS_MAX_SENTINEL { None } else { Some(limits.max as _) };
let table_type = Box::new(wasm_tabletype_t::new(TableType::new(
(*valtype).into(),
limits.min as _,

View File

@@ -50,9 +50,7 @@ pub struct wasm_valtype_t {
impl Default for wasm_valtype_t {
fn default() -> Self {
Self {
valkind: wasm_valkind_enum::WASM_I32,
}
Self { valkind: wasm_valkind_enum::WASM_I32 }
}
}
@@ -72,9 +70,7 @@ impl From<&wasm_valtype_t> for ValType {
impl From<ValType> for wasm_valtype_t {
fn from(other: ValType) -> Self {
Self {
valkind: other.into(),
}
Self { valkind: other.into() }
}
}
@@ -91,7 +87,5 @@ pub unsafe extern "C" fn wasm_valtype_delete(_valtype: Option<Box<wasm_valtype_t
#[no_mangle]
pub unsafe extern "C" fn wasm_valtype_kind(valtype: Option<&wasm_valtype_t>) -> wasm_valkind_t {
valtype
.expect("`wasm_valtype_kind: argument is a null pointer")
.valkind as wasm_valkind_t
valtype.expect("`wasm_valtype_kind: argument is a null pointer").valkind as wasm_valkind_t
}

View File

@@ -133,10 +133,7 @@ mod tests {
#[test]
fn test_wasmer_is_headless() {
set_var(
"COMPILER",
if cfg!(feature = "compiler") { "0" } else { "1" },
);
set_var("COMPILER", if cfg!(feature = "compiler") { "0" } else { "1" });
(assert_c! {
#include "tests/wasmer_wasm.h"
@@ -155,23 +152,9 @@ mod tests {
#[test]
fn test_wasmer_is_compiler_available() {
set_var(
"CRANELIFT",
if cfg!(feature = "cranelift") {
"1"
} else {
"0"
},
);
set_var("CRANELIFT", if cfg!(feature = "cranelift") { "1" } else { "0" });
set_var("LLVM", if cfg!(feature = "llvm") { "1" } else { "0" });
set_var(
"SINGLEPASS",
if cfg!(feature = "singlepass") {
"1"
} else {
"0"
},
);
set_var("SINGLEPASS", if cfg!(feature = "singlepass") { "1" } else { "0" });
(assert_c! {
#include "tests/wasmer_wasm.h"
@@ -196,14 +179,7 @@ mod tests {
fn test_wasmer_is_engine_available() {
set_var("JIT", if cfg!(feature = "jit") { "1" } else { "0" });
set_var("NATIVE", if cfg!(feature = "native") { "1" } else { "0" });
set_var(
"OBJECT_FILE",
if cfg!(feature = "object-file") {
"1"
} else {
"0"
},
);
set_var("OBJECT_FILE", if cfg!(feature = "object-file") { "1" } else { "0" });
(assert_c! {
#include "tests/wasmer_wasm.h"

View File

@@ -55,9 +55,7 @@ pub struct wasmer_features_t {
/// See the module's documentation.
#[no_mangle]
pub extern "C" fn wasmer_features_new() -> Box<wasmer_features_t> {
Box::new(wasmer_features_t {
inner: Features::new(),
})
Box::new(wasmer_features_t { inner: Features::new() })
}
/// Delete a [`wasmer_features_t`].

View File

@@ -215,10 +215,7 @@ pub extern "C" fn wasmer_metering_get_remaining_points(instance: &wasm_instance_
/// See module's documentation.
#[no_mangle]
pub extern "C" fn wasmer_metering_points_are_exhausted(instance: &wasm_instance_t) -> bool {
matches!(
get_remaining_points(&instance.inner),
MeteringPoints::Exhausted,
)
matches!(get_remaining_points(&instance.inner), MeteringPoints::Exhausted,)
}
/// Set a new amount of points for the given metering middleware.
@@ -313,7 +310,5 @@ pub extern "C" fn wasmer_metering_as_middleware(
) -> Option<Box<wasmer_middleware_t>> {
let metering = metering?;
Some(Box::new(wasmer_middleware_t {
inner: metering.inner,
}))
Some(Box::new(wasmer_middleware_t { inner: metering.inner }))
}

View File

@@ -147,10 +147,7 @@ pub unsafe extern "C" fn wasmer_triple_new(
triple: Option<&wasm_name_t>,
) -> Option<Box<wasmer_triple_t>> {
let triple = triple?;
let triple = c_try!(str::from_utf8(slice::from_raw_parts(
triple.data,
triple.size
)));
let triple = c_try!(str::from_utf8(slice::from_raw_parts(triple.data, triple.size)));
Some(Box::new(wasmer_triple_t {
inner: c_try!(Triple::from_str(triple).map_err(|e| CApiError { msg: e.to_string() })),
@@ -183,9 +180,7 @@ pub unsafe extern "C" fn wasmer_triple_new(
/// See also [`wasmer_triple_new`].
#[no_mangle]
pub extern "C" fn wasmer_triple_new_from_host() -> Box<wasmer_triple_t> {
Box::new(wasmer_triple_t {
inner: Triple::host(),
})
Box::new(wasmer_triple_t { inner: Triple::host() })
}
/// Delete a [`wasmer_triple_t`].
@@ -265,9 +260,7 @@ pub struct wasmer_cpu_features_t {
/// See [`wasmer_cpu_features_t`].
#[no_mangle]
pub extern "C" fn wasmer_cpu_features_new() -> Box<wasmer_cpu_features_t> {
Box::new(wasmer_cpu_features_t {
inner: CpuFeature::set(),
})
Box::new(wasmer_cpu_features_t { inner: CpuFeature::set() })
}
/// Delete a [`wasmer_cpu_features_t`].

View File

@@ -171,11 +171,9 @@ fn wasi_get_unordered_imports_inner(
let store = &store.inner;
let version = c_try!(
get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
})
);
let version = c_try!(get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
}));
let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);

View File

@@ -114,10 +114,7 @@ wasm_declare_vec!(val);
impl Clone for wasm_val_t {
fn clone(&self) -> Self {
wasm_val_t {
kind: self.kind,
of: self.of.clone(),
}
wasm_val_t { kind: self.kind, of: self.of.clone() }
}
}
@@ -130,18 +127,10 @@ pub unsafe extern "C" fn wasm_val_copy(
out.kind = val.kind;
out.of = match val.kind.try_into() {
Ok(kind) => match kind {
wasm_valkind_enum::WASM_I32 => wasm_val_inner {
int32_t: val.of.int32_t,
},
wasm_valkind_enum::WASM_I64 => wasm_val_inner {
int64_t: val.of.int64_t,
},
wasm_valkind_enum::WASM_F32 => wasm_val_inner {
float32_t: val.of.float32_t,
},
wasm_valkind_enum::WASM_F64 => wasm_val_inner {
float64_t: val.of.float64_t,
},
wasm_valkind_enum::WASM_I32 => wasm_val_inner { int32_t: val.of.int32_t },
wasm_valkind_enum::WASM_I64 => wasm_val_inner { int64_t: val.of.int64_t },
wasm_valkind_enum::WASM_F32 => wasm_val_inner { float32_t: val.of.float32_t },
wasm_valkind_enum::WASM_F64 => wasm_val_inner { float64_t: val.of.float64_t },
wasm_valkind_enum::WASM_ANYREF => wasm_val_inner { wref: val.of.wref },
wasm_valkind_enum::WASM_FUNCREF => wasm_val_inner { wref: val.of.wref },
},

View File

@@ -13,9 +13,7 @@ pub struct OutputCapturer {
impl OutputCapturer {
pub fn new() -> Self {
Self {
buffer: VecDeque::new(),
}
Self { buffer: VecDeque::new() }
}
}
@@ -48,36 +46,21 @@ impl WasiFile for OutputCapturer {
// fail when reading or Seeking
impl Read for OutputCapturer {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not read from capturing stdout"))
}
fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> std::io::Result<usize> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(std::io::Error::new(std::io::ErrorKind::Other, "can not read from capturing stdout"))
}
fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not read from capturing stdout"))
}
fn read_exact(&mut self, _buf: &mut [u8]) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not read from capturing stdout"))
}
}
impl Seek for OutputCapturer {
fn seek(&mut self, _pos: io::SeekFrom) -> io::Result<u64> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not seek capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not seek capturing stdout"))
}
}
impl Write for OutputCapturer {

View File

@@ -175,24 +175,18 @@ pub struct wasi_env_t {
#[no_mangle]
pub extern "C" fn wasi_env_new(mut config: Box<wasi_config_t>) -> Option<Box<wasi_env_t>> {
if !config.inherit_stdout {
config
.state_builder
.stdout(Box::new(capture_files::OutputCapturer::new()));
config.state_builder.stdout(Box::new(capture_files::OutputCapturer::new()));
}
if !config.inherit_stderr {
config
.state_builder
.stderr(Box::new(capture_files::OutputCapturer::new()));
config.state_builder.stderr(Box::new(capture_files::OutputCapturer::new()));
}
// TODO: impl capturer for stdin
let wasi_state = c_try!(config.state_builder.build());
Some(Box::new(wasi_env_t {
inner: WasiEnv::new(wasi_state),
}))
Some(Box::new(wasi_env_t { inner: WasiEnv::new(wasi_state) }))
}
/// Delete a [`wasi_env_t`].
@@ -272,10 +266,7 @@ fn read_inner(wasi_file: &mut Box<dyn WasiFile>, inner_buffer: &mut [u8]) -> isi
if let Some(oc) = wasi_file.downcast_mut::<capture_files::OutputCapturer>() {
let total_to_read = min(inner_buffer.len(), oc.buffer.len());
for (address, value) in inner_buffer
.iter_mut()
.zip(oc.buffer.drain(..total_to_read))
{
for (address, value) in inner_buffer.iter_mut().zip(oc.buffer.drain(..total_to_read)) {
*address = value;
}
@@ -367,11 +358,9 @@ fn wasi_get_imports_inner(
let store = &store.inner;
let version = c_try!(
get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
})
);
let version = c_try!(get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
}));
let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);