Skip to content

[breaking change] Update retain to use &mut instead of & #61

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

Merged
merged 1 commit into from
Nov 15, 2017
Merged
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
8 changes: 4 additions & 4 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,11 @@ impl<A: Array> SmallVec<A> {
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
/// This method operates in place and preserves the order of the retained
/// elements.
pub fn retain<F: FnMut(&A::Item) -> bool>(&mut self, mut f: F) {
pub fn retain<F: FnMut(&mut A::Item) -> bool>(&mut self, mut f: F) {
let mut del = 0;
let len = self.len;
for i in 0..len {
if !f(&self[i]) {
if !f(&mut self[i]) {
del += 1;
} else if del > 0 {
self.swap(i - del, i);
Expand Down Expand Up @@ -1615,15 +1615,15 @@ pub mod tests {
fn test_retain() {
// Test inline data storate
let mut sv: SmallVec<[i32; 5]> = SmallVec::from_slice(&[1, 2, 3, 3, 4]);
sv.retain(|&i| i != 3);
sv.retain(|&mut i| i != 3);
assert_eq!(sv.pop(), Some(4));
assert_eq!(sv.pop(), Some(2));
assert_eq!(sv.pop(), Some(1));
assert_eq!(sv.pop(), None);

// Test spilled data storage
let mut sv: SmallVec<[i32; 3]> = SmallVec::from_slice(&[1, 2, 3, 3, 4]);
sv.retain(|&i| i != 3);
sv.retain(|&mut i| i != 3);
assert_eq!(sv.pop(), Some(4));
assert_eq!(sv.pop(), Some(2));
assert_eq!(sv.pop(), Some(1));
Expand Down