use std::marker::PhantomData;
use crate::{
model::Model,
value::{DecodeError, Value},
};
#[derive(Debug)]
pub struct Point<M: Model, T: Value> {
pub offset: u16,
pub length: u16,
pub writable: bool,
model: PhantomData<M>,
point_type: PhantomData<T>,
}
impl<M: Model, T: Value> Point<M, T> {
pub const fn new(offset: u16, length: u16, writable: bool) -> Self {
Self {
offset,
length,
writable,
model: PhantomData,
point_type: PhantomData,
}
}
pub fn from_data(&self, data: &[u16]) -> Result<T, DecodeError> {
let slice = data
.get(self.offset as usize..(self.offset as usize + self.length as usize))
.ok_or(DecodeError::OutOfBounds)?;
let value = T::decode(slice)?;
Ok(value)
}
}
impl<M: Model, T: Value> Copy for Point<M, T> {}
impl<M: Model, T: Value> Clone for Point<M, T> {
fn clone(&self) -> Self {
*self
}
}