Add RocList from array

This commit is contained in:
Ayaz Hafiz 2022-08-17 17:58:03 -05:00
parent b1e41a5995
commit f70993c2a3
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
2 changed files with 24 additions and 0 deletions

View file

@ -483,6 +483,12 @@ where
}
}
impl<T, const SIZE: usize> From<[T; SIZE]> for RocList<T> {
fn from(array: [T; SIZE]) -> Self {
Self::from_iter(array)
}
}
impl<'a, T> IntoIterator for &'a RocList<T> {
type Item = &'a T;
type IntoIter = core::slice::Iter<'a, T>;

View file

@ -221,6 +221,24 @@ mod test_roc_std {
assert_eq!(from_iter, from_slice);
}
#[test]
fn list_from_array() {
let elems: [i64; 5] = [1, 2, 3, 4, 5];
let from_slice = RocList::from_slice(&elems);
let from_array = RocList::from(elems);
assert_eq!(from_array, from_slice);
assert_eq!(from_array.capacity(), from_slice.capacity());
}
#[test]
fn list_from_array_zero_size() {
let elems: [(); 5] = [(), (), (), (), ()];
let from_slice = RocList::from_slice(&elems);
let from_array = RocList::from(elems);
assert_eq!(from_array, from_slice);
assert_eq!(from_array.capacity(), from_slice.capacity());
}
#[test]
fn roc_result_to_rust_result() {
let greeting = "Hello, World!";