use crate::Value;
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct Array(pub(crate) Vec<Value>);
impl Array {
#[must_use]
pub const fn new() -> Self {
Self(Vec::new())
}
#[must_use]
pub const fn get_ref(&self) -> &Vec<Value> {
&self.0
}
pub fn get_mut(&mut self) -> &mut Vec<Value> {
&mut self.0
}
#[must_use]
pub fn into_inner(self) -> Vec<Value> {
self.0
}
pub fn from_sequence<I>(items: I) -> Self
where
I: IntoIterator<Item = Value>,
{
Self(items.into_iter().collect())
}
pub fn try_from_sequence<I, E>(items: I) -> Result<Self, E>
where
I: IntoIterator<Item = Result<Value, E>>,
{
items.into_iter().collect::<Result<Vec<_>, _>>().map(Self)
}
}
impl<T: Into<Value> + Copy> From<&[T]> for Array {
fn from(slice: &[T]) -> Self {
Self(slice.iter().map(|&x| x.into()).collect())
}
}
impl<const N: usize, T: Into<Value>> From<[T; N]> for Array {
fn from(array: [T; N]) -> Self {
Self(array.into_iter().map(|x| x.into()).collect())
}
}
impl<T: Into<Value>> From<Vec<T>> for Array {
fn from(vec: Vec<T>) -> Self {
Self(vec.into_iter().map(|x| x.into()).collect())
}
}
impl<T: Into<Value>> From<Box<[T]>> for Array {
fn from(boxed: Box<[T]>) -> Self {
Self(Vec::from(boxed).into_iter().map(|x| x.into()).collect())
}
}
impl From<()> for Array {
fn from(_: ()) -> Self {
Self(Vec::new())
}
}