Skip to content

Commit 78a0d78

Browse files
committed
Make FixedBuf take ownership of underlying byte slice
Signed-off-by: adamperlin <[email protected]>
1 parent 30826ca commit 78a0d78

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/hyperlight_common/src/fixed_buf.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ limitations under the License.
1616

1717
use core::fmt;
1818
use core::result::Result;
19-
use core::ffi::{CStr, FromBytesUntilNulError};
2019

21-
/// FixedStringBuf is a buffer that can hold a fixed-size string.
20+
21+
/// FixedStringBuf is a buffer that can hold a fixed-size string of capacity N.
2222
/// It is meant to be used with a slice that the user has pre-allocated
2323
/// to avoid extra allocations during string formatting.
24-
pub struct FixedStringBuf<'a> {
25-
pub buf: &'a mut [u8],
24+
pub struct FixedStringBuf<const N: usize> {
25+
pub buf: [u8; N],
2626
pub pos: usize,
2727
}
2828

29-
impl<'a> fmt::Write for FixedStringBuf<'a> {
29+
impl<'a, const N: usize> fmt::Write for FixedStringBuf<N> {
3030
fn write_str(&mut self, s: &str) -> fmt::Result {
3131
// we always reserve 1 byte for the null terminator,
3232
// as the buffer must be convertible to CStr.
@@ -41,49 +41,53 @@ impl<'a> fmt::Write for FixedStringBuf<'a> {
4141
}
4242
}
4343

44-
impl <'a> FixedStringBuf<'a> {
44+
impl <const N: usize> FixedStringBuf<N> {
4545
pub fn as_str(&self) -> Result<&str, core::str::Utf8Error> {
4646
core::str::from_utf8(&self.buf[..self.pos])
4747
}
4848

49-
pub fn new(buf: &'a mut [u8]) -> FixedStringBuf<'a> {
50-
assert!(buf.len() > 0);
51-
FixedStringBuf {
52-
buf,
49+
pub const fn new() -> Self {
50+
return FixedStringBuf{
51+
buf: [0u8; N],
5352
pos: 0,
5453
}
5554
}
5655

57-
pub fn reset(&mut self) {
58-
self.pos = 0;
59-
}
60-
6156
/// Null terminates the underlying buffer,
6257
/// and converts to a CStr which borrows the underlying buffer's slice.
63-
pub fn as_c_str(&mut self) -> Result<&CStr, FromBytesUntilNulError> {
58+
pub fn as_c_str(&mut self) -> Result<&core::ffi::CStr, core::ffi::FromBytesUntilNulError> {
6459
// null terminate the buffer.
6560
// we are guaranteed to have enough space since we always reserve one extra
6661
// byte for null in write_str, and assert buf.len() > 0 in the constructor.
6762
assert!(self.buf.len() > 0 && self.pos < self.buf.len());
6863
self.buf[self.pos] = 0;
69-
CStr::from_bytes_until_nul(&self.buf[..self.pos + 1])
64+
core::ffi::CStr::from_bytes_until_nul(&self.buf[..self.pos + 1])
7065
}
7166
}
7267

68+
7369
mod test {
74-
75-
76-
70+
// disable unused import warnings
71+
#![allow(unused_imports)]
72+
use core::fmt::Write;
73+
use core::fmt;
74+
use super::FixedStringBuf;
75+
7776
#[test]
7877
fn test_fixed_buf() {
79-
let mut bs = [0; 21];
80-
let mut buf = FixedStringBuf::new(&mut bs);
78+
let mut buf = FixedStringBuf::<21>::new();
79+
80+
assert_eq!(buf.as_str().unwrap(), "");
81+
8182
write!(&mut buf, "{}", "0123456789").expect("Failed to write to FixedBuf");
8283
write!(&mut buf, "{}", "0123456789").expect("Failed to write to FixedBuf");
8384
assert_eq!(buf.as_str().unwrap(), "01234567890123456789");
8485
assert_eq!(buf.pos, 20);
8586

8687
let res = write!(&mut buf, "10");
8788
assert_eq!(res, Err(fmt::Error));
89+
90+
let c_str = buf.as_c_str().unwrap();
91+
assert_eq!(c_str.to_bytes(), b"01234567890123456789");
8892
}
8993
}

src/hyperlight_guest_bin/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,11 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
144144
_panic_handler(info)
145145
}
146146

147-
static mut PANIC_MSG: [u8; 512] = [0u8; 512];
148-
149-
#[allow(static_mut_refs)]
150-
static PANIC_BUF: Mutex<FixedStringBuf> = Mutex::new(FixedStringBuf{
151-
buf: unsafe { &mut PANIC_MSG },
152-
pos: 0,
153-
});
147+
static PANIC_BUF: Mutex<FixedStringBuf<512>> = Mutex::new(FixedStringBuf::new());
154148

155149
#[inline(always)]
156150
fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
157-
let mut panic_buf_guard: MutexGuard<'_, FixedStringBuf<'static>> = PANIC_BUF.lock();
151+
let mut panic_buf_guard = PANIC_BUF.lock();
158152
let write_res = write!(panic_buf_guard, "{}", info);
159153
if let Err(_) = write_res {
160154
unsafe { abort_with_code_and_message(&[ErrorCode::UnknownError as u8], b"panic: message format failed\0".as_ptr() as *const i8)}

0 commit comments

Comments
 (0)