From 6a20ef708b68f08e3f881616de2160d5e1a47984 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Mon, 13 Nov 2023 19:29:42 +0100 Subject: [PATCH 1/2] Add BoundedVec::try_rotate_{left,right} Signed-off-by: Oliver Tale-Yazdi --- bounded-collections/src/bounded_vec.rs | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/bounded-collections/src/bounded_vec.rs b/bounded-collections/src/bounded_vec.rs index 3d426995..c8c7e87a 100644 --- a/bounded-collections/src/bounded_vec.rs +++ b/bounded-collections/src/bounded_vec.rs @@ -638,6 +638,26 @@ impl> BoundedVec { Err(element) } } + + /// Exactly the same semantics as [`Vec::rotate_left`], but returns an `Err` (and is a noop) if `mid` is larger then the current length. + pub fn try_rotate_left(&mut self, mid: usize) -> Result<(), ()> { + if mid > self.len() { + return Err(()) + } + + self.0.rotate_left(mid); + Ok(()) + } + + /// Exactly the same semantics as [`Vec::rotate_right`], but returns an `Err` (and is a noop) if `mid` is larger then the current length. + pub fn try_rotate_right(&mut self, mid: usize) -> Result<(), ()> { + if mid > self.len() { + return Err(()) + } + + self.0.rotate_right(mid); + Ok(()) + } } impl BoundedVec { @@ -1297,6 +1317,44 @@ mod test { assert!(bound == &unbound[..]); } + #[test] + fn bounded_vec_try_rotate_left_works() { + let o = BoundedVec::>::truncate_from(vec![1, 2, 3]); + let mut bound = o.clone(); + + bound.try_rotate_left(0).unwrap(); + assert_eq!(bound, o); + bound.try_rotate_left(3).unwrap(); + assert_eq!(bound, o); + + bound.try_rotate_left(4).unwrap_err(); + assert_eq!(bound, o); + + bound.try_rotate_left(1).unwrap(); + assert_eq!(bound, vec![2, 3, 1]); + bound.try_rotate_left(2).unwrap(); + assert_eq!(bound, o); + } + + #[test] + fn bounded_vec_try_rotate_right_works() { + let o = BoundedVec::>::truncate_from(vec![1, 2, 3]); + let mut bound = o.clone(); + + bound.try_rotate_right(0).unwrap(); + assert_eq!(bound, o); + bound.try_rotate_right(3).unwrap(); + assert_eq!(bound, o); + + bound.try_rotate_right(4).unwrap_err(); + assert_eq!(bound, o); + + bound.try_rotate_right(1).unwrap(); + assert_eq!(bound, vec![3, 1, 2]); + bound.try_rotate_right(2).unwrap(); + assert_eq!(bound, o); + } + // Just a test that structs containing `BoundedVec` and `BoundedSlice` can derive `Hash`. (This was broken when // they were deriving `Hash`). #[test] From 5624324d8eca9473641b2ec6da8887c915605c78 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Mon, 13 Nov 2023 19:32:13 +0100 Subject: [PATCH 2/2] Update changelog Signed-off-by: Oliver Tale-Yazdi --- bounded-collections/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bounded-collections/CHANGELOG.md b/bounded-collections/CHANGELOG.md index a76dffed..744f41c6 100644 --- a/bounded-collections/CHANGELOG.md +++ b/bounded-collections/CHANGELOG.md @@ -4,6 +4,9 @@ The format is based on [Keep a Changelog]. [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ +## [0.2.0] - 2023-11-13 +- Added `try_rotate_left` and `try_rotate_right` to `BoundedVec`. [#800](https://github.com/paritytech/parity-common/pull/800) + ## [0.1.9] - 2023-10-10 - Added `serde` support for `BoundedBTreeSet`. [#781](https://github.com/paritytech/parity-common/pull/781)