Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new_mapped method #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,51 @@ impl<T> Slab<T> {
len: old_len,
}
}

/// Construct a new slab with a new allocation by mapping each entry of the
/// slab through a function.
///
/// This preserves not only the locations of entries but also the order of
/// the vacant list, which `slab.iter().map(f).collect()` would not.
///
/// # Examples
///
/// ```
/// # use slab::*;
/// let mut slab1 = Slab::new();
///
/// let k1 = slab1.insert(0);
/// let k2 = slab1.insert(1);
/// let k3 = slab1.insert(2);
///
/// slab1.remove(k2);
///
/// let mut slab2 = slab1.new_mapped(|_k, n| n.to_string());
///
/// let k4 = slab1.insert(3);
/// assert_eq!(k4, slab2.insert(3.to_string()));
///
/// assert_eq!(2, slab1.remove(k3));
/// assert_eq!("2", slab2.remove(k3));
/// ```
pub fn new_mapped<T2, F>(&self, mut f: F) -> Slab<T2>
where
F: FnMut(usize, &T) -> T2,
{
Slab {
entries: self
.entries
.iter()
.enumerate()
.map(|(key, entry)| match entry {
&Entry::Vacant(next) => Entry::Vacant(next),
&Entry::Occupied(ref t) => Entry::Occupied(f(key, t)),
})
.collect(),
next: self.next,
len: self.len,
}
}
}

impl<T> ops::Index<usize> for Slab<T> {
Expand Down