Skip to content

Commit

Permalink
Implement TryFrom<EcoVec<T>> for [T; N] (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Laurenz <laurmaedje@gmail.com>
  • Loading branch information
kaikalii and laurmaedje authored Oct 30, 2024
1 parent 20c2337 commit 17dbc88
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A clone-on-write alternative to [`Vec`].
use core::alloc::Layout;
use core::array;
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::{self, Debug, Formatter};
Expand Down Expand Up @@ -994,6 +995,31 @@ impl<T: Clone> From<Vec<T>> for EcoVec<T> {
}
}

impl<T: Clone, const N: usize> TryFrom<EcoVec<T>> for [T; N] {
type Error = EcoVec<T>;

fn try_from(mut vec: EcoVec<T>) -> Result<Self, Self::Error> {
if vec.len() != N {
return Err(vec);
}

Ok(if vec.is_unique() {
// Set the length to zero to prevent double drop.
vec.len = 0;

// Safety:
// - We have unique ownership over the underlying allocation.
// - The pointer returned by `data()` is valid for `N` reads.
// - We take ownership of the value and don't drop it again
// in our drop impl.
unsafe { ptr::read(vec.data() as *const [T; N]) }
} else {
// Safety: We know that the length is correct.
unsafe { array::from_fn(|i| vec.get_unchecked(i).clone()) }
})
}
}

impl<T: Clone> FromIterator<T> for EcoVec<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let iter = iter.into_iter();
Expand Down
18 changes: 18 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,24 @@ fn test_vec_clear_drop_panic() {
vec.clear();
}

#[test]
fn test_array_from_vec() {
let array = [String::from("foo"), String::from("bar")];
let a = EcoVec::from(array.clone());
let b = a.clone();
let c: [String; 2] = a.try_into().unwrap();
assert_eq!(b, c);
let d = b.clone();
assert_eq!(c, array);
assert_eq!(d, array);
drop(b);
assert_eq!(c, array);
drop(d);
assert_eq!(c, array);

assert_eq!(<[String; 0]>::try_from(EcoVec::new()).unwrap(), <[String; 0]>::default());
}

#[test]
fn test_str_new() {
// Test inline strings.
Expand Down

0 comments on commit 17dbc88

Please sign in to comment.