use serde::{ser, de}; use generic_array::{GenericArray, ArrayLength, typenum}; pub type Array = GenericArray; pub trait LineValid where Self: Sized, { type Length: ArrayLength; fn try_clone_array(a: &Array) -> Result; fn clone_line(&self) -> Array; } pub trait Line where Self: LineValid, { fn clone_array(a: &Array) -> Self; } impl LineValid for Array where L: ArrayLength, { type Length = L; fn try_clone_array(a: &Array) -> Result { Ok(a.clone()) } fn clone_line(&self) -> Array { self.clone() } } impl Line for Array where L: ArrayLength, { fn clone_array(a: &Array) -> Self { a.clone() } } impl LineValid for [u8; 0] { type Length = typenum::U0; fn try_clone_array(_a: &Array) -> Result { Ok([]) } fn clone_line(&self) -> Array { Array::default() } } impl Line for [u8; 0] { fn clone_array(_a: &Array) -> Self { [] } } pub struct LineLike(pub T) where T: Line; impl Clone for LineLike where T: Line, { fn clone(&self) -> Self { let array = self.0.clone_line(); LineLike(T::clone_array(&array)) } } impl ser::Serialize for LineLike where T: Line, { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { let array = self.0.clone_line(); array.serialize(serializer) } } impl<'de, T> de::Deserialize<'de> for LineLike where T: Line, { fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'de>, { let array = de::Deserialize::deserialize(deserializer)?; Ok(LineLike(T::clone_array(&array))) } }