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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::marker::PhantomData;

use crate::{io::SeekFrom, ApplyLayout, Decode, Encode, Fixed, Frame, Read, Seek, Write, IO};

pub struct Raw<T>(PhantomData<T>);

impl<T> Fixed for Raw<T> {
    fn size() -> u64 {
        0
    }
}

impl<T> Decode for Raw<T> {
    fn decode<R: Read + Seek + IO>(_: &mut R) -> Result<Self, R::Error> {
        Ok(Raw(PhantomData))
    }
}

impl<T> Encode for Raw<T> {
    fn encode<W: Write + Seek + IO>(&self, _: &mut W) -> Result<(), W::Error> {
        Ok(())
    }
}

impl<'a, T: 'a, R: 'a + IO> ApplyLayout<'a, R> for Raw<T> {
    type Frame = RawFrame<'a, T, R>;

    fn apply_layout(stream: &'a mut R, offset: u64) -> Result<Self::Frame, R::Error> {
        Ok(RawFrame {
            stream,
            offset,
            pd: PhantomData,
        })
    }
}

pub struct RawFrame<'a, T, R> {
    stream: &'a mut R,
    offset: u64,
    pd: PhantomData<T>,
}

impl<'a, T: Decode + Fixed, R: Read + Seek + IO> RawFrame<'a, T, R> {
    pub fn get(&mut self, index: u64) -> Result<T, R::Error> {
        self.stream
            .seek(SeekFrom::Start(self.offset + index * T::size()))?;
        T::decode(self.stream)
    }
}

impl<'a, T: Encode + Fixed, R: Write + Seek + IO> RawFrame<'a, T, R> {
    pub fn set(&mut self, index: u64, elem: &T) -> Result<(), R::Error> {
        self.stream
            .seek(SeekFrom::Current((index * T::size()) as i64))?;
        elem.encode(self.stream)
    }
}

impl<'a, T, R> Frame<R> for RawFrame<'a, T, R> {
    fn stream(&mut self) -> &mut R {
        self.stream
    }

    fn offset(&self) -> u64 {
        self.offset
    }
}

#[derive(Debug)]
pub struct RawBytes;

impl Fixed for RawBytes {
    fn size() -> u64 {
        0
    }
}

impl Decode for RawBytes {
    fn decode<R: Read + Seek + IO>(_: &mut R) -> Result<Self, R::Error> {
        Ok(RawBytes)
    }
}

impl Encode for RawBytes {
    fn encode<W: Write + Seek + IO>(&self, _: &mut W) -> Result<(), W::Error> {
        Ok(())
    }
}

impl<'a, R: 'a + IO> ApplyLayout<'a, R> for RawBytes {
    type Frame = RawBytesFrame<'a, R>;

    fn apply_layout(stream: &'a mut R, offset: u64) -> Result<Self::Frame, R::Error> {
        Ok(RawBytesFrame { stream, offset })
    }
}

pub struct RawBytesFrame<'a, R> {
    stream: &'a mut R,
    offset: u64,
}

impl<'a, R: Seek + IO> RawBytesFrame<'a, R> {
    pub fn direct_stream(&mut self) -> Result<&mut R, R::Error> {
        self.stream.seek(SeekFrom::Start(self.offset))?;
        Ok(&mut self.stream)
    }
}

impl<'a, R> Frame<R> for RawBytesFrame<'a, R> {
    fn stream(&mut self) -> &mut R {
        self.stream
    }

    fn offset(&self) -> u64 {
        self.offset
    }
}