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
/// A block to write data to.
pub struct WriteBlock<T: Copy + Clone + Default + Send> {
    pub(crate) block: Vec<Vec<T>>,

    pub(crate) written_frames: usize,
    pub(crate) restart_count: usize,
}

impl<T: Copy + Clone + Default + Send> WriteBlock<T> {
    /// # Safety
    ///
    /// Using an allocated but uninitialized [`Vec`] is safe because the block data
    /// will be always filled before it is sent to be written by the IO server.
    pub fn new(num_channels: usize, block_size: usize) -> Self {
        let mut block: Vec<Vec<T>> = Vec::with_capacity(num_channels);
        for _ in 0..num_channels {
            let mut data: Vec<T> = Vec::with_capacity(block_size);
            #[allow(clippy::uninit_vec)] // TODO
            unsafe {
                data.set_len(block_size)
            };
            block.push(data);
        }

        WriteBlock {
            block,
            written_frames: 0,
            restart_count: 0,
        }
    }

    pub fn block(&self) -> &[Vec<T>] {
        self.block.as_slice()
    }

    pub fn written_frames(&self) -> usize {
        self.written_frames
    }
}

pub(crate) struct HeapData<T: Copy + Clone + Default + Send> {
    pub block_pool: Vec<WriteBlock<T>>,
    pub current_block: Option<WriteBlock<T>>,
    pub next_block: Option<WriteBlock<T>>,
}