diff --git a/CHANGELOG.md b/CHANGELOG.md index 354d8fb..c338694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.15.0 +### Added +- `AnyVec` implements `Extend` now. +- `AnyVecTyped` implements `Extend` now. +- `AnyVec::append()` added. +- `AnyVecTyped::append()` added. + ## 0.14.0 ### Added - Now library `no_std` friendly. diff --git a/src/any_value/wrapper.rs b/src/any_value/wrapper.rs index d76694d..9dcc3d5 100644 --- a/src/any_value/wrapper.rs +++ b/src/any_value/wrapper.rs @@ -8,6 +8,7 @@ use crate::any_value::{AnyValue, AnyValueMut, AnyValueTypelessMut, AnyValueTypel /// special in any way. /// /// [AnyValueRaw]: super::AnyValueRaw +#[derive(Debug)] pub struct AnyValueWrapper{ value: T } @@ -17,7 +18,6 @@ impl AnyValueWrapper { Self{ value } } } - impl AnyValueSizeless for AnyValueWrapper { type Type = T; diff --git a/src/any_vec.rs b/src/any_vec.rs index 9aaecfc..b7c414c 100644 --- a/src/any_vec.rs +++ b/src/any_vec.rs @@ -831,6 +831,25 @@ impl Clone for AnyVec Extend for AnyVec +where + Traits: ?Sized + Trait, + M: MemBuilder, + A: AnyValue, +{ + /// # Panics + /// + /// * Panics if type mismatch. + /// * Panics if out of memory. + fn extend>(&mut self, iter: T) { + let iter = iter.into_iter(); + self.raw.reserve(iter.size_hint().0); + for v in iter { + self.push(v); + } + } +} + impl Debug for AnyVec{ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("AnyVec") @@ -930,4 +949,10 @@ impl<'a, T: 'static + Debug, M: MemBuilder + 'a> Debug for AnyVecMut<'a, T, M>{ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { self.0.fmt(f) } +} +impl<'a, T: 'static, M: MemBuilder + 'a> Extend for AnyVecMut<'a, T, M>{ + #[inline] + fn extend>(&mut self, iter: I) { + self.0.extend(iter) + } } \ No newline at end of file diff --git a/src/any_vec_typed.rs b/src/any_vec_typed.rs index 1d81957..d78dd84 100644 --- a/src/any_vec_typed.rs +++ b/src/any_vec_typed.rs @@ -302,6 +302,18 @@ impl<'a, T: 'static + Debug, M: MemBuilder> Debug for AnyVecTyped<'a, T, M>{ } } +impl<'a, T: 'static, M: MemBuilder> Extend for AnyVecTyped<'a, T, M>{ + fn extend>(&mut self, iter: I) { + let iter = iter.into_iter(); + self.this_mut().reserve(iter.size_hint().0); + // We can't prove that iterator will return actual len in size_hint(), + // so we have to push items as usual. + for v in iter { + self.push(v); + } + } +} + // Do not implement Index, since we can't do the same for AnyVec /* impl<'a, T: 'static, I: SliceIndex<[T]>> Index for AnyVecTyped<'a, T> { diff --git a/tests/any_vec.rs b/tests/any_vec.rs index 411d754..a98d98a 100644 --- a/tests/any_vec.rs +++ b/tests/any_vec.rs @@ -342,6 +342,25 @@ fn reserve_exact_test(){ assert_eq!(any_vec.capacity(), 6); } +#[test] +fn extend_test(){ + let mut any_vec: AnyVec = AnyVec::new::(); + any_vec.extend([ + AnyValueWrapper::new(String::from("0")), + AnyValueWrapper::new(String::from("1")), + AnyValueWrapper::new(String::from("2")), + ]); + + assert_equal( + any_vec.iter().map(|v|v.downcast_ref::().unwrap()), + &[ + String::from("0"), + String::from("1"), + String::from("2"), + ] + ); +} + #[test] fn shrink_to_fit_test(){ let mut any_vec: AnyVec = AnyVec::new::(); diff --git a/tests/any_vec_typed.rs b/tests/any_vec_typed.rs index 3bd5758..3298a64 100644 --- a/tests/any_vec_typed.rs +++ b/tests/any_vec_typed.rs @@ -225,6 +225,27 @@ fn append_test() { assert!(any_vec2.is_empty()); } +#[test] +fn extend_test(){ + let mut any_vec: AnyVec = AnyVec::new::(); + let mut vec = any_vec.downcast_mut::().unwrap(); + vec.extend([ + String::from("0"), + String::from("1"), + String::from("2"), + ]); + + assert_equal( + vec, + &[ + String::from("0"), + String::from("1"), + String::from("2"), + ] + ); +} + + /* #[test] fn any_vec_index_test() {