-
Notifications
You must be signed in to change notification settings - Fork 0
/
position.rs
56 lines (48 loc) · 1.07 KB
/
position.rs
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
52
53
54
55
56
use crate::{PointerType, StaticSize, Write, Writer};
use derive_deref::Deref;
use std::{marker::PhantomData, mem::size_of};
#[derive(Debug, Deref)]
pub struct Position<T>(PointerType, PhantomData<T>)
where
T: ?Sized;
impl<T> Clone for Position<T>
where
T: ?Sized,
{
fn clone(&self) -> Self {
Position(self.0, PhantomData)
}
}
impl<T> Copy for Position<T> where T: ?Sized {}
impl<T> Position<T>
where
T: ?Sized,
{
pub fn new(value: PointerType) -> Position<T> {
Position(value, PhantomData)
}
pub fn cast<U>(&self) -> Position<U> {
Position::new(self.0)
}
pub fn offset<U: ?Sized>(&self, offset: PointerType) -> Position<U> {
Position::new(self.0 + offset)
}
}
impl<T> StaticSize for Position<T>
where
T: ?Sized,
{
const STATIC_SIZE: PointerType = size_of::<PointerType>() as PointerType;
}
impl<T> Write for Position<T>
where
T: ?Sized,
{
type Output = Position<T>;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
let src = writer.position::<Position<()>>();
let dst = self;
let offset = *src - **dst;
writer.write(&offset).cast()
}
}