Fix Rust Simple and Rust String examples

This commit is contained in:
Constantine Solovev
2018-07-25 10:21:38 +04:00
parent c04a3c4a9b
commit dd72c7124c
7 changed files with 21 additions and 20 deletions

View File

@@ -9,9 +9,3 @@ Compile Rust to WASM and then to the JVM. In order of complexity:
* [rust-simple](rust-simple)
* [rust-string](rust-string)
* [rust-regex](rust-regex)
### C/C++
Compile C to WASM and then to the JVM. In order of complexity:
* [c-simple](c-simple)

View File

@@ -1,6 +1,7 @@
#![feature(allocator_api)]
use std::heap::{Alloc, Heap, Layout};
use std::ptr::NonNull;
use std::alloc::{Alloc, Global, Layout};
use std::ffi::{CString};
use std::mem;
use std::os::raw::c_char;
@@ -30,17 +31,17 @@ pub extern "C" fn prepend_from_rust(ptr: *mut u8, len: usize) -> *const c_char {
}
#[no_mangle]
pub extern "C" fn alloc(size: usize) -> *mut u8 {
pub extern "C" fn alloc(size: usize) -> NonNull<u8> {
unsafe {
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
Heap.alloc(layout).unwrap()
Global.alloc(layout).unwrap()
}
}
#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut u8, size: usize) {
pub extern "C" fn dealloc(ptr: NonNull<u8>, size: usize) {
unsafe {
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
Heap.dealloc(ptr, layout);
Global.dealloc(ptr, layout);
}
}