use crate::{PointerType, StaticSize, Write, Writer}; use derive_deref::Deref; use std::{marker::PhantomData, mem::size_of}; #[derive(Debug, Deref)] pub struct Position(PointerType, PhantomData) where T: ?Sized; impl Clone for Position where T: ?Sized, { fn clone(&self) -> Self { Position(self.0, PhantomData) } } impl Copy for Position where T: ?Sized {} impl Position where T: ?Sized, { pub fn new(value: PointerType) -> Position { Position(value, PhantomData) } pub fn cast(&self) -> Position { Position::new(self.0) } pub fn offset(&self, offset: PointerType) -> Position { Position::new(self.0 + offset) } } impl StaticSize for Position where T: ?Sized, { const STATIC_SIZE: PointerType = size_of::() as PointerType; } impl Write for Position where T: ?Sized, { type Output = Position; fn write(&self, writer: &mut Writer) -> Position { let src = writer.position::>(); let dst = self; let offset = *src - **dst; writer.write(&offset).cast() } }