use binrw::{binrw, BinRead, BinWrite}; use std::ops::{Deref, DerefMut}; /// Wrapper to prefix array of single type #[binrw] #[derive(Debug, PartialEq)] pub struct ArrayOf where // Static bounds due to https://github.com/jam1garner/binrw/issues/199 T: for<'a> BinRead = ()> + 'static, T: for<'a> BinWrite = ()> + 'static, { // Temporary variable that holds number of elements #[br(temp)] #[bw(try_calc(u32::try_from(value.len())))] count: u32, #[br(count = count)] pub value: Vec, } impl Deref for ArrayOf where T: for<'a> BinRead = ()> + 'static, T: for<'a> BinWrite = ()> + 'static, { type Target = Vec; fn deref(&self) -> &Self::Target { &self.value } } impl DerefMut for ArrayOf where T: for<'a> BinRead = ()> + 'static, T: for<'a> BinWrite = ()> + 'static, { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.value } } impl From> for ArrayOf where T: for<'a> BinRead = ()> + 'static, T: for<'a> BinWrite = ()> + 'static, { fn from(value: Vec) -> Self { Self { value } } }