summaryrefslogtreecommitdiffstats
path: root/rsnix/src/strbuf.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rsnix/src/strbuf.rs')
-rw-r--r--rsnix/src/strbuf.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/rsnix/src/strbuf.rs b/rsnix/src/strbuf.rs
index 262720d..0e3ea56 100644
--- a/rsnix/src/strbuf.rs
+++ b/rsnix/src/strbuf.rs
@@ -69,3 +69,31 @@ impl<const N: usize> fmt::Debug for String<N> {
69 write!(f, "{:?}", self.as_str()) 69 write!(f, "{:?}", self.as_str())
70 } 70 }
71} 71}
72
73/// Small `fmt::Write` over a byte slice — no allocation, bounded.
74/// Shared by the panic handler (task-panic message) and the scheduler
75/// (page-fault abort message) so they don't each reinvent it.
76pub struct FmtBuf<'a> {
77 b: &'a mut [u8],
78 n: usize,
79}
80
81impl<'a> FmtBuf<'a> {
82 pub fn new(b: &'a mut [u8]) -> Self {
83 FmtBuf { b, n: 0 }
84 }
85
86 /// Bytes written so far (clamped at the buffer length).
87 pub fn len(&self) -> usize {
88 self.n
89 }
90}
91
92impl fmt::Write for FmtBuf<'_> {
93 fn write_str(&mut self, s: &str) -> fmt::Result {
94 let m = s.len().min(self.b.len() - self.n);
95 self.b[self.n..self.n + m].copy_from_slice(&s.as_bytes()[..m]);
96 self.n += m;
97 Ok(())
98 }
99}