Skip to main content

devela/sys/os/linux/io/term/
cc.rs

1// devela/src/sys/os/linux/io/term/cc.rs
2//
3//! Typed Linux termios special-character indices.
4//
5
6use crate::LINUX_TERMIOS_CC as CC;
7
8#[doc = crate::_tags!(linux term)]
9/// A symbolic index into [`LinuxTermios::c_cc`][crate::LinuxTermios::c_cc].
10#[doc = crate::_doc_meta!{
11    location("sys/os/linux/io/term"),
12    test_size_of(LinuxTermiosCc = 1|8),
13}]
14///
15/// These constants identify slots in the terminal control-character array.
16/// They are indices, not character values.
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
18#[repr(transparent)]
19pub struct LinuxTermiosCc(u8);
20
21impl LinuxTermiosCc {
22    /// The number of Linux termios control-character slots.
23    pub const COUNT: usize = 19; // would be CC::NCCS
24
25    /// Interrupt character.
26    pub const VINTR: Self = Self(CC::VINTR);
27    /// Quit character.
28    pub const VQUIT: Self = Self(CC::VQUIT);
29    /// Erase character.
30    pub const VERASE: Self = Self(CC::VERASE);
31    /// Kill character.
32    pub const VKILL: Self = Self(CC::VKILL);
33    /// End-of-file character.
34    pub const VEOF: Self = Self(CC::VEOF);
35    /// Timeout value for noncanonical reads.
36    pub const VTIME: Self = Self(CC::VTIME);
37    /// Minimum byte count for noncanonical reads.
38    pub const VMIN: Self = Self(CC::VMIN);
39    /// Switch character.
40    pub const VSWTC: Self = Self(CC::VSWTC);
41    /// Start character.
42    pub const VSTART: Self = Self(CC::VSTART);
43    /// Stop character.
44    pub const VSTOP: Self = Self(CC::VSTOP);
45    /// Suspend character.
46    pub const VSUSP: Self = Self(CC::VSUSP);
47    /// Additional end-of-line character.
48    pub const VEOL: Self = Self(CC::VEOL);
49    /// Reprint unread characters.
50    pub const VREPRINT: Self = Self(CC::VREPRINT);
51    /// Discard pending output.
52    pub const VDISCARD: Self = Self(CC::VDISCARD);
53    /// Word erase character.
54    pub const VWERASE: Self = Self(CC::VWERASE);
55    /// Literal next character.
56    pub const VLNEXT: Self = Self(CC::VLNEXT);
57    /// Second additional end-of-line character.
58    pub const VEOL2: Self = Self(CC::VEOL2);
59
60    /// Returns a checked control-character index.
61    #[must_use]
62    pub const fn new(index: u8) -> Option<Self> {
63        if (index as usize) < Self::COUNT { Some(Self(index)) } else { None }
64    }
65    /// Returns the raw index as `u8`.
66    #[must_use]
67    pub const fn as_u8(self) -> u8 {
68        self.0
69    }
70    /// Returns the raw index as `usize`.
71    #[must_use]
72    pub const fn index(self) -> usize {
73        self.0 as usize
74    }
75    const _ASSERT_RAW_VALUES: () = {
76        assert!(Self::VINTR.index() == CC::VINTR as usize);
77        assert!(Self::VQUIT.index() == CC::VQUIT as usize);
78        assert!(Self::VERASE.index() == CC::VERASE as usize);
79        assert!(Self::VKILL.index() == CC::VKILL as usize);
80        assert!(Self::VEOF.index() == CC::VEOF as usize);
81        assert!(Self::VTIME.index() == CC::VTIME as usize);
82        assert!(Self::VMIN.index() == CC::VMIN as usize);
83        assert!(Self::VSWTC.index() == CC::VSWTC as usize);
84        assert!(Self::VSTART.index() == CC::VSTART as usize);
85        assert!(Self::VSTOP.index() == CC::VSTOP as usize);
86        assert!(Self::VSUSP.index() == CC::VSUSP as usize);
87        assert!(Self::VEOL.index() == CC::VEOL as usize);
88        assert!(Self::VREPRINT.index() == CC::VREPRINT as usize);
89        assert!(Self::VDISCARD.index() == CC::VDISCARD as usize);
90        assert!(Self::VWERASE.index() == CC::VWERASE as usize);
91        assert!(Self::VLNEXT.index() == CC::VLNEXT as usize);
92        assert!(Self::VEOL2.index() == CC::VEOL2 as usize);
93    };
94}