1use crate::space::*;
28use crate::Atom;
29use urid::UriBound;
30
31pub struct Chunk;
35
36unsafe impl UriBound for Chunk {
37 const URI: &'static [u8] = sys::LV2_ATOM__Chunk;
38}
39
40impl<'a, 'b> Atom<'a, 'b> for Chunk
41where
42 'a: 'b,
43{
44 type ReadParameter = ();
45 type ReadHandle = &'a [u8];
46 type WriteParameter = ();
47 type WriteHandle = FramedMutSpace<'a, 'b>;
48
49 fn read(space: Space<'a>, _: ()) -> Option<&'a [u8]> {
50 space.data()
51 }
52
53 fn init(frame: FramedMutSpace<'a, 'b>, _: ()) -> Option<FramedMutSpace<'a, 'b>> {
54 Some(frame)
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use crate::chunk::*;
61 use crate::*;
62 use std::mem::size_of;
63 use urid::*;
64
65 #[test]
66 fn test_chunk_and_slice_writer() {
67 const SLICE_LENGTH: usize = 42;
68
69 let map = HashURIDMapper::new();
70 let urids = crate::AtomURIDCollection::from_map(&map).unwrap();
71
72 let mut raw_space: Box<[u8]> = Box::new([0; 256]);
73
74 {
76 let mut space = RootMutSpace::new(raw_space.as_mut());
77 let mut writer = (&mut space as &mut dyn MutSpace)
78 .init(urids.chunk, ())
79 .unwrap();
80
81 for (i, value) in writer
82 .allocate(SLICE_LENGTH - 1, false)
83 .map(|(_, data)| data)
84 .unwrap()
85 .into_iter()
86 .enumerate()
87 {
88 *value = i as u8;
89 }
90 (&mut writer as &mut dyn MutSpace)
91 .write(&41u8, false)
92 .unwrap();
93 }
94
95 {
97 let raw_space = raw_space.as_ref();
98 let (atom, data) = raw_space.split_at(size_of::<sys::LV2_Atom>());
99
100 let atom = unsafe { &*(atom.as_ptr() as *const sys::LV2_Atom) };
101 assert_eq!(atom.size as usize, SLICE_LENGTH);
102 assert_eq!(atom.type_, urids.chunk.get());
103
104 let data = data.split_at(SLICE_LENGTH).0;
105 for i in 0..SLICE_LENGTH {
106 assert_eq!(data[i] as usize, i);
107 }
108 }
109
110 {
112 let space = Space::from_reference(raw_space.as_ref());
113
114 let data = Chunk::read(space.split_atom_body(urids.chunk).unwrap().0, ()).unwrap();
115 assert_eq!(data.len(), SLICE_LENGTH);
116
117 for (i, value) in data.iter().enumerate() {
118 assert_eq!(*value as usize, i);
119 }
120 }
121 }
122}