MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / push

Method push

crates/environ/src/scopevec.rs:31–59  ·  view source on GitHub ↗

Transfers ownership of `data` into this scope and then yields the slice back to the caller. The original data will be deallocated when `self` is dropped.

(&self, data: Vec<T>)

Source from the content-addressed store, hash-verified

29 ///
30 /// The original data will be deallocated when `self` is dropped.
31 pub fn push(&self, data: Vec<T>) -> &mut [T] {
32 let data: Box<[T]> = data.into();
33 let len = data.len();
34
35 let mut storage = self.data.borrow_mut();
36 storage.push(data);
37 let ptr = storage.last_mut().unwrap().as_mut_ptr();
38
39 // This should be safe for a few reasons:
40 //
41 // * The returned pointer on the heap that `data` owns. Despite moving
42 // `data` around it doesn't actually move the slice itself around, so
43 // the pointer returned should be valid (and length).
44 //
45 // * The lifetime of the returned pointer is connected to the lifetime
46 // of `self`. This reflects how when `self` is destroyed the `data` is
47 // destroyed as well, or otherwise the returned slice will be valid
48 // for as long as `self` is valid since `self` owns the original data
49 // at that point.
50 //
51 // * This function was given ownership of `data` so it should be safe to
52 // hand back a mutable reference. Once placed within a `ScopeVec` the
53 // data is never mutated so the caller will enjoy exclusive access to
54 // the slice of the original vec.
55 //
56 // This all means that it should be safe to return a mutable slice of
57 // all of `data` after the data has been pushed onto our internal list.
58 unsafe { core::slice::from_raw_parts_mut(ptr, len) }
59 }
60
61 /// Iterate over items in this `ScopeVec`, consuming ownership.
62 pub fn into_iter(self) -> impl ExactSizeIterator<Item = Box<[T]>> {

Callers 15

index.jsFile · 0.45
annotate_clifFunction · 0.45
push_tagMethod · 0.45
push_functionMethod · 0.45
from_wasmparserMethod · 0.45
importMethod · 0.45
import_transcoderMethod · 0.45
import_prepare_callMethod · 0.45
translate_helperMethod · 0.45
encodeMethod · 0.45

Calls 4

lenMethod · 0.45
as_mut_ptrMethod · 0.45
unwrapMethod · 0.45
last_mutMethod · 0.45

Tested by 1

smokeFunction · 0.36