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>)
| 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]>> { |