1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use core::marker::PhantomData; use super::{Encoding, Error}; pub trait Entry where Self: Sized, { type Error; const SIZE: usize; fn new(slice: &[u8], encoding: Encoding) -> Result<Self, Self::Error>; } #[derive(Clone)] pub struct Table<'a, E> where E: Entry, { slice: &'a [u8], encoding: Encoding, phantom_data: PhantomData<E>, } impl<'a, E> Table<'a, E> where E: Entry<Error = Error>, { pub fn new(slice: &'a [u8], encoding: Encoding) -> Self { Table { slice, encoding, phantom_data: PhantomData, } } pub fn length(&self) -> usize { self.slice.len() / E::SIZE } pub fn pick(&self, index: usize) -> Result<E, E::Error> { let start = index * E::SIZE; let end = start + E::SIZE; if self.slice.len() < end { return Err(Error::SliceTooShort); }; E::new(&self.slice[start..end], self.encoding.clone()) } }