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
use crate::structures::terminal::{Terminal, TerminalWriteFn};

#[repr(C)]
#[derive(Debug)]
/// Response to [`TerminalRequest`]
pub struct TerminalResponse {
    /// The response revision number
    pub revision: u64,
    /// The number of [Terminal]s in `terminals`
    pub terminal_count: u64,
    /// A pointer to an array of [Terminal] pointers
    pub terminals: *const *const Terminal,
    /// The terminal write function
    ///
    /// # Important
    /// It must be noted that this is the physical address of the write function
    pub write: TerminalWriteFn,
}

impl TerminalResponse {
    /// Get the terminal slice
    ///
    /// # Safety
    /// The pointer must point to a valid array of [Terminal]s
    #[must_use]
    pub unsafe fn get_terminals(&self) -> Option<&[&Terminal]> {
        if self.terminals.is_null() {
            return None;
        }
        Some(core::slice::from_raw_parts(
            self.terminals.cast::<&Terminal>(),
            self.terminal_count.try_into().ok()?,
        ))
    }

    /// Get the terminal slice
    ///
    /// # Safety
    /// The pointer must point to a valid array of [Terminal]s.
    /// Additionally, you must ensure that this is called *nowhere* else, otherwise
    /// very, very bad things may occur due to read and write tearing
    #[must_use]
    pub unsafe fn get_terminals_mut(&self) -> Option<&mut [&mut Terminal]> {
        if self.terminals.is_null() {
            return None;
        }
        Some(core::slice::from_raw_parts_mut(
            self.terminals as *mut &mut Terminal,
            self.terminal_count.try_into().ok()?,
        ))
    }
}