1
0
mirror of https://github.com/fluencelabs/wasmer synced 2025-04-02 16:01:03 +00:00

feat(interface-types) Use VecDeque to remove unsafe code.

This commit is contained in:
Ivan Enderlin 2020-04-06 07:55:56 +02:00
parent 4faf827dc9
commit c2b1b87f9a
2 changed files with 7 additions and 25 deletions
lib/interface-types/src
interpreter/instructions
lib.rs

@ -8,7 +8,7 @@ use crate::{
Instruction, Instruction,
}, },
}; };
use std::mem::{transmute, MaybeUninit}; use std::collections::VecDeque;
/// Build a `InterfaceValue::Record` based on values on the stack. /// Build a `InterfaceValue::Record` based on values on the stack.
/// ///
@ -52,37 +52,21 @@ use std::mem::{transmute, MaybeUninit};
/// ///
/// This latter approach allows to allocate one and final vector to /// This latter approach allows to allocate one and final vector to
/// hold all the record values. /// hold all the record values.
#[allow(unsafe_code)]
fn record_lift_( fn record_lift_(
stack: &mut Stack<InterfaceValue>, stack: &mut Stack<InterfaceValue>,
record_type: &RecordType, record_type: &RecordType,
) -> Result<InterfaceValue, InstructionErrorKind> { ) -> Result<InterfaceValue, InstructionErrorKind> {
let length = record_type.fields.len(); let length = record_type.fields.len();
let mut values = { let mut values = VecDeque::with_capacity(length);
// Initialize a vector of length `length` with `MaybeUninit`
// values.
let mut v = Vec::with_capacity(length);
for _ in 0..length {
v.push(MaybeUninit::<InterfaceValue>::uninit());
}
v
};
let max = length - 1;
// Iterate over fields in reverse order to match the stack `pop` // Iterate over fields in reverse order to match the stack `pop`
// order. // order.
for (nth, field) in record_type.fields.iter().rev().enumerate() { for field in record_type.fields.iter().rev() {
match field { match field {
// The record type tells a record is expected. // The record type tells a record is expected.
InterfaceType::Record(record_type) => { InterfaceType::Record(record_type) => {
// Build it recursively. // Build it recursively.
let value = record_lift_(stack, &record_type)?; values.push_front(record_lift_(stack, &record_type)?)
unsafe {
values[max - nth].as_mut_ptr().write(value);
}
} }
// Any other type. // Any other type.
ty => { ty => {
@ -96,14 +80,12 @@ fn record_lift_(
}); });
} }
unsafe { values.push_front(value)
values[max - nth].as_mut_ptr().write(value);
}
} }
} }
} }
Ok(InterfaceValue::Record(unsafe { transmute(values) })) Ok(InterfaceValue::Record(values.into_iter().collect()))
} }
executable_instruction!( executable_instruction!(

@ -41,12 +41,12 @@
missing_docs, missing_docs,
nonstandard_style, nonstandard_style,
unreachable_patterns, unreachable_patterns,
unsafe_code,
unused_imports, unused_imports,
unused_mut, unused_mut,
unused_unsafe, unused_unsafe,
unused_variables unused_variables
)] )]
#![forbid(unsafe_code)]
#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")] #![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
#![doc(html_logo_url = "https://github.com/wasmerio.png")] #![doc(html_logo_url = "https://github.com/wasmerio.png")]