use crate::error::{Error, ErrorCode};
use crate::utils::storage::Vec;
use super::{tlv_array_iter, FromTLV, TLVArray, TLVElement, TLVTag, TLVWrite, ToTLV, TLV};
impl<'a, T, const N: usize> FromTLV<'a> for [T; N]
where
T: FromTLV<'a> + Default,
{
fn from_tlv(element: &TLVElement<'a>) -> Result<Self, Error> {
let mut vec = Vec::<T, N>::new();
for item in TLVArray::new(element.clone())? {
vec.push(item?).map_err(|_| ErrorCode::ConstraintError)?;
}
while !vec.is_full() {
vec.push(Default::default())
.map_err(|_| ErrorCode::ConstraintError)?;
}
Ok(unwrap!(vec
.into_array()
.map_err(|_| ErrorCode::ConstraintError)))
}
}
impl<T, const N: usize> ToTLV for [T; N]
where
T: ToTLV,
{
fn to_tlv<W: TLVWrite>(&self, tag: &TLVTag, tw: W) -> Result<(), Error> {
self.as_slice().to_tlv(tag, tw)
}
fn tlv_iter(&self, tag: TLVTag) -> impl Iterator<Item = Result<TLV<'_>, Error>> {
tlv_array_iter(tag, self.iter())
}
}