Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Thunderdome Changelog

## Unreleased Changes
* Added `Arena::map` for converting an arena to another, with different value type, preserving indexing.

## 0.3.0 (2020-10-16)
* Added `Arena::invalidate` for invalidating indices on-demand, as a faster remove-followed-by-reinsert.
Expand Down
23 changes: 23 additions & 0 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,29 @@ impl<T> Arena<T> {
slot: 0,
}
}

/// Returns an arena with identical indexing and function `f` applied to each value.
pub fn map<F, U>(mut self, mut f: F) -> Arena<U>
where
F: FnMut(T) -> U,
{
use Entry::*;
Arena {
storage: self
.storage
.drain(..)
.map(|entry| match entry {
Occupied(OccupiedEntry { generation, value }) => Occupied(OccupiedEntry {
generation,
value: f(value),
}),
Empty(empty) => Empty(empty),
})
.collect(),
len: self.len,
first_free: self.first_free,
}
}
}

/// Methods exposed only within the crate.
Expand Down