You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/vec/avec.rs
+161Lines changed: 161 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -35,14 +35,37 @@ impl<T, A: Free> AVec<T, A> {
35
35
/// data between <code>[avec](Self).[len](Self::len)\(\) .. [avec](Self).[capacity](Self::capacity)\(\)</code>, which can be used to initialize elements in-place before calling <code>[avec](Self).[set_len](Self::set_len)\(\)</code>.
/// * initializing new elements if grown (UB hazard!)
55
+
/// * reallocating if the length specified is larger than capacity (UB hazard!)
56
+
/// * [`Drop`]ing old elements if shrunk (leak hazard!)
57
+
///
58
+
/// ### Safety
59
+
/// * `new_len` must be less than or equal to <code>[capacity](Self::capacity)()</code>.
60
+
/// * If <code>new_len > [avec](Self).[len](Self::len)()</code>, the elements between <code>[avec](Self).[len](Self::len)() .. new_len</code> must have been initialized.
for value in slice.iter().cloned(){unsafe{self.push_within_capacity_unchecked(value)}}
64
94
Ok(())
65
95
}
66
96
97
+
/// Append all elements from `slice` to `self` by [`Clone`]ing.
98
+
///
99
+
/// Panics without [`Clone`]ing anything if (re)allocating the vector fails.
67
100
#[cfg(global_oom_handling)]pubfnextend_from_slice(&mutself,slice:&[T])whereT:Clone,A:Realloc{self.try_extend_from_slice(slice).expect("out of memory")}
68
101
102
+
/// [`Clone`] elements from within `self` and append them to `self`.
103
+
///
104
+
/// Panics without [`Clone`]ing anything if (re)allocating the vector fails.
@@ -89,10 +125,26 @@ impl<T, A: Free> AVec<T, A> {
89
125
}
90
126
}
91
127
128
+
/// Construct an [`AVec`] from it's raw pointer, length, capacity, and default allocator.
129
+
///
130
+
/// You *generally* only want to do this when you previously broke down a vector of the same type with <code>[into_raw_parts](Self::into_raw_parts)</code>.
131
+
///
132
+
/// ### Safety
133
+
/// * `(data, capacity, align_of::<T>())` should exactly describe an allocation belonging to allocator `A`.
134
+
/// * `0 .. length` should be initialized elements of type `T`.
135
+
/// * [`AVec`] takes exclusive ownership of said allocation.
/// Construct an [`AVec`] from it's raw pointer, length, capacity, and allocator.
141
+
///
142
+
/// You *generally* only want to do this when you previously broke down a vector of the same type with <code>[into_raw_parts_with_allocator](Self::into_raw_parts_with_allocator)</code>.
143
+
///
144
+
/// ### Safety
145
+
/// * `(data, capacity, align_of::<T>())` should exactly describe an allocation belonging to `allocator`.
146
+
/// * `0 .. length` should be initialized elements of type `T`.
147
+
/// * [`AVec`] takes exclusive ownership of said allocation.
/// This may allocate more capacity than requested to encourage amortized constant behavior via exponential growth patterns.
274
+
///
275
+
/// Noop if <code>[len](Self::len)() + additional <= [capacity](Self::capacity)()</code>.
276
+
///
277
+
/// Panics if reallocation was necessary but failed.
185
278
#[cfg(global_oom_handling)]pubfnreserve(&mutself,additional:usize)whereA:Realloc{self.try_reserve(additional).expect("unable to reserve more memory")}
279
+
280
+
/// Reserve enough <code>[capacity](Self::capacity)()</code> for *exactly* <code>[len](Self::len)() + additional</code> elements.
281
+
/// Beware: This will avoid exponential growth, which can easily lead to O(N<sup>2</sup>) behavior!
282
+
///
283
+
/// Noop if <code>[len](Self::len)() + additional <= [capacity](Self::capacity)()</code>.
284
+
///
285
+
/// Panics if reallocation was necessary but failed.
186
286
#[cfg(global_oom_handling)]pubfnreserve_exact(&mutself,additional:usize)whereA:Realloc{self.try_reserve_exact(additional).expect("unable to reserve more memory")}
@@ -199,21 +299,49 @@ impl<T, A: Free> AVec<T, A> {
199
299
self.try_resize_with(new_len, || value.clone())
200
300
}
201
301
302
+
/// Resize `self` to be `new_len` elements. `value` will be repeatedly [`Clone`]ed if <code>new_len > [len](Self::len)()</code>, or ignored otherwise.
303
+
///
304
+
/// Panics if reallocation was necessary but failed.
202
305
#[cfg(global_oom_handling)]pubfnresize(&mutself,new_len:usize,value:T)whereT:Clone,A:Realloc{self.try_resize(new_len, value).expect("unable to reserve more memory")}
306
+
307
+
/// Resize `self` to be `new_len` elements. If <code>new_len > [len](Self::len)()</code>, `f()` will be called to create new elements, otherwise `f` is ignored.
308
+
///
309
+
/// Panics if reallocation was necessary but failed.
203
310
#[cfg(global_oom_handling)]pubfnresize_with<F:FnMut() -> T>(&mutself,new_len:usize,f:F)whereA:Realloc{self.try_resize_with(new_len, f).expect("unable to reserve more memory")}
204
311
312
+
/// Remove all elements `e` of `self` where `!f(e)`.
0 commit comments