Skip to content
Snippets Groups Projects
Commit 525fc8eb authored by Oliver Tale-Yazdi's avatar Oliver Tale-Yazdi Committed by GitHub
Browse files

Implement `Deref` for `BoundedSlice` (#11660)


* Impl Deref for BoundedSlice

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

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

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

Co-authored-by: default avatarBastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: default avatarKeith Yeung <kungfukeith11@gmail.com>
parent d4b89c6f
Branches
No related merge requests found
......@@ -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];
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment