@@ -5,8 +5,14 @@ use crate::{objects::*, types::*, values::wasm_val_t};
55/// An element owned by a C API vector.
66pub trait Element : Default {
77 /// Copies the element and any owned handle.
8+ ///
9+ /// # Safety
10+ /// The element and any handle it contains must be initialized and live.
811 unsafe fn copy ( & self ) -> Self ;
912 /// Releases any owned handle.
13+ ///
14+ /// # Safety
15+ /// The element must own its handle and must not have been destroyed already.
1016 unsafe fn destroy ( & mut self ) ;
1117}
1218
@@ -51,19 +57,27 @@ impl<T: Element> Vector<T> {
5157 }
5258
5359 /// Borrows initialized elements. Zero-length vectors may have null data.
60+ ///
61+ /// # Safety
62+ /// For nonempty vectors, `data` must point to `size` live, initialized
63+ /// elements that are not mutated for the duration of the borrow.
5464 pub ( crate ) unsafe fn as_slice ( & self ) -> & [ T ] {
5565 if self . size == 0 { & [ ] } else { unsafe { slice:: from_raw_parts ( self . data , self . size ) } }
5666 }
5767
5868 /// Borrows initialized elements exclusively.
69+ ///
70+ /// # Safety
71+ /// For nonempty vectors, `data` must point to `size` live, initialized
72+ /// elements exclusively accessible for the duration of the borrow.
5973 pub ( crate ) unsafe fn as_mut_slice ( & mut self ) -> & mut [ T ] {
6074 if self . size == 0 { & mut [ ] } else { unsafe { slice:: from_raw_parts_mut ( self . data , self . size ) } }
6175 }
6276}
6377
6478impl < T : Element > Clone for Vector < T > {
6579 fn clone ( & self ) -> Self {
66- // SAFETY: Rust-owned vectors always contain initialized, live elements.
80+ // SAFETY: the source vector must contain initialized, live elements.
6781 unsafe { Self :: from_vec ( self . as_slice ( ) . iter ( ) . map ( |value| value. copy ( ) ) . collect ( ) ) }
6882 }
6983}
0 commit comments