pub struct Bytes<const SIZE: usize>(pub [u8; SIZE]);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");Sourcepub fn as_c_str(&self) -> &CStr
pub fn as_c_str(&self) -> &CStr
Converts the byte array to a C string reference.
Creates a CStr reference from the internal byte array, treating it as
a null-terminated C string. This is useful for passing strings to C FFI
functions that expect *const c_char or &CStr.
§Safety
This method is unsafe because it assumes:
- The byte array contains valid UTF-8 data
- The byte array is null-terminated
- There are no interior null bytes before the terminating null
Violating these assumptions may lead to undefined behavior.
§Returns
A reference to a CStr with lifetime tied to self.
§Examples
use osal_rs::utils::Bytes;
let bytes = Bytes::<16>::new_by_str("Hello");
let c_str = bytes.as_c_str();
extern "C" {
fn print_string(s: *const core::ffi::c_char);
}
unsafe {
print_string(c_str.as_ptr());
}Sourcepub fn as_cstring(&self) -> CString
pub fn as_cstring(&self) -> CString
Converts the byte array to an owned C string.
Creates a new CString by copying the contents of the internal byte array.
Unlike as_c_str, this method allocates heap memory and
returns an owned string that can outlive the original Bytes instance.
§Safety
This method uses from_vec_unchecked which assumes the byte array
does not contain any interior null bytes. If this assumption is violated,
the resulting CString will be invalid.
§Returns
An owned CString containing a copy of the byte array data.
§Memory Allocation
This method allocates on the heap. In memory-constrained embedded systems,
prefer as_c_str when possible.
§Examples
use osal_rs::utils::Bytes;
fn process_name(bytes: &Bytes<16>) -> alloc::ffi::CString {
// Create an owned copy that can be returned
bytes.as_cstring()
}
let name = Bytes::<16>::new_by_str("Task");
let owned = process_name(&name);
// 'name' can be dropped, 'owned' still validTrait Implementations§
Source§impl<const SIZE: usize> DerefMut for Bytes<SIZE>
impl<const SIZE: usize> DerefMut for Bytes<SIZE>
Source§fn deref_mut(&mut self) -> &mut Self::Target
fn deref_mut(&mut self) -> &mut Self::Target
Provides mutable access to the underlying byte array.
This allows Bytes to be mutably dereferenced, enabling direct modification
of the internal byte array through the DerefMut trait.
§Examples
use osal_rs::utils::Bytes;
let mut bytes = Bytes::<8>::new();
bytes[0] = b'H';
bytes[1] = b'i';
assert_eq!(bytes[0], b'H');Source§impl<const SIZE: usize> Deserialize for Bytes<SIZE>
Available on non-crate feature serde only.Deserialization implementation for Bytes<SIZE> when the serde feature is disabled.
impl<const SIZE: usize> Deserialize for Bytes<SIZE>
serde only.Deserialization implementation for Bytes<SIZE> when the serde feature is disabled.
This implementation provides basic deserialization by copying bytes from a slice
into a fixed-size array. If the source slice is shorter than SIZE, the remaining
bytes are zero-filled. If longer, it’s truncated to fit.
Source§fn from_bytes(bytes: &[u8]) -> Result<Self>
fn from_bytes(bytes: &[u8]) -> Result<Self>
Creates a Bytes instance from a byte slice.
§Parameters
bytes- The source byte slice to deserialize from
§Returns
Ok(Bytes<SIZE>)- A newBytesinstance with data copied from the slice
§Examples
use osal_rs::utils::Bytes;
use osal_rs::os::Deserialize;
let data = b"Hello";
let bytes = Bytes::<16>::from_bytes(data).unwrap();
// Result: [b'H', b'e', b'l', b'l', b'o', 0, 0, 0, ...]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"Source§impl<const SIZE: usize> Ord for Bytes<SIZE>
impl<const SIZE: usize> Ord for Bytes<SIZE>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const SIZE: usize> PartialOrd for Bytes<SIZE>
impl<const SIZE: usize> PartialOrd for Bytes<SIZE>
Source§impl<const SIZE: usize> Serialize for Bytes<SIZE>
Available on non-crate feature serde only.Serialization implementation for Bytes<SIZE> when the serde feature is disabled.
impl<const SIZE: usize> Serialize for Bytes<SIZE>
serde only.Serialization implementation for Bytes<SIZE> when the serde feature is disabled.
This implementation provides basic serialization by directly returning a reference
to the underlying byte array. It’s used when the library is compiled without the
serde feature, providing a lightweight alternative serialization mechanism.