retain_mut-0.1.2/.cargo_vcs_info.json0000644000000001121377401174600132450ustar { "git": { "sha1": "64fa0183e8a3e92ac14d8892e0facbf2d6ccbd4e" } } retain_mut-0.1.2/.gitignore010064400017500001750000000000361337746755200140540ustar 00000000000000/target **/*.rs.bk Cargo.lock retain_mut-0.1.2/Cargo.toml0000644000000016201377401174600112500ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "retain_mut" version = "0.1.2" authors = ["Xidorn Quan "] description = "Provide retain_mut method that has the same functionality as retain but gives mutable borrow to the predicate." readme = "README.md" keywords = ["retain", "no_std"] categories = ["rust-patterns"] license = "MIT" repository = "https://github.com/upsuper/retain_mut" [dependencies] retain_mut-0.1.2/Cargo.toml.orig010064400017500001750000000006041377401163600147410ustar 00000000000000[package] name = "retain_mut" version = "0.1.2" authors = ["Xidorn Quan "] description = "Provide retain_mut method that has the same functionality as retain but gives mutable borrow to the predicate." license = "MIT" repository = "https://github.com/upsuper/retain_mut" categories = ["rust-patterns"] keywords = ["retain", "no_std"] readme = "README.md" [dependencies] retain_mut-0.1.2/LICENSE010064400017500001750000000017771337747122300130740ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. retain_mut-0.1.2/README.md010064400017500001750000000017211362632561500133330ustar 00000000000000# RetainMut This crate provides trait `RetainMut` which provides `retain_mut` method for `Vec` and `VecDeque`. `retain_mut` is basically the same as `retain` except that it gives mutable reference of items to the predicate function. Since there is no reason `retain` couldn't have been designed this way, this crate basically just copies the code from std with minor (1-line) change to hand out mutable reference. The code these impls are based on can be found in code comments of this crate. This was probably a historical mistake in Rust library, that `retain` should do this at the very beginning. See [rust-lang/rust#25477](https://github.com/rust-lang/rust/issues/25477). ## Examples ### `Vec` ```rust let mut vec = vec![1, 2, 3, 4]; vec.retain_mut(|x| { *x *= 3; *x % 2 == 0 }); assert_eq!(vec, [6, 12]); ``` ### `VecDeque` ```rust let mut deque = VecDeque::from(vec![1, 2, 3, 4]); deque.retain_mut(|x| { *x *= 3; *x % 2 == 0 }); assert_eq!(deque, [6, 12]); ``` retain_mut-0.1.2/src/lib.rs010064400017500001750000000054211377401104600137520ustar 00000000000000//! This crate provides trait `RetainMut` which //! provides `retain_mut` method for `Vec` and `VecDeque`. //! //! `retain_mut` is basically the same as `retain` except that //! it gives mutable reference of items to the predicate function. //! //! Since there is no reason `retain` couldn't have been designed this way, //! this crate basically just copies the code from std with minor (1-line) change //! to hand out mutable reference. //! The code these impls are based on can be found in code comments of this crate. //! //! # Examples //! //! ## `Vec` //! //! ``` //! # use retain_mut::RetainMut; //! let mut vec = vec![1, 2, 3, 4]; //! vec.retain_mut(|x| { *x *= 3; *x % 2 == 0 }); //! assert_eq!(vec, [6, 12]); //! ``` //! //! ## `VecDeque` //! //! ``` //! # use retain_mut::RetainMut; //! # use std::collections::VecDeque; //! let mut deque = VecDeque::from(vec![1, 2, 3, 4]); //! deque.retain_mut(|x| { *x *= 3; *x % 2 == 0 }); //! assert_eq!(deque, [6, 12]); //! ``` #![no_std] extern crate alloc; use alloc::collections::vec_deque::VecDeque; use alloc::vec::Vec; /// Trait that provides `retain_mut` method. pub trait RetainMut { /// Retains only the elements specified by the predicate. /// /// In other words, remove all elements `e` such that `f(&e)` returns `false`. /// This method operates in place, visiting each element exactly once in the /// original order, and preserves the order of the retained elements. fn retain_mut(&mut self, f: F) where F: FnMut(&mut T) -> bool; } impl RetainMut for Vec { // The implementation is based on // https://github.com/rust-lang/rust/blob/0eb878d2aa6e3a1cb315f3f328681b26bb4bffdb/src/liballoc/vec.rs#L1072-L1093 fn retain_mut(&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(&mut v[i]) { del += 1; } else if del > 0 { v.swap(i - del, i); } } } if del > 0 { self.truncate(len - del); } } } impl RetainMut for VecDeque { // The implementation is based on // https://github.com/rust-lang/rust/blob/0eb878d2aa6e3a1cb315f3f328681b26bb4bffdb/src/liballoc/collections/vec_deque.rs#L1978-L1995 fn retain_mut(&mut self, mut f: F) where F: FnMut(&mut T) -> bool, { let len = self.len(); let mut del = 0; for i in 0..len { if !f(&mut self[i]) { del += 1; } else if del > 0 { self.swap(i - del, i); } } if del > 0 { self.truncate(len - del); } } }