Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/any_value/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::any_value::{AnyValue, AnyValueMut, AnyValueTypelessMut, AnyValueTypel
/// special in any way.
///
/// [AnyValueRaw]: super::AnyValueRaw
#[derive(Debug)]
pub struct AnyValueWrapper<T: 'static>{
value: T
}
Expand All @@ -17,7 +18,6 @@ impl<T: 'static> AnyValueWrapper<T> {
Self{ value }
}
}

impl<T: 'static> AnyValueSizeless for AnyValueWrapper<T> {
type Type = T;

Expand Down
25 changes: 25 additions & 0 deletions src/any_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,25 @@ impl<Traits: ?Sized + Cloneable + Trait, M: MemBuilder> Clone for AnyVec<Traits,
}
}

impl<Traits, M, A> Extend<A> for AnyVec<Traits, M>
where
Traits: ?Sized + Trait,
M: MemBuilder,
A: AnyValue,
{
/// # Panics
///
/// * Panics if type mismatch.
/// * Panics if out of memory.
fn extend<T: IntoIterator<Item=A>>(&mut self, iter: T) {
let iter = iter.into_iter();
self.raw.reserve(iter.size_hint().0);
for v in iter {
self.push(v);
}
}
}

impl<Traits: ?Sized + Trait, M: MemBuilder> Debug for AnyVec<Traits, M>{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("AnyVec")
Expand Down Expand Up @@ -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<T> for AnyVecMut<'a, T, M>{
#[inline]
fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
self.0.extend(iter)
}
}
12 changes: 12 additions & 0 deletions src/any_vec_typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ impl<'a, T: 'static + Debug, M: MemBuilder> Debug for AnyVecTyped<'a, T, M>{
}
}

impl<'a, T: 'static, M: MemBuilder> Extend<T> for AnyVecTyped<'a, T, M>{
fn extend<I: IntoIterator<Item=T>>(&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<I> for AnyVecTyped<'a, T> {
Expand Down
19 changes: 19 additions & 0 deletions tests/any_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>();
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::<String>().unwrap()),
&[
String::from("0"),
String::from("1"),
String::from("2"),
]
);
}

#[test]
fn shrink_to_fit_test(){
let mut any_vec: AnyVec = AnyVec::new::<String>();
Expand Down
21 changes: 21 additions & 0 deletions tests/any_vec_typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,27 @@ fn append_test() {
assert!(any_vec2.is_empty());
}

#[test]
fn extend_test(){
let mut any_vec: AnyVec = AnyVec::new::<String>();
let mut vec = any_vec.downcast_mut::<String>().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() {
Expand Down