Skip to content

Commit

Permalink
refactor(lib): make Rust module compile-able under stable Rust
Browse files Browse the repository at this point in the history
Implementation based on the Rust standard library implementation
for `vec_into_raw_parts`.
  • Loading branch information
dndx committed Aug 22, 2023
1 parent db6f6d2 commit 72647e7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
21 changes: 12 additions & 9 deletions lib/ngx-wasm-rs/lib/backtrace/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ impl Drop for ngx_wasm_backtrace_name_table_t {

fn vec_into_wasm_byte_vec_t(bv: *mut wasm_byte_vec_t, v: Vec<u8>) {
unsafe {
let (ptr, len, _cap) = v.into_raw_parts();
(*bv).size = len;
(*bv).data = ptr;
// FIXME Update this when vec_into_raw_parts is stabilized
let mut v = mem::ManuallyDrop::new(v);
(*bv).size = v.len();
(*bv).data = v.as_mut_ptr();
}
}

Expand All @@ -63,22 +64,24 @@ pub unsafe extern "C" fn ngx_wasm_backtrace_get_name_table(

let mut names = Vec::<ngx_wasm_backtrace_name_t>::with_capacity(table.len());
for (idx, name) in table.drain(..) {
let (bytes, len, _cap) = name.into_raw_parts();
// FIXME Update this when vec_into_raw_parts is stabilized
let mut name = mem::ManuallyDrop::new(name);
let name_t = ngx_wasm_backtrace_name_t {
idx,
name: Box::new(wasm_byte_vec_t {
size: len,
data: bytes,
size: name.len(),
data: name.as_mut_ptr(),
}),
};

names.push(name_t);
}

let (bytes, len, _cap) = names.into_raw_parts();
// FIXME Update this when vec_into_raw_parts is stabilized
let mut names = mem::ManuallyDrop::new(names);
Some(Box::new(ngx_wasm_backtrace_name_table_t {
size: len,
table: bytes,
size: names.len(),
table: names.as_mut_ptr(),
}))
} else {
None
Expand Down
1 change: 0 additions & 1 deletion lib/ngx-wasm-rs/lib/backtrace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#![feature(vec_into_raw_parts)]
pub(crate) mod backtrace;
pub mod c_api;
8 changes: 4 additions & 4 deletions lib/ngx-wasm-rs/lib/wat/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(vec_into_raw_parts)]
use ngx_wasm_c_api::*;
use regex::Regex;
use std::mem;
Expand Down Expand Up @@ -26,9 +25,10 @@ fn extract_message(err: &str) -> Option<String> {

fn vec_into_wasm_byte_vec_t(bv: *mut wasm_byte_vec_t, v: Vec<u8>) -> () {
unsafe {
let (ptr, len, _cap) = v.into_raw_parts();
(*bv).size = len;
(*bv).data = ptr;
// FIXME Update this when vec_into_raw_parts is stabilized
let mut v = mem::ManuallyDrop::new(v);
(*bv).size = v.len();
(*bv).data = v.as_mut_ptr();
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly
stable
11 changes: 8 additions & 3 deletions util/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ export RUSTFLAGS="$TEST_NGINX_CARGO_RUSTFLAGS"
eval cargo build \
--lib \
"${args[@]}" \
--target wasm32-wasi \
--out-dir $DIR_TESTS_LIB_WASM \
-Z unstable-options
--target wasm32-wasi

if [ "$TEST_NGINX_CARGO_PROFILE" = release ]; then
cp target/wasm32-wasi/release/*.wasm $DIR_TESTS_LIB_WASM

else
cp target/wasm32-wasi/debug/*.wasm $DIR_TESTS_LIB_WASM
fi

if [ $(uname -s) = Linux ]; then
export TEST_NGINX_EVENT_TYPE=epoll
Expand Down

0 comments on commit 72647e7

Please sign in to comment.