I'm writing Rust code for WebAssembly to handle strings from JavaScript land.
Since WebAssembly has no real string type, I'm trying to pass a pointer to WebAssembly memory object which points to UTF-8 encoded string.
#[no_mangle]
pub extern "C" fn check(ptr: *mut u8, length: u32) -> u32 {
unsafe {
let buf: &[u8] = std::slice::from_raw_parts(ptr, length as usize);
// do some operations on buf
0
}
}
It works fine, expect that I have to depend on the std crate, which bloats the final binary to about 600KB.
Is there any way to get rid of std::slice::from_raw_parts but still be able to cast a raw pointer to a slice?
libcoreis the exact same that you use fromlibstd.std::slice::from_raw_parts_mutiscore::slice::from_raw_parts_mut. Switching from one to the other shouldn't change anything. It really sounds like some piece of unused code or data isn't being properly removed. – Shepmaster Jun 21 '18 at 21:52std, I also add "#![no_std]" and its counterparts. Those code made a lot difference on the final binary. – liuyanghejerry Jun 26 '18 at 12:52