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
use std::ops::Range;
use crate::{Trit, TritBuf};
pub trait RawEncoding {
type Trit: Trit;
type Buf: RawEncodingBuf<Slice=Self>;
fn empty() -> &'static Self;
fn len(&self) -> usize;
fn as_i8_slice(&self) -> &[i8];
unsafe fn as_i8_slice_mut(&mut self) -> &mut [i8];
unsafe fn get_unchecked(&self, index: usize) -> Self::Trit;
unsafe fn set_unchecked(&mut self, index: usize, trit: Self::Trit);
unsafe fn slice_unchecked(&self, range: Range<usize>) -> &Self;
unsafe fn slice_unchecked_mut(&mut self, range: Range<usize>) -> &mut Self;
fn is_valid(repr: &i8) -> bool;
unsafe fn from_raw_unchecked(b: &[i8], num_trits: usize) -> &Self;
unsafe fn from_raw_unchecked_mut(b: &mut [i8], num_trits: usize) -> &mut Self;
}
pub trait RawEncodingBuf {
type Slice: RawEncoding + ?Sized;
fn new() -> Self where Self: Sized;
fn from_trits(trits: &[<Self::Slice as RawEncoding>::Trit]) -> Self
where
Self: Sized
{
let mut this = Self::new();
for trit in trits {
this.push(*trit);
}
this
}
fn push(&mut self, trit: <Self::Slice as RawEncoding>::Trit);
fn pop(&mut self) -> Option<<Self::Slice as RawEncoding>::Trit>;
fn as_slice(&self) -> &Self::Slice;
fn as_slice_mut(&mut self) -> &mut Self::Slice;
fn into_encoding<T: RawEncodingBuf>(this: TritBuf<Self>) -> TritBuf<T>
where
Self: Sized,
T: RawEncodingBuf,
T::Slice: RawEncoding<Trit = <Self::Slice as RawEncoding>::Trit>,
{
this.trits().collect()
}
}