Skip to content

vec: add extract_if()#665

Open
max-heller wants to merge 2 commits into
rust-embedded:mainfrom
max-heller:vec-extract-if
Open

vec: add extract_if()#665
max-heller wants to merge 2 commits into
rust-embedded:mainfrom
max-heller:vec-extract-if

Conversation

@max-heller

Copy link
Copy Markdown

Adapts alloc::vec::Vec::extract_if() to implement heapless::Vec::extract_if()

I noticed that vec::Drain stores LenT instead of usize for its internal indices. Should we do the same here to save a few bytes? I wasn't sure if it's worth it relative to the (also small) cost of casting back and forth between LenT and usize.

@max-heller max-heller force-pushed the vec-extract-if branch 6 times, most recently from 4824e86 to 6707e51 Compare June 9, 2026 19:06
@hiSandog

Copy link
Copy Markdown

Worth using LenT here for consistency — the existing vec::Drain in the crate already stores its indices as LenT, and since heapless::Vec's own length is bounded by LenT anyway, the retained/removed range indices can never exceed LenT::MAX, so the narrowing is always safe. The LenT as usize casts on indexing aren't a perf concern: extract_if's indices drive slice bounds and drop_in_place bookkeeping, not a hot arithmetic loop, and on most targets the cast compiles to nothing. Main thing to watch is keeping the kept-range start/end and the read/write cursors all on LenT (not mixed), so you don't end up casting in two directions. Keeping the two drain-like types (Drain and ExtractIf) on the same index representation is a nice consistency win too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants