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
33 changes: 33 additions & 0 deletions src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ impl<T: PartialEq<T>, R: RefCount, A: Allocator> PartialEq<&[T]> for RefCountedV
}
}

impl<T: Eq, R: RefCount, A: Allocator> Eq for RefCountedVector<T, R, A> {}

impl<T, R: RefCount, A: Allocator> AsRef<[T]> for RefCountedVector<T, R, A> {
fn as_ref(&self) -> &[T] {
self.as_slice()
Expand All @@ -679,6 +681,12 @@ impl<T, R: RefCount> Default for RefCountedVector<T, R, Global> {
}
}

impl<T, R: RefCount> FromIterator<T> for RefCountedVector<T, R, Global> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Vector::from_iter(iter).into_shared_with_ref_count()
}
}

impl<'a, T, R: RefCount, A: Allocator> IntoIterator for &'a RefCountedVector<T, R, A> {
type Item = &'a T;
type IntoIter = core::slice::Iter<'a, T>;
Expand Down Expand Up @@ -734,6 +742,12 @@ impl<T: Clone, R: RefCount, A: Allocator + Clone> DerefMut for RefCountedVector<
}
}

impl<T: core::hash::Hash, R: RefCount, A: Allocator> core::hash::Hash for RefCountedVector<T, R, A> {
fn hash<H>(&self, state: &mut H) where H: core::hash::Hasher {
self.as_slice().hash(state)
}
}

impl<T: Debug, R: RefCount, A: Allocator> Debug for RefCountedVector<T, R, A> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
self.as_slice().fmt(f)
Expand Down Expand Up @@ -836,6 +850,25 @@ fn grow() {
assert_eq!(b.as_slice(), &[num(1), num(2), num(3)]);
}

#[test]
fn eq_and_hash() {
use std::collections::HashMap;

let mut map: HashMap<AtomicSharedVector<Box<u32>>, u32> = HashMap::new();
map.insert((0..3).map(num).collect(), 1);
map.insert((0..2).map(num).collect(), 2);

let key: AtomicSharedVector<Box<u32>> = (0..3).map(num).collect();
assert_eq!(map.get(&key), Some(&1));
// Distinct buffers with the same contents hash and compare equal.
assert!(!key.ptr_eq(map.keys().find(|k| **k == key).unwrap()));

let mut set: std::collections::HashSet<SharedVector<u32>> = std::collections::HashSet::new();
set.insert(SharedVector::from_slice(&[1, 2]));
assert!(set.contains(&SharedVector::from_slice(&[1, 2])));
assert!(!set.contains(&SharedVector::from_slice(&[1, 3])));
}

#[test]
fn ensure_unique_empty() {
let mut v: SharedVector<u32> = SharedVector::new();
Expand Down
64 changes: 52 additions & 12 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::alloc::{AllocError, Allocator, Global};
use crate::drain::Drain;
use crate::into_iter::IntoIter;
use crate::raw::{
self, buffer_layout, AtomicRefCount, BufferSize, Header, HeaderBuffer, RefCount, VecHeader, move_data,
self, buffer_layout, BufferSize, Header, HeaderBuffer, RefCount, VecHeader, move_data,
};
use crate::shared::{AtomicSharedVector, SharedVector};
use crate::shared::{AtomicSharedVector, RefCountedVector, SharedVector};
use crate::splice::Splice;
use crate::{grow_amortized, DefaultRefCount};

Expand Down Expand Up @@ -1149,13 +1149,7 @@ impl<T, A: Allocator> Vector<T, A> {
where
A: Allocator + Clone,
{
if self.raw.header.cap == 0 {
return SharedVector::try_with_capacity_in(0, self.allocator.clone()).unwrap();
}
unsafe {
let inner = self.into_header_buffer::<DefaultRefCount>();
SharedVector { inner }
}
self.into_shared_with_ref_count()
}

/// Make this vector immutable.
Expand All @@ -1164,15 +1158,24 @@ impl<T, A: Allocator> Vector<T, A> {
/// to be reallocated.
#[inline]
pub fn into_shared_atomic(self) -> AtomicSharedVector<T, A>
where
A: Allocator + Clone,
{
self.into_shared_with_ref_count()
}

/// Make this vector immutable, with the reference counting scheme of the caller's choice.
#[inline]
pub(crate) fn into_shared_with_ref_count<R: RefCount>(self) -> RefCountedVector<T, R, A>
where
A: Allocator + Clone,
{
if self.raw.header.cap == 0 {
return AtomicSharedVector::try_with_capacity_in(0, self.allocator.clone()).unwrap();
return RefCountedVector::try_with_capacity_in(0, self.allocator.clone()).unwrap();
}
unsafe {
let inner = self.into_header_buffer::<AtomicRefCount>();
AtomicSharedVector { inner }
let inner = self.into_header_buffer::<R>();
RefCountedVector { inner }
}
}

Expand Down Expand Up @@ -1493,6 +1496,8 @@ impl<T: PartialEq<T>, A: Allocator> PartialEq<&[T]> for Vector<T, A> {
}
}

impl<T: Eq, A: Allocator> Eq for Vector<T, A> {}

impl<T, A: Allocator> AsRef<[T]> for Vector<T, A> {
fn as_ref(&self) -> &[T] {
self.as_slice()
Expand All @@ -1511,6 +1516,15 @@ impl<T> Default for Vector<T, Global> {
}
}

impl<T> FromIterator<T> for Vector<T, Global> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut vector = Vector::new();
vector.extend(iter);

vector
}
}

impl<T, A: Allocator> IntoIterator for Vector<T, A> {
type Item = T;
type IntoIter = IntoIter<T, A>;
Expand Down Expand Up @@ -1676,6 +1690,32 @@ fn basic_unique() {
assert_eq!(d.as_slice(), &[num(0), num(1), num(2), num(3), num(4)]);
}

#[test]
fn from_iterator() {
fn num(val: u32) -> Box<u32> {
Box::new(val)
}

let v: Vector<Box<u32>> = (0..4).map(num).collect();
assert_eq!(v.as_slice(), &[num(0), num(1), num(2), num(3)]);

let v: Vector<Box<u32>> = Vector::from_iter([]);
assert!(v.is_empty());

let v: SharedVector<Box<u32>> = (0..3).map(num).collect();
assert_eq!(v.as_slice(), &[num(0), num(1), num(2)]);

let v: AtomicSharedVector<Box<u32>> = (0..3).map(num).collect();
assert_eq!(v.as_slice(), &[num(0), num(1), num(2)]);

// Iterators without an upper bound on their size hint.
let v: AtomicSharedVector<Box<u32>> = (0..5).filter(|n| n % 2 == 0).map(num).collect();
assert_eq!(v.as_slice(), &[num(0), num(2), num(4)]);

let v: AtomicSharedVector<Box<u32>> = std::iter::empty().collect();
assert!(v.is_empty());
}

#[test]
fn shrink() {
let mut v: Vector<u32> = Vector::with_capacity(32);
Expand Down
Loading