Created
November 23, 2016 20:34
-
-
Save MortimerGoro/18c5a9bf0dd080ba1749def59781bdf6 to your computer and use it in GitHub Desktop.
TypedArrays example for blogpost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Creates a typed JS array from a Rust slice | |
pub fn slice_to_array_buffer_view<T>(cx: *mut JSContext, data: &[T]) -> *mut JSObject | |
where T: ArrayBufferViewContents | |
{ | |
unsafe { | |
let js_object = T::new(cx, data.len() as u32); | |
assert!(!js_object.is_null()); | |
update_array_buffer_view(js_object, data); | |
js_object | |
} | |
} | |
/// Updates a typed JS array from a Rust slice | |
pub unsafe fn update_array_buffer_view<T>(obj: *mut JSObject, data: &[T]) | |
where T: ArrayBufferViewContents | |
{ | |
let mut buffer = array_buffer_view_data(obj); | |
if let Some(ref mut buffer) = buffer { | |
ptr::copy_nonoverlapping(&data[0], &mut buffer[0], data.len()) | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment