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
use traits::{FromPointerReader, FromPointerBuilder};
use private::layout::{ListReader, ListBuilder, PointerReader, PointerBuilder,
PrimitiveElement, element_size_for_type};
use Result;
#[derive(Clone, Copy)]
pub struct Owned<T> {
marker: ::std::marker::PhantomData<T>,
}
impl <'a, T> ::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 : ::std::marker::PhantomData<T>,
reader : ListReader<'a>
}
impl <'a, T : PrimitiveElement> Reader<'a, T> {
pub fn new<'b>(reader : ListReader<'b>) -> Reader<'b, T> {
Reader::<'b, T> { reader : reader, marker : ::std::marker::PhantomData }
}
pub fn len(&self) -> u32 { self.reader.len() }
}
impl <'a, T : PrimitiveElement> FromPointerReader<'a> for Reader<'a, T> {
fn get_from_pointer(reader : &PointerReader<'a>) -> Result<Reader<'a, T>> {
Ok(Reader { reader : try!(reader.get_list(element_size_for_type::<T>(), ::std::ptr::null())),
marker : ::std::marker::PhantomData })
}
}
impl <'a, T : PrimitiveElement> Reader<'a, T> {
pub fn get(&self, index : u32) -> T {
assert!(index < self.len());
PrimitiveElement::get(&self.reader, index)
}
}
pub struct Builder<'a, T> where T: PrimitiveElement {
marker : ::std::marker::PhantomData<T>,
builder : ListBuilder<'a>
}
impl <'a, T> Builder<'a, T> where T: PrimitiveElement {
pub fn new(builder : ListBuilder<'a>) -> Builder<'a, T> {
Builder { builder : builder, marker : ::std::marker::PhantomData }
}
pub fn len(&self) -> u32 { self.builder.len() }
pub fn set(&mut self, index : u32, value : T) {
PrimitiveElement::set(&self.builder, index, value);
}
}
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(element_size_for_type::<T>(), size),
marker : ::std::marker::PhantomData }
}
fn get_from_pointer(builder : PointerBuilder<'a>) -> Result<Builder<'a, T>> {
Ok(Builder { builder : try!(builder.get_list(element_size_for_type::<T>(), ::std::ptr::null())),
marker : ::std::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)
}
}
impl <'a, T> ::traits::SetPointerBuilder<Builder<'a, T>> for Reader<'a, T>
where T: PrimitiveElement
{
fn set_pointer_builder<'b>(pointer : ::private::layout::PointerBuilder<'b>,
value : Reader<'a, T>) -> Result<()> {
pointer.set_list(&value.reader)
}
}