pub struct Bytes<const SIZE: usize = 0>(pub [u8; SIZE]);Expand description
Fixed-size byte array wrapper with string conversion utilities.
Bytes is a generic wrapper around a fixed-size byte array that provides
convenient methods for converting between strings and byte arrays. It’s
particularly useful for interfacing with C APIs that expect fixed-size
character buffers, or for storing strings in embedded systems with
constrained memory.
§Type Parameters
SIZE- The size of the internal byte array (default: 0)
§Examples
use osal_rs::utils::Bytes;
// Create an empty 32-byte buffer
let mut buffer = Bytes::<32>::new();
// Create a buffer from a string
let name = Bytes::<16>::new_by_str("TaskName");
println!("{}", name); // Prints "TaskName"
// Create from any type that implements ToString
let number = 42;
let num_bytes = Bytes::<8>::new_by_string(&number);Tuple Fields§
§0: [u8; SIZE]Implementations§
Source§impl<const SIZE: usize> Bytes<SIZE>
impl<const SIZE: usize> Bytes<SIZE>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new Bytes instance filled with zeros.
This is a const function, allowing it to be used in const contexts and static variable declarations.
§Returns
A Bytes instance with all bytes set to 0.
§Examples
use osal_rs::utils::Bytes;
const BUFFER: Bytes<64> = Bytes::new();
let runtime_buffer = Bytes::<32>::new();
assert_eq!(runtime_buffer[0], 0);Sourcepub fn new_by_str(str: &str) -> Self
pub fn new_by_str(str: &str) -> Self
Creates a new Bytes instance from a string slice.
Copies the bytes from the input string into the fixed-size array.
If the string is shorter than SIZE, the remaining bytes are zero-filled.
If the string is longer, it is truncated to fit.
§Parameters
str- The source string to convert
§Returns
A Bytes instance containing the string data.
§Examples
use osal_rs::utils::Bytes;
let short = Bytes::<16>::new_by_str("Hi");
// Internal array: [b'H', b'i', 0, 0, 0, ...]
let exact = Bytes::<5>::new_by_str("Hello");
// Internal array: [b'H', b'e', b'l', b'l', b'o']
let long = Bytes::<3>::new_by_str("Hello");
// Internal array: [b'H', b'e', b'l'] (truncated)Sourcepub fn new_by_string(str: &impl ToString) -> Self
pub fn new_by_string(str: &impl ToString) -> Self
Creates a new Bytes instance from any type implementing ToString.
This is a convenience wrapper around new_by_str
that first converts the input to a string.
§Parameters
str- Any value that implementsToString
§Returns
A Bytes instance containing the string representation of the input.
§Examples
use osal_rs::utils::Bytes;
// From integer
let num_bytes = Bytes::<8>::new_by_string(&42);
// From String
let string = String::from("Task");
let str_bytes = Bytes::<16>::new_by_string(&string);
// From custom type with ToString
#[derive(Debug)]
struct TaskId(u32);
impl ToString for TaskId {
fn to_string(&self) -> String {
format!("Task-{}", self.0)
}
}
let task_bytes = Bytes::<16>::new_by_string(&TaskId(5));Sourcepub fn fill_str(&mut self, dest: &mut str)
pub fn fill_str(&mut self, dest: &mut str)
Fills a mutable string slice with the contents of the byte array.
Attempts to convert the internal byte array to a UTF-8 string and copies it into the destination string slice. Only copies up to the minimum of the source and destination lengths.
§Parameters
dest- The destination string slice to fill
§Panics
Currently panics (todo!) if the byte array contains invalid UTF-8.
§Examples
use osal_rs::utils::Bytes;
let bytes = Bytes::<16>::new_by_str("Hello World");
let mut output = String::from(" "); // 16 spaces
bytes.fill_str(unsafe { output.as_mut_str() });
assert_eq!(&output[..11], "Hello World");Trait Implementations§
Source§impl<const SIZE: usize> Display for Bytes<SIZE>
impl<const SIZE: usize> Display for Bytes<SIZE>
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the byte array as a C-style null-terminated string.
This implementation treats the byte array as a C string and converts it to a Rust string for display. If the conversion fails, it displays “Conversion error”.
§Safety
This method assumes the byte array contains valid UTF-8 data and is null-terminated. Invalid data may result in the error message being displayed.
§Examples
use osal_rs::utils::Bytes;
let bytes = Bytes::<16>::new_by_str("Hello");
println!("{}", bytes); // Prints "Hello"