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
use core::{marker};
use crate::traits::{FromPointerReader, FromPointerBuilder, IndexMove, ListIter};
use crate::private::layout::{ListReader, ListBuilder, PointerReader, PointerBuilder,
PrimitiveElement, data_bits_per_element};
use crate::Result;
#[derive(Clone, Copy)]
pub struct Owned<T> {
marker: marker::PhantomData<T>,
}
impl <'a, T> crate::traits::Owned<'a> for Owned<T> where T: PrimitiveElement {
type Reader = Reader<'a, T>;
type Builder = Builder<'a, T>;
}
#[derive(Clone, Copy)]
pub struct Reader<'a, T> where T: PrimitiveElement {
marker: marker::PhantomData<T>,
reader: ListReader<'a>
}
impl <'a, T: PrimitiveElement> Reader<'a, T> {
pub fn len(&self) -> u32 { self.reader.len() }
pub fn iter(self) -> ListIter<Reader<'a, T>, T>{
let l = self.len();
ListIter::new(self, l)
}
}
impl <'a, T: PrimitiveElement> FromPointerReader<'a> for Reader<'a, T> {
fn get_from_pointer(reader: &PointerReader<'a>, default: Option<&'a [crate::Word]>) -> Result<Reader<'a, T>> {
Ok(Reader { reader: reader.get_list(T::element_size(), default)?,
marker: marker::PhantomData })
}
}
impl <'a, T: PrimitiveElement> IndexMove<u32, T> for Reader<'a, T>{
fn index_move(&self, index: u32) -> T {
self.get(index)
}
}
impl <'a, T: PrimitiveElement> Reader<'a, T> {
pub fn get(&self, index: u32) -> T {
assert!(index < self.len());
PrimitiveElement::get(&self.reader, index)
}
#[cfg(all(target_endian = "little"))]
pub fn as_slice(&self) -> Option<&[T]> {
if self.reader.get_element_size() == T::element_size() {
let bytes = self.reader.into_raw_bytes();
Some (unsafe {
use core::slice;
slice::from_raw_parts(bytes.as_ptr() as *mut T, 8*bytes.len()/(data_bits_per_element(T::element_size())) as usize)
})
} else {
None
}
}
}
impl <'a, T> crate::traits::IntoInternalListReader<'a> for Reader<'a, T> where T: PrimitiveElement {
fn into_internal_list_reader(self) -> ListReader<'a> {
self.reader
}
}
pub struct Builder<'a, T> where T: PrimitiveElement {
marker: marker::PhantomData<T>,
builder: ListBuilder<'a>
}
impl <'a, T> Builder<'a, T> where T: PrimitiveElement {
pub fn len(&self) -> u32 { self.builder.len() }
pub fn into_reader(self) -> Reader<'a, T> {
Reader {
marker: marker::PhantomData,
reader: self.builder.into_reader(),
}
}
pub fn set(&mut self, index: u32, value: T) {
PrimitiveElement::set(&self.builder, index, value);
}
#[cfg(all(target_endian = "little"))]
pub fn as_slice(&self) -> Option<&mut [T]> {
if self.builder.get_element_size() == T::element_size() {
let bytes = self.builder.into_raw_bytes();
Some (unsafe {
use core::slice;
slice::from_raw_parts_mut(bytes.as_ptr() as *mut T, 8*bytes.len()/(data_bits_per_element(T::element_size())) as usize)
})
} else {
None
}
}
}
impl <'a, T: PrimitiveElement> FromPointerBuilder<'a> for Builder<'a, T> {
fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a, T> {
Builder { builder: builder.init_list(T::element_size(), size),
marker: marker::PhantomData }
}
fn get_from_pointer(builder: PointerBuilder<'a>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a, T>> {
Ok(Builder { builder: builder.get_list(T::element_size(), default)?,
marker: marker::PhantomData })
}
}
impl <'a, T : PrimitiveElement> Builder<'a, T> {
pub fn get(&self, index: u32) -> T {
assert!(index < self.len());
PrimitiveElement::get_from_builder(&self.builder, index)
}
pub fn reborrow<'b>(&'b self) -> Builder<'b, T> {
Builder { .. *self }
}
}
impl <'a, T> crate::traits::SetPointerBuilder for Reader<'a, T>
where T: PrimitiveElement
{
fn set_pointer_builder<'b>(pointer: PointerBuilder<'b>,
value: Reader<'a, T>,
canonicalize: bool) -> Result<()> {
pointer.set_list(&value.reader, canonicalize)
}
}
impl <'a, T> ::core::iter::IntoIterator for Reader<'a, T>
where T: PrimitiveElement
{
type Item = T;
type IntoIter = ListIter<Reader<'a, T>, Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}