From 525fc8ebc39f3f7ac5adcb0862ca54716ec5b575 Mon Sep 17 00:00:00 2001
From: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Date: Tue, 14 Jun 2022 11:42:52 +0200
Subject: [PATCH] Implement `Deref` for `BoundedSlice` (#11660)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Impl Deref for BoundedSlice

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update primitives/runtime/src/bounded/bounded_vec.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
---
 .../runtime/src/bounded/bounded_vec.rs        | 22 +++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/substrate/primitives/runtime/src/bounded/bounded_vec.rs b/substrate/primitives/runtime/src/bounded/bounded_vec.rs
index 555a34819a4..c9c9f851d32 100644
--- a/substrate/primitives/runtime/src/bounded/bounded_vec.rs
+++ b/substrate/primitives/runtime/src/bounded/bounded_vec.rs
@@ -223,6 +223,15 @@ impl<'a, T, S> Clone for BoundedSlice<'a, T, S> {
 // Since a reference `&T` is always `Copy`, so is `BoundedSlice<'a, T, S>`.
 impl<'a, T, S> Copy for BoundedSlice<'a, T, S> {}
 
+// will allow for all immutable operations of `[T]` on `BoundedSlice<T>`.
+impl<'a, T, S> Deref for BoundedSlice<'a, T, S> {
+	type Target = [T];
+
+	fn deref(&self) -> &Self::Target {
+		self.0
+	}
+}
+
 impl<'a, T, S> sp_std::iter::IntoIterator for BoundedSlice<'a, T, S> {
 	type Item = &'a T;
 	type IntoIter = sp_std::slice::Iter<'a, T>;
@@ -647,7 +656,7 @@ impl<T, S> AsMut<[T]> for BoundedVec<T, S> {
 	}
 }
 
-// will allow for immutable all operations of `Vec<T>` on `BoundedVec<T>`.
+// will allow for all immutable operations of `Vec<T>` on `BoundedVec<T>`.
 impl<T, S> Deref for BoundedVec<T, S> {
 	type Target = Vec<T>;
 
@@ -970,7 +979,7 @@ pub mod test {
 	}
 
 	#[test]
-	fn deref_coercion_works() {
+	fn deref_vec_coercion_works() {
 		let bounded: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3];
 		// these methods come from deref-ed vec.
 		assert_eq!(bounded.len(), 3);
@@ -978,6 +987,15 @@ pub mod test {
 		assert!(!bounded.is_empty());
 	}
 
+	#[test]
+	fn deref_slice_coercion_works() {
+		let bounded = BoundedSlice::<u32, ConstU32<7>>::try_from(&[1, 2, 3][..]).unwrap();
+		// these methods come from deref-ed slice.
+		assert_eq!(bounded.len(), 3);
+		assert!(bounded.iter().next().is_some());
+		assert!(!bounded.is_empty());
+	}
+
 	#[test]
 	fn try_mutate_works() {
 		let bounded: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3, 4, 5, 6];
-- 
GitLab