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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::ops::Range;
use core::slice::{Chunks, ChunksMut};

use crate::access::{Access, ReadAccess, ReadOnly, WriteAccess, WriteOnly};

pub enum Operation<'a, A: Access> {
    Read {
        buf: &'a mut [u8],
        witness: A::ReadWitness,
    },
    Write {
        buf: &'a [u8],
        witness: A::WriteWitness,
    },
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum OperationType {
    Read,
    Write,
}

impl OperationType {
    pub fn is_read(self) -> bool {
        self == Self::Read
    }

    pub fn is_write(self) -> bool {
        self == Self::Write
    }
}

impl<'a, A: Access> Operation<'a, A> {
    pub fn with_read_access<A1: ReadAccess + Access<WriteWitness = A::WriteWitness>>(
        &'a mut self,
    ) -> Operation<'a, A1> {
        match self {
            Self::Read { buf, .. } => Operation::Read {
                buf,
                witness: A1::READ_WITNESS,
            },
            Self::Write { buf, witness } => Operation::Write {
                buf,
                witness: *witness,
            },
        }
    }

    pub fn with_write_access<A1: WriteAccess + Access<ReadWitness = A::ReadWitness>>(
        &'a mut self,
    ) -> Operation<'a, A1> {
        match self {
            Self::Read { buf, witness } => Operation::Read {
                buf,
                witness: *witness,
            },
            Self::Write { buf, .. } => Operation::Write {
                buf,
                witness: A1::WRITE_WITNESS,
            },
        }
    }

    pub fn len(&self) -> usize {
        match self {
            Self::Read { buf, .. } => buf.len(),
            Self::Write { buf, .. } => buf.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn ty(&self) -> OperationType {
        match self {
            Self::Read { .. } => OperationType::Read,
            Self::Write { .. } => OperationType::Write,
        }
    }

    pub fn index(&'a mut self, index: Range<usize>) -> Self {
        match self {
            Self::Read { buf, witness } => Self::Read {
                buf: &mut buf[index],
                witness: *witness,
            },
            Self::Write { buf, witness } => Self::Write {
                buf: &buf[index],
                witness: *witness,
            },
        }
    }

    pub fn split_at(&'a mut self, mid: usize) -> (Self, Self) {
        match self {
            Self::Read { buf, witness } => {
                let (left, right) = buf.split_at_mut(mid);
                let left = Self::Read {
                    buf: left,
                    witness: *witness,
                };
                let right = Self::Read {
                    buf: right,
                    witness: *witness,
                };
                (left, right)
            }
            Self::Write { buf, witness } => {
                let (left, right) = buf.split_at(mid);
                let left = Self::Write {
                    buf: left,
                    witness: *witness,
                };
                let right = Self::Write {
                    buf: right,
                    witness: *witness,
                };
                (left, right)
            }
        }
    }

    pub fn chunks(&'a mut self, chunk_size: usize) -> impl Iterator<Item = Operation<'a, A>> {
        match self {
            Self::Read { buf, witness } => OperationChunks::Read {
                it: buf.chunks_mut(chunk_size),
                witness: *witness,
            },
            Self::Write { buf, witness } => OperationChunks::Write {
                it: buf.chunks(chunk_size),
                witness: *witness,
            },
        }
    }
}

enum OperationChunks<'a, A: Access> {
    Read {
        it: ChunksMut<'a, u8>,
        witness: A::ReadWitness,
    },
    Write {
        it: Chunks<'a, u8>,
        witness: A::WriteWitness,
    },
}

impl<'a, A: Access> Iterator for OperationChunks<'a, A> {
    type Item = Operation<'a, A>;

    fn next(&mut self) -> Option<Operation<'a, A>> {
        match self {
            Self::Read { it, witness } => it.next().map(|buf| Operation::Read {
                buf,
                witness: *witness,
            }),
            Self::Write { it, witness } => it.next().map(|buf| Operation::Write {
                buf,
                witness: *witness,
            }),
        }
    }
}

impl<'a> Operation<'a, ReadOnly> {
    #[allow(clippy::explicit_auto_deref)]
    pub fn as_read(&'a mut self) -> &'a mut [u8] {
        match self {
            Self::Read { buf, .. } => buf,
            Self::Write { witness, .. } => witness.absurd(),
        }
    }
}

impl<'a> Operation<'a, WriteOnly> {
    #[allow(clippy::explicit_auto_deref)]
    pub fn as_write(&'a self) -> &'a [u8] {
        match self {
            Self::Read { witness, .. } => witness.absurd(),
            Self::Write { buf, .. } => buf,
        }
    }
}

impl<'a, A: ReadAccess> Operation<'a, A> {
    pub fn read(buf: &'a mut [u8]) -> Self {
        Self::Read {
            buf,
            witness: A::READ_WITNESS,
        }
    }
}

impl<'a, A: WriteAccess> Operation<'a, A> {
    pub fn write(buf: &'a [u8]) -> Self {
        Self::Write {
            buf,
            witness: A::WRITE_WITNESS,
        }
    }
}