1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
use std::fmt; /// A wrapper type for exposing raw Rust slices as `TypedArray`s /// at zero cost without copying. /// /// The only thing you can do with this is to pass it to the `js!` macro. /// /// Using this is **highly unsafe**! After you pass it to the `js!` macro /// you **must** use it **before** triggering any Rust code whatsoever, /// either directly or indirectly. Breaking this rule will result /// in undefined behavior! /// /// # Examples /// /// ```rust /// let slice: &[u8] = &[1, 2, 3]; /// let slice = unsafe { UnsafeTypedArray::new( slice ) }; /// js!( /// var slice = @{slice}; /// // `slice` is an Uint8Array /// var sum = slice[0] + slice[1] + slice[2]; /// console.log( "Sum of array elements: ", sum ); /// ); /// ``` pub struct UnsafeTypedArray< 'a, T: 'a >( pub(crate) &'a [T] ); impl< 'a, T > fmt::Debug for UnsafeTypedArray< 'a, T > { #[inline] fn fmt( &self, formatter: &mut fmt::Formatter ) -> Result< (), fmt::Error > { write!( formatter, "UnsafeTypedArray" ) } } impl< 'a, T > UnsafeTypedArray< 'a, T > { /// Creates a new `UnsafeTypedArray`. /// /// Even though this function is marked as `unsafe` /// the unsafely only comes into play after you /// pass it to the `js!` macro. #[inline] pub unsafe fn new( slice: &'a [T] ) -> Self { UnsafeTypedArray( slice ) } }