|
| 1 | +//! An atom containing memory of undefined type. |
| 2 | +//! |
| 3 | +//! This contents of this atom is considered as a simple blob of data. It used, for example, by the host to transmit the size of a writable atom port. Since it is so simple, it does not need a reading or writing parameter. |
| 4 | +//! |
| 5 | +//! # Example |
| 6 | +//! ``` |
| 7 | +//! use lv2_core::prelude::*; |
| 8 | +//! use lv2_atom::prelude::*; |
| 9 | +//! |
| 10 | +//! use lv2_atom::space::{AtomSpace, AtomWriter, SpaceWriter}; |
| 11 | +//! |
| 12 | +//! #[derive(PortCollection)] |
| 13 | +//! struct MyPorts { |
| 14 | +//! input: InputPort<AtomPort>, |
| 15 | +//! output: OutputPort<AtomPort>, |
| 16 | +//! } |
| 17 | +//! |
| 18 | +//! fn run(ports: &mut MyPorts, urids: &AtomURIDCollection) { |
| 19 | +//! let in_chunk: &AtomSpace = ports.input.read(urids.chunk).unwrap(); |
| 20 | +//! let mut out_chunk: AtomWriter = ports.output.write(urids.chunk).unwrap(); |
| 21 | +//! |
| 22 | +//! let bytes = in_chunk.as_bytes(); |
| 23 | +//! out_chunk.write_bytes(bytes).unwrap(); |
| 24 | +//! } |
| 25 | +//! ``` |
| 26 | +//! |
| 27 | +//! # Specification |
| 28 | +//! |
| 29 | +//! [http://lv2plug.in/ns/ext/atom/atom.html#Chunk](http://lv2plug.in/ns/ext/atom/atom.html#Chunk) |
| 30 | +use crate::space::error::{AtomReadError, AtomWriteError}; |
| 31 | +use crate::space::*; |
| 32 | +use crate::AtomWriter; |
| 33 | +use crate::{Atom, AtomHandle}; |
| 34 | +use urid::UriBound; |
| 35 | + |
| 36 | +/// An atom containing an arbitrary byte buffer. |
| 37 | +/// |
| 38 | +/// [See also the module documentation.](index.html) |
| 39 | +pub struct Chunk; |
| 40 | + |
| 41 | +unsafe impl UriBound for Chunk { |
| 42 | + const URI: &'static [u8] = sys::LV2_ATOM__Chunk; |
| 43 | +} |
| 44 | + |
| 45 | +pub struct ChunkReaderHandle; |
| 46 | +impl<'a> AtomHandle<'a> for ChunkReaderHandle { |
| 47 | + type Handle = &'a AtomSpace; |
| 48 | +} |
| 49 | + |
| 50 | +pub struct ChunkWriterHandle; |
| 51 | +impl<'a> AtomHandle<'a> for ChunkWriterHandle { |
| 52 | + type Handle = AtomWriter<'a>; |
| 53 | +} |
| 54 | + |
| 55 | +impl Atom for Chunk { |
| 56 | + type ReadHandle = ChunkReaderHandle; |
| 57 | + type WriteHandle = ChunkWriterHandle; |
| 58 | + |
| 59 | + #[inline] |
| 60 | + unsafe fn read( |
| 61 | + body: &AtomSpace, |
| 62 | + ) -> Result<<Self::ReadHandle as AtomHandle>::Handle, AtomReadError> { |
| 63 | + Ok(body) |
| 64 | + } |
| 65 | + |
| 66 | + #[inline] |
| 67 | + fn write( |
| 68 | + frame: AtomWriter, |
| 69 | + ) -> Result<<Self::WriteHandle as AtomHandle>::Handle, AtomWriteError> { |
| 70 | + Ok(frame) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + use crate::atoms::chunk::*; |
| 77 | + use crate::*; |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn test_chunk_and_slice_writer() { |
| 81 | + const SLICE_LENGTH: usize = 42; |
| 82 | + |
| 83 | + let map = HashURIDMapper::new(); |
| 84 | + let urids = crate::atoms::AtomURIDCollection::from_map(&map).unwrap(); |
| 85 | + |
| 86 | + let mut raw_space = AlignedVec::<AtomHeader>::new_with_capacity(64); |
| 87 | + let raw_space = raw_space.as_space_mut(); |
| 88 | + |
| 89 | + // writing |
| 90 | + { |
| 91 | + let mut space = SpaceCursor::new(raw_space.as_bytes_mut()); |
| 92 | + let mut writer = space.write_atom(urids.chunk).unwrap(); |
| 93 | + let data = writer.allocate(SLICE_LENGTH).unwrap(); |
| 94 | + |
| 95 | + for (i, value) in data.iter_mut().enumerate() { |
| 96 | + *value = i as u8; |
| 97 | + } |
| 98 | + |
| 99 | + space.write_value(41u8).unwrap(); |
| 100 | + } |
| 101 | + |
| 102 | + // verifying |
| 103 | + { |
| 104 | + let atom = unsafe { raw_space.read().next_atom() }.unwrap(); |
| 105 | + assert_eq!(atom.header().size_of_body(), SLICE_LENGTH); |
| 106 | + assert_eq!(atom.header().urid(), urids.chunk.get()); |
| 107 | + |
| 108 | + let data = atom.body().as_bytes(); |
| 109 | + for (i, value) in data.iter().enumerate() { |
| 110 | + assert_eq!(*value as usize, i); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // reading |
| 115 | + { |
| 116 | + let data = |
| 117 | + unsafe { Chunk::read(raw_space.read().next_atom().unwrap().body()) }.unwrap(); |
| 118 | + assert_eq!(data.bytes_len(), SLICE_LENGTH); |
| 119 | + |
| 120 | + for (i, value) in data.as_bytes().iter().enumerate() { |
| 121 | + assert_eq!(*value as usize, i); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments