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