diff options
Diffstat (limited to 'vendor/thin-vec/src/lib.rs')
-rw-r--r-- | vendor/thin-vec/src/lib.rs | 75 |
1 files changed, 68 insertions, 7 deletions
diff --git a/vendor/thin-vec/src/lib.rs b/vendor/thin-vec/src/lib.rs index 60785150e..a2384c62e 100644 --- a/vendor/thin-vec/src/lib.rs +++ b/vendor/thin-vec/src/lib.rs @@ -403,7 +403,9 @@ unsafe impl<T: Send> Send for ThinVec<T> {} /// Creates a `ThinVec` containing the arguments. /// -/// ``` +// A hack to avoid linking problems with `cargo test --features=gecko-ffi`. +#[cfg_attr(not(feature = "gecko-ffi"), doc = "```")] +#[cfg_attr(feature = "gecko-ffi", doc = "```ignore")] /// #[macro_use] extern crate thin_vec; /// /// fn main() { @@ -771,7 +773,9 @@ impl<T> ThinVec<T> { /// /// # Examples /// - /// ``` + // A hack to avoid linking problems with `cargo test --features=gecko-ffi`. + #[cfg_attr(not(feature = "gecko-ffi"), doc = "```")] + #[cfg_attr(feature = "gecko-ffi", doc = "```ignore")] /// # #[macro_use] extern crate thin_vec; /// # fn main() { /// let mut vec = thin_vec![1, 2, 3, 4]; @@ -783,13 +787,41 @@ impl<T> ThinVec<T> { where F: FnMut(&T) -> bool, { + self.retain_mut(|x| f(&*x)); + } + + /// Retains only the elements specified by the predicate, passing a mutable reference to it. + /// + /// In other words, remove all elements `e` such that `f(&mut e)` returns `false`. + /// This method operates in place and preserves the order of the retained + /// elements. + /// + /// # Examples + /// + // A hack to avoid linking problems with `cargo test --features=gecko-ffi`. + #[cfg_attr(not(feature = "gecko-ffi"), doc = "```")] + #[cfg_attr(feature = "gecko-ffi", doc = "```ignore")] + /// # #[macro_use] extern crate thin_vec; + /// # fn main() { + /// let mut vec = thin_vec![1, 2, 3, 4, 5]; + /// vec.retain_mut(|x| { + /// *x += 1; + /// (*x)%2 == 0 + /// }); + /// assert_eq!(vec, [2, 4, 6]); + /// # } + /// ``` + pub fn retain_mut<F>(&mut self, mut f: F) + where + F: FnMut(&mut T) -> bool, + { let len = self.len(); let mut del = 0; { let v = &mut self[..]; for i in 0..len { - if !f(&v[i]) { + if !f(&mut v[i]) { del += 1; } else if del > 0 { v.swap(i - del, i); @@ -807,7 +839,9 @@ impl<T> ThinVec<T> { /// /// # Examples /// - /// ``` + // A hack to avoid linking problems with `cargo test --features=gecko-ffi`. + #[cfg_attr(not(feature = "gecko-ffi"), doc = "```")] + #[cfg_attr(feature = "gecko-ffi", doc = "```ignore")] /// # #[macro_use] extern crate thin_vec; /// # fn main() { /// let mut vec = thin_vec![10, 20, 21, 30, 20]; @@ -835,7 +869,9 @@ impl<T> ThinVec<T> { /// /// # Examples /// - /// ``` + // A hack to avoid linking problems with `cargo test --features=gecko-ffi`. + #[cfg_attr(not(feature = "gecko-ffi"), doc = "```")] + #[cfg_attr(feature = "gecko-ffi", doc = "```ignore")] /// # #[macro_use] extern crate thin_vec; /// # fn main() { /// let mut vec = thin_vec!["foo", "bar", "Bar", "baz", "bar"]; @@ -1014,7 +1050,9 @@ impl<T: Clone> ThinVec<T> { /// /// # Examples /// - /// ``` + // A hack to avoid linking problems with `cargo test --features=gecko-ffi`. + #[cfg_attr(not(feature = "gecko-ffi"), doc = "```")] + #[cfg_attr(feature = "gecko-ffi", doc = "```ignore")] /// # #[macro_use] extern crate thin_vec; /// # fn main() { /// let mut vec = thin_vec!["hello"]; @@ -1056,7 +1094,9 @@ impl<T: PartialEq> ThinVec<T> { /// /// # Examples /// - /// ``` + // A hack to avoid linking problems with `cargo test --features=gecko-ffi`. + #[cfg_attr(not(feature = "gecko-ffi"), doc = "```")] + #[cfg_attr(feature = "gecko-ffi", doc = "```ignore")] /// # #[macro_use] extern crate thin_vec; /// # fn main() { /// let mut vec = thin_vec![1, 2, 2, 3, 2]; @@ -1814,6 +1854,15 @@ mod tests { { let mut v = ThinVec::<i32>::new(); + v.retain_mut(|_| unreachable!()); + + assert_eq!(v.len(), 0); + assert_eq!(v.capacity(), 0); + assert_eq!(&v[..], &[]); + } + + { + let mut v = ThinVec::<i32>::new(); v.dedup_by_key(|x| *x); assert_eq!(v.len(), 0); @@ -2090,6 +2139,18 @@ mod std_tests { } #[test] + fn test_retain_mut() { + let mut vec = thin_vec![9, 9, 9, 9]; + let mut i = 0; + vec.retain_mut(|x| { + i += 1; + *x = i; + i != 4 + }); + assert_eq!(vec, [1, 2, 3]); + } + + #[test] fn test_dedup() { fn case(a: ThinVec<i32>, b: ThinVec<i32>) { let mut v = a; |