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 crate::space::*;
use crate::*;
use urid::*;
pub struct Tuple;
unsafe impl UriBound for Tuple {
const URI: &'static [u8] = sys::LV2_ATOM__Tuple;
}
impl<'a, 'b> Atom<'a, 'b> for Tuple
where
'a: 'b,
{
type ReadParameter = ();
type ReadHandle = TupleIterator<'a>;
type WriteParameter = ();
type WriteHandle = TupleWriter<'a, 'b>;
fn read(body: Space<'a>, _: ()) -> Option<TupleIterator<'a>> {
Some(TupleIterator { space: body })
}
fn init(frame: FramedMutSpace<'a, 'b>, _: ()) -> Option<TupleWriter<'a, 'b>> {
Some(TupleWriter { frame })
}
}
pub struct TupleIterator<'a> {
space: Space<'a>,
}
impl<'a> Iterator for TupleIterator<'a> {
type Item = UnidentifiedAtom<'a>;
fn next(&mut self) -> Option<UnidentifiedAtom<'a>> {
let (atom, space) = self.space.split_atom()?;
self.space = space;
Some(UnidentifiedAtom::new(atom))
}
}
pub struct TupleWriter<'a, 'b> {
frame: FramedMutSpace<'a, 'b>,
}
impl<'a, 'b> TupleWriter<'a, 'b> {
pub fn init<'c, A: Atom<'a, 'c>>(
&'c mut self,
child_urid: URID<A>,
child_parameter: A::WriteParameter,
) -> Option<A::WriteHandle> {
(&mut self.frame as &mut dyn MutSpace).init(child_urid, child_parameter)
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use crate::space::*;
use std::mem::size_of;
use urid::*;
#[test]
fn test_tuple() {
let map = HashURIDMapper::new();
let urids = crate::AtomURIDCollection::from_map(&map).unwrap();
let mut raw_space: Box<[u8]> = Box::new([0; 256]);
{
let mut space = RootMutSpace::new(raw_space.as_mut());
let mut writer = (&mut space as &mut dyn MutSpace)
.init(urids.tuple, ())
.unwrap();
{
let mut vector_writer =
writer.init::<Vector<Int>>(urids.vector, urids.int).unwrap();
vector_writer.append(&[17; 9]).unwrap();
}
writer.init::<Int>(urids.int, 42).unwrap();
}
{
let (atom, space) = raw_space.split_at(size_of::<sys::LV2_Atom>());
let atom = unsafe { &*(atom.as_ptr() as *const sys::LV2_Atom) };
assert_eq!(atom.type_, urids.tuple);
assert_eq!(
atom.size as usize,
size_of::<sys::LV2_Atom_Vector>()
+ size_of::<i32>() * 9
+ 4
+ size_of::<sys::LV2_Atom_Int>()
);
let (vector, space) = space.split_at(size_of::<sys::LV2_Atom_Vector>());
let vector = unsafe { &*(vector.as_ptr() as *const sys::LV2_Atom_Vector) };
assert_eq!(vector.atom.type_, urids.vector);
assert_eq!(
vector.atom.size as usize,
size_of::<sys::LV2_Atom_Vector_Body>() + size_of::<i32>() * 9
);
assert_eq!(vector.body.child_size as usize, size_of::<i32>());
assert_eq!(vector.body.child_type, urids.int);
let (vector_items, space) = space.split_at(size_of::<i32>() * 9);
let vector_items =
unsafe { std::slice::from_raw_parts(vector_items.as_ptr() as *const i32, 9) };
assert_eq!(vector_items, &[17; 9]);
let (_, space) = space.split_at(4);
let (int, _) = space.split_at(size_of::<sys::LV2_Atom_Int>());
let int = unsafe { &*(int.as_ptr() as *const sys::LV2_Atom_Int) };
assert_eq!(int.atom.type_, urids.int);
assert_eq!(int.atom.size as usize, size_of::<i32>());
assert_eq!(int.body, 42);
}
{
let space = Space::from_slice(raw_space.as_ref());
let (body, _) = space.split_atom_body(urids.tuple).unwrap();
let items: Vec<UnidentifiedAtom> = Tuple::read(body, ()).unwrap().collect();
assert_eq!(items[0].read(urids.vector, urids.int).unwrap(), [17; 9]);
assert_eq!(items[1].read(urids.int, ()).unwrap(), 42);
}
}
}