1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
//! Extension traits for `std::Vec`. /// Extension trait with useful methods for [`std::vec::Vec`]. /// /// [`std::vec::Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html pub trait VecExt<T> { /// Resizes the `Vec` in-place if the provided `new_len` is **greater** than /// the current `Vec` length. /// /// In simple words, this method only make vector bigger, but not smaller. /// Calling this method with a length smaller or equal to the current length will /// do nothing. /// /// This method uses a closure to create new values on every push. If /// you'd rather [`Clone`] a given value, use [`resize_up`]. If you want /// to use the [`Default`] trait to generate values, you can pass /// [`Default::default()`] as the second argument. /// /// # Examples /// /// ``` /// use stdext::prelude::*; /// /// let mut vec = vec![1, 2, 3]; /// vec.resize_up_with(5, Default::default); /// assert_eq!(vec, [1, 2, 3, 0, 0]); /// /// let mut vec = vec![]; /// let mut p = 1; /// vec.resize_up_with(4, || { p *= 2; p }); /// assert_eq!(vec, [2, 4, 8, 16]); /// /// let mut vec = vec![1, 2, 3]; /// vec.resize_up_with(1, Default::default); /// assert_eq!(vec, [1, 2, 3]); // Resizing to smaller size does nothing. /// ``` /// /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html /// [`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html /// [`Default::default()`]: https://doc.rust-lang.org/std/default/trait.Default.html#tymethod.default /// [`resize_up`]: ./trait.VecExtClone.html#tymethod.resize_up fn resize_up_with<F>(&mut self, new_len: usize, f: F) where F: FnMut() -> T; } /// Extension trait with useful methods for [`std::vec::Vec`]. /// /// This trait contains functions that require `T` to implement `Clone` trait. /// /// [`std::vec::Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html pub trait VecExtClone<T: Clone> { /// Resizes the `Vec` in-place if the provided `new_len` is **greater** than /// the current `Vec` length. /// /// In simple words, this method only make vector bigger, but not smaller. /// Calling this method with a length smaller or equal to the current length will /// do nothing. /// /// This method requires `T` to implement [`Clone`], /// in order to be able to clone the passed value. /// If you need more flexibility (or want to rely on [`Default`] instead of /// [`Clone`]), use [`resize_up_with`]. /// /// # Examples /// /// ``` /// use stdext::prelude::*; /// /// let mut vec = vec!["hello"]; /// vec.resize_up(3, "world"); /// assert_eq!(vec, ["hello", "world", "world"]); /// /// let mut vec = vec![1, 2, 3, 4]; /// vec.resize_up(2, 0); /// assert_eq!(vec, [1, 2, 3, 4]); // Resizing to smaller size does nothing. /// ``` /// /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html /// [`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html /// [`resize_up_with`]: ./trait.VecExt.html#tymethod.resize_up_with fn resize_up(&mut self, new_len: usize, value: T); } impl<T> VecExt<T> for Vec<T> { fn resize_up_with<F>(&mut self, new_len: usize, f: F) where F: FnMut() -> T, { if self.len() < new_len { self.resize_with(new_len, f); } } } impl<T: Clone> VecExtClone<T> for Vec<T> { fn resize_up(&mut self, new_len: usize, value: T) { if self.len() < new_len { self.resize(new_len, value); } } }