File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -4987,6 +4987,22 @@ macro_rules! arrays {
4987
4987
self . raw_copy_to( dst) ;
4988
4988
}
4989
4989
4990
+ /// Copy the contents of the source Rust slice into this
4991
+ /// JS typed array.
4992
+ ///
4993
+ /// This function will efficiently copy the memory from within
4994
+ /// the wasm module's own linear memory to this typed array.
4995
+ ///
4996
+ /// # Panics
4997
+ ///
4998
+ /// This function will panic if this typed array's length is
4999
+ /// different than the length of the provided `src` array.
5000
+ pub fn copy_from( & self , src: & [ $ty] ) {
5001
+ assert_eq!( self . length( ) as usize , src. len( ) ) ;
5002
+ // This is safe because the `set` function copies from its TypedArray argument
5003
+ unsafe { self . set( & $name:: view( src) , 0 ) }
5004
+ }
5005
+
4990
5006
/// Efficiently copies the contents of this JS typed array into a new Vec.
4991
5007
pub fn to_vec( & self ) -> Vec <$ty> {
4992
5008
let mut output = vec![ $ty:: default ( ) ; self . length( ) as usize ] ;
Original file line number Diff line number Diff line change @@ -145,6 +145,16 @@ fn copy_to() {
145
145
}
146
146
}
147
147
148
+ #[ wasm_bindgen_test]
149
+ fn copy_from ( ) {
150
+ let x = [ 1 , 2 , 3 ] ;
151
+ let array = Int32Array :: new ( & 3 . into ( ) ) ;
152
+ array. copy_from ( & x) ;
153
+ array. for_each ( & mut |x, i, _| {
154
+ assert_eq ! ( x, ( i + 1 ) as i32 ) ;
155
+ } ) ;
156
+ }
157
+
148
158
#[ wasm_bindgen_test]
149
159
fn to_vec ( ) {
150
160
let array = Int32Array :: new ( & 10 . into ( ) ) ;
You can’t perform that action at this time.
0 commit comments