about summary refs log tree commit diff
path: root/library/alloc/src/vec/extract_if.rs
AgeCommit message (Collapse)AuthorLines
2025-09-22avoid violating `slice::from_raw_parts` safety contract in `Vec::extract_if`Petros Angelatos-25/+39
The implementation of the `Vec::extract_if` iterator violates the safety contract adverized by `slice::from_raw_parts` by always constructing a mutable slice for the entire length of the vector even though that span of memory can contain holes from items already drained. The safety contract of `slice::from_raw_parts` requires that all elements must be properly initialized. As an example we can look at the following code: ```rust let mut v = vec![Box::new(0u64), Box::new(1u64)]; for item in v.extract_if(.., |x| **x == 0) { drop(item); } ``` In the second iteration a `&mut [Box<u64>]` slice of length 2 will be constructed. The first slot of the slice contains the bitpattern of an already deallocated box, which is invalid. This fixes the issue by only creating references to valid items and using pointer manipulation for the rest. I have also taken the liberty to remove the big `unsafe` blocks in place of targetted ones with a SAFETY comment. The approach closely mirrors the implementation of `Vec::retain_mut`. Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
2025-05-05Consistent trait bounds for ExtractIf Debug implsDavid Tolnay-2/+13
2025-04-09replace version placeholderBoxy-3/+3
2025-02-23stabilize extract_ifbendn-5/+3
2024-12-16remove obsolete comment and pub(super) visibilityThe 8472-12/+6
2024-12-16remove bounds from vec and linkedlist ExtractIfThe 8472-12/+5
since drain-on-drop behavior was removed those bounds no longer serve a purpose
2024-12-16Add a range argument to vec.extract_ifThe 8472-5/+18
2024-07-29Reformat `use` declarations.Nicholas Nethercote-3/+2
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2023-06-15remove unused fieldThe 8472-8/+0
since DrainFilter no longer continues draining when it's dropped the panic tracking is no longer needed.
2023-06-14s/drain_filter/extract_if/ for Vec, Btree{Map,Set} and LinkedListThe 8472-0/+121