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
2 changes: 1 addition & 1 deletion src/any_value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub trait AnyValueCloneable: AnyValueSizeless {
unsafe fn clone_into(&self, out: *mut u8);

#[inline]
fn lazy_clone(&self) -> LazyClone<Self>
fn lazy_clone(&self) -> LazyClone<'_, Self>
where Self: Sized
{
LazyClone::new(self)
Expand Down
56 changes: 37 additions & 19 deletions src/any_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use core::ops::{Deref, DerefMut, Range, RangeBounds};
use core::ptr::NonNull;
use core::{fmt, ptr, slice};
use core::slice::{from_raw_parts, from_raw_parts_mut};
use crate::{AnyVecTyped, into_range, mem, ops};
use crate::any_value::{AnyValue, AnyValueSizeless};
use crate::{AnyVecTyped, into_range, mem, ops, assert_types_equal};
use crate::any_value::{AnyValue, AnyValueSizeless, Unknown};
use crate::any_vec_raw::{AnyVecRaw, DropFn};
use crate::ops::{TempValue, Remove, SwapRemove, remove, swap_remove, Pop, pop};
use crate::ops::{Drain, Splice, drain, splice};
Expand Down Expand Up @@ -423,7 +423,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
/// Returns [`AnyVecRef`] - typed view to const AnyVec,
/// if container holds elements of type T, or None if it isn’t.
#[inline]
pub fn downcast_ref<T: 'static>(&self) -> Option<AnyVecRef<T, M>> {
pub fn downcast_ref<T: 'static>(&self) -> Option<AnyVecRef<'_, T, M>> {
if self.element_typeid() == TypeId::of::<T>() {
unsafe{ Some(self.downcast_ref_unchecked()) }
} else {
Expand All @@ -438,14 +438,14 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
/// The container elements must be of type `T`.
/// Calling this method with the incorrect type is undefined behavior.
#[inline]
pub unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> AnyVecRef<T, M> {
pub unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> AnyVecRef<'_, T, M> {
AnyVecRef(AnyVecTyped::new(NonNull::from(&self.raw)))
}

/// Returns [`AnyVecMut`] - typed view to mut AnyVec,
/// if container holds elements of type T, or None if it isn’t.
#[inline]
pub fn downcast_mut<T: 'static>(&mut self) -> Option<AnyVecMut<T, M>> {
pub fn downcast_mut<T: 'static>(&mut self) -> Option<AnyVecMut<'_, T, M>> {
if self.element_typeid() == TypeId::of::<T>() {
unsafe{ Some(self.downcast_mut_unchecked()) }
} else {
Expand All @@ -460,7 +460,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
/// The container elements must be of type `T`.
/// Calling this method with the incorrect type is undefined behavior.
#[inline]
pub unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> AnyVecMut<T, M> {
pub unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> AnyVecMut<'_, T, M> {
AnyVecMut(AnyVecTyped::new(NonNull::from(&mut self.raw)))
}

Expand Down Expand Up @@ -489,12 +489,12 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
}

#[inline]
pub fn iter(&self) -> IterRef<Traits, M>{
pub fn iter(&self) -> IterRef<'_, Traits, M>{
Iter::new(AnyVecPtr::from(self), 0, self.len())
}

#[inline]
pub fn iter_mut(&mut self) -> IterMut<Traits, M>{
pub fn iter_mut(&mut self) -> IterMut<'_, Traits, M>{
let len = self.len();
Iter::new(AnyVecPtr::from(self), 0, len)
}
Expand All @@ -505,12 +505,12 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
///
/// * Panics if index is out of bounds.
#[inline]
pub fn at(&self, index: usize) -> ElementRef<Traits, M>{
pub fn at(&self, index: usize) -> ElementRef<'_, Traits, M>{
self.get(index).unwrap()
}

#[inline]
pub fn get(&self, index: usize) -> Option<ElementRef<Traits, M>>{
pub fn get(&self, index: usize) -> Option<ElementRef<'_, Traits, M>>{
if index < self.len(){
Some(unsafe{ self.get_unchecked(index) })
} else {
Expand All @@ -519,7 +519,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
}

#[inline]
pub unsafe fn get_unchecked(&self, index: usize) -> ElementRef<Traits, M>{
pub unsafe fn get_unchecked(&self, index: usize) -> ElementRef<'_, Traits, M>{
let element_ptr = self.raw.get_unchecked(index) as *mut u8;
ElementRef(
ManuallyDrop::new(ElementPointer::new(
Expand All @@ -535,12 +535,12 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
///
/// * Panics if index is out of bounds.
#[inline]
pub fn at_mut(&mut self, index: usize) -> ElementMut<Traits, M>{
pub fn at_mut(&mut self, index: usize) -> ElementMut<'_, Traits, M>{
self.get_mut(index).unwrap()
}

#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<ElementMut<Traits, M>>{
pub fn get_mut(&mut self, index: usize) -> Option<ElementMut<'_, Traits, M>>{
if index < self.len(){
Some(unsafe{ self.get_unchecked_mut(index) })
} else {
Expand All @@ -549,7 +549,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
}

#[inline]
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> ElementMut<Traits, M> {
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> ElementMut<'_, Traits, M> {
let element_ptr = self.raw.get_unchecked_mut(index);
ElementMut(
ManuallyDrop::new(ElementPointer::new(
Expand Down Expand Up @@ -625,7 +625,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
/// [`mem::forget`]: core::mem::forget
///
#[inline]
pub fn pop(&mut self) -> Option<Pop<Traits, M>> {
pub fn pop(&mut self) -> Option<Pop<'_, Traits, M>> {
if self.is_empty(){
None
} else {
Expand All @@ -648,7 +648,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
/// [`mem::forget`]: core::mem::forget
///
#[inline]
pub fn remove(&mut self, index: usize) -> Remove<Traits, M> {
pub fn remove(&mut self, index: usize) -> Remove<'_, Traits, M> {
self.raw.index_check(index);
TempValue::new(remove::Remove::new(
AnyVecPtr::from(self),
Expand All @@ -669,13 +669,31 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
/// [`mem::forget`]: core::mem::forget
///
#[inline]
pub fn swap_remove(&mut self, index: usize) -> SwapRemove<Traits, M> {
pub fn swap_remove(&mut self, index: usize) -> SwapRemove<'_, Traits, M> {
self.raw.index_check(index);
TempValue::new(swap_remove::SwapRemove::new(
AnyVecPtr::from(self),
index
))
}

/// Moves all the elements of `other` into `self`, leaving `other` empty.
///
/// # Panics
///
/// * Panics if types mismatch.
/// * Panics if out of memory.
pub fn append<OtherTraits, OtherM>(
&mut self, other: &mut AnyVec<OtherTraits, OtherM>
) where
OtherTraits: ?Sized + Trait,
OtherM: MemBuilder
{
assert_types_equal(other.element_typeid(), self.element_typeid());
unsafe{
self.raw.append_unchecked::<Unknown, _>(&mut other.raw);
}
}

/// Removes the specified range from the vector in bulk, returning all removed
/// elements as an iterator. If the iterator is dropped before being fully consumed,
Expand All @@ -697,7 +715,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
/// [`mem::forget`]: core::mem::forget
///
#[inline]
pub fn drain(&mut self, range: impl RangeBounds<usize>) -> Drain<Traits, M> {
pub fn drain(&mut self, range: impl RangeBounds<usize>) -> Drain<'_, Traits, M> {
let Range{start, end} = into_range(self.len(), range);
ops::Iter(drain::Drain::new(
AnyVecPtr::from(self),
Expand Down Expand Up @@ -729,7 +747,7 @@ impl<Traits: ?Sized + Trait, M: MemBuilder> AnyVec<Traits, M>
///
#[inline]
pub fn splice<I: IntoIterator>(&mut self, range: impl RangeBounds<usize>, replace_with: I)
-> Splice<Traits, M, I::IntoIter>
-> Splice<'_, Traits, M, I::IntoIter>
where
I::IntoIter: ExactSizeIterator,
I::Item: AnyValue
Expand Down
32 changes: 32 additions & 0 deletions src/any_vec_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,38 @@ impl<M: MemBuilder> AnyVecRaw<M> {

self.len += 1;
}

/// # Safety
///
/// Type is not checked.
pub unsafe fn append_unchecked<T: 'static, OtherM: MemBuilder>(
&mut self, other: &mut AnyVecRaw<OtherM>
) {
self.reserve(other.len);

// copy
unsafe {
if !Unknown::is::<T>(){
ptr::copy_nonoverlapping(
other.mem.as_ptr().cast::<T>(),
self.mem.as_mut_ptr().cast::<T>().add(self.len),
other.len
);
} else {
let element_size = self.element_layout().size();
ptr::copy_nonoverlapping(
other.mem.as_ptr(),
self.mem.as_mut_ptr().add(self.len*element_size),
other.len*element_size
);
}
}

// update len(s)
self.len += other.len;
other.len = 0;

}

#[inline]
pub fn clear(&mut self){
Expand Down
13 changes: 13 additions & 0 deletions src/any_vec_typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ impl<'a, T: 'static, M: MemBuilder + 'a> AnyVecTyped<'a, T, M>{
}
}

/// Moves all the elements of `other` into `self`, leaving `other` empty.
///
/// # Panics
///
/// * Panics if out of memory.
pub fn append<OtherM: MemBuilder>(
&mut self, other: &mut AnyVecTyped<T, OtherM>
) {
unsafe{
self.this_mut().append_unchecked::<T, _>(other.this_mut());
}
}

#[inline]
pub fn drain(&mut self, range: impl RangeBounds<usize>)
-> impl ElementIterator<Item = T> + 'a
Expand Down
27 changes: 27 additions & 0 deletions tests/any_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,33 @@ fn any_vec_splice_test() {
]);
}

#[test]
fn append_test() {
let mut any_vec: AnyVec = AnyVec::new::<String>();
any_vec.push(AnyValueWrapper::new(String::from("0")));
any_vec.push(AnyValueWrapper::new(String::from("1")));
any_vec.push(AnyValueWrapper::new(String::from("2")));
any_vec.push(AnyValueWrapper::new(String::from("3")));
any_vec.push(AnyValueWrapper::new(String::from("4")));

let mut any_vec2: AnyVec = AnyVec::new::<String>();
any_vec2.push(AnyValueWrapper::new(String::from("100")));
any_vec2.push(AnyValueWrapper::new(String::from("200")));

any_vec.append(&mut any_vec2);

assert_equal(any_vec.downcast_ref::<String>().unwrap().as_slice(), &[
String::from("0"),
String::from("1"),
String::from("2"),
String::from("3"),
String::from("4"),
String::from("100"),
String::from("200"),
]);
assert!(any_vec2.is_empty());
}

#[test]
fn any_vec_insert_front(){
let mut any_vec: AnyVec = AnyVec::new::<usize>();
Expand Down
29 changes: 29 additions & 0 deletions tests/any_vec_typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,35 @@ fn any_vec_debug() {
assert_eq!(format!("{vec:?}"), format!("{control_vec:?}"));
}

#[test]
fn append_test() {
let mut any_vec: AnyVec = AnyVec::new::<String>();
let mut vec = any_vec.downcast_mut::<String>().unwrap();
vec.push(String::from("0"));
vec.push(String::from("1"));
vec.push(String::from("2"));
vec.push(String::from("3"));
vec.push(String::from("4"));

let mut any_vec2: AnyVec = AnyVec::new::<String>();
let mut vec2 = any_vec2.downcast_mut::<String>().unwrap();
vec2.push(String::from("100"));
vec2.push(String::from("200"));

vec.append(&mut vec2);

assert_equal(any_vec.downcast_ref::<String>().unwrap().as_slice(), &[
String::from("0"),
String::from("1"),
String::from("2"),
String::from("3"),
String::from("4"),
String::from("100"),
String::from("200"),
]);
assert!(any_vec2.is_empty());
}

/*
#[test]
fn any_vec_index_test() {
Expand Down