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
// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//! List of lists.

use crate::traits::{FromPointerReader, FromPointerBuilder, ListIter, IndexMove};
use crate::private::layout::{ListReader, ListBuilder, PointerReader, PointerBuilder, Pointer};
use crate::Result;

#[derive(Clone, Copy)]
pub struct Owned<T> where T: crate::traits::Owned {
    marker: ::core::marker::PhantomData<T>,
}

impl<T> crate::traits::Owned for Owned<T> where T: crate::traits::Owned {
    type Reader<'a> = Reader<'a, T>;
    type Builder<'a> = Builder<'a, T>;
}

pub struct Reader<'a, T> where T: crate::traits::Owned {
    marker: ::core::marker::PhantomData<T::Reader<'a>>,
    reader: ListReader<'a>
}

impl <'a, T> Reader<'a, T> where T: crate::traits::Owned {
    pub fn len(&self) -> u32 { self.reader.len() }
    pub fn iter(self) -> ListIter<Reader<'a, T>, Result<T::Reader<'a>>> {
        ListIter::new(self, self.len())
    }
}

impl <'a, T> Clone for Reader<'a, T> where T: crate::traits::Owned {
    fn clone(&self) -> Reader<'a, T> {
        Reader { marker : self.marker, reader : self.reader }
    }
}

impl <'a, T> Copy for Reader<'a, T> where T: crate::traits::Owned {}

impl <'a, T>  IndexMove<u32, Result<T::Reader<'a>>> for Reader<'a, T>
where T: crate::traits::Owned {
    fn index_move(&self, index : u32) -> Result<T::Reader<'a>> {
        self.get(index)
    }
}

impl <'a, T> FromPointerReader<'a> for Reader<'a, T> where T: crate::traits::Owned {
    fn get_from_pointer(reader: &PointerReader<'a>, default: Option<&'a [crate::Word]>) -> Result<Reader<'a, T>> {
        Ok(Reader { reader: reader.get_list(Pointer, default)?,
                    marker: ::core::marker::PhantomData })
    }
}

impl <'a, T> Reader<'a, T> where T: crate::traits::Owned {
    /// Gets the element at position `index`. Panics if `index` is greater than or
    /// equal to `len()`.
    pub fn get(self, index: u32) -> Result<T::Reader<'a>> {
        assert!(index <  self.len());
        FromPointerReader::get_from_pointer(&self.reader.get_pointer_element(index), None)
    }

    /// Gets the element at position `index`. Returns `None` if `index`
    /// is greater than or equal to `len()`.
    pub fn try_get(self, index: u32) -> Option<Result<T::Reader<'a>>> {
        if index <  self.len() {
            Some(FromPointerReader::get_from_pointer(&self.reader.get_pointer_element(index), None))
        } else {
            None
        }
    }
}

impl <'a, T> crate::traits::IntoInternalListReader<'a> for Reader<'a, T> where T: crate::traits::Owned {
    fn into_internal_list_reader(self) -> ListReader<'a> {
        self.reader
    }
}

pub struct Builder<'a, T> where T: crate::traits::Owned {
    marker: ::core::marker::PhantomData<T>,
    builder: ListBuilder<'a>
}

impl <'a, T> Builder<'a, T> where T: crate::traits::Owned {
    pub fn len(&self) -> u32 { self.builder.len() }

    pub fn into_reader(self) -> Reader<'a, T> {
        Reader { reader: self.builder.into_reader(), marker: ::core::marker::PhantomData }
    }
}

impl <'a, T> Builder<'a, T> where T: crate::traits::Owned {
    pub fn init(self, index: u32, size: u32) -> T::Builder<'a> {
        FromPointerBuilder::init_pointer(self.builder.get_pointer_element(index), size)
    }
}

impl <'a, T> Builder<'a, T> where T: crate::traits::Owned {
    pub fn reborrow(&mut self) -> Builder<'_, T> {
        Builder {builder: self.builder.reborrow(), marker: ::core::marker::PhantomData}
    }
}

impl <'a, T> FromPointerBuilder<'a> for Builder<'a, T> where T: crate::traits::Owned {
    fn init_pointer(builder: PointerBuilder<'a>, size : u32) -> Builder<'a, T> {
        Builder {
            marker: ::core::marker::PhantomData,
            builder: builder.init_list(Pointer, size)
        }
    }
    fn get_from_pointer(builder: PointerBuilder<'a>, default: Option<&'a [crate::Word]>) -> Result<Builder<'a, T>> {
        Ok(Builder {
            marker: ::core::marker::PhantomData,
            builder: builder.get_list(Pointer, default)?
        })
    }
}

impl <'a, T> Builder<'a, T> where T: crate::traits::Owned {
    /// Gets the element at position `index`. Panics if `index` is greater than or
    /// equal to `len()`.
    pub fn get(self, index: u32) -> Result<T::Builder<'a>> {
        assert!(index < self.len());
        FromPointerBuilder::get_from_pointer(self.builder.get_pointer_element(index), None)
    }

    /// Gets the element at position `index`. Returns `None` if `index`
    /// is greater than or equal to `len()`.
    pub fn try_get(self, index: u32) -> Option<Result<T::Builder<'a>>> {
        if index < self.len() {
            Some(FromPointerBuilder::get_from_pointer(self.builder.get_pointer_element(index), None))
        } else {
            None
        }
    }

    pub fn set<'b>(&self, index: u32, value: T::Reader<'a>) -> Result<()>
        where T::Reader<'a>: crate::traits::IntoInternalListReader<'b>
    {
        use crate::traits::IntoInternalListReader;
        assert!(index < self.len());
        self.builder.get_pointer_element(index).set_list(&value.into_internal_list_reader(), false)
    }
}

impl <'a, T> crate::traits::SetPointerBuilder for Reader<'a, T>
    where T:crate::traits::Owned
{
    fn set_pointer_builder<'b>(pointer: crate::private::layout::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: crate::traits::Owned
{
    type Item = Result<T::Reader<'a>>;
    type IntoIter = ListIter<Reader<'a, T>, Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}