Bytes

Struct Bytes 

Source
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>

Source

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);
Source

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)
Source

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 implements ToString
§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));
Source

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 AsSyncStr for Bytes

Source§

fn as_str(&self) -> &str

Returns a string slice reference.

This method provides access to the underlying string data in a way that is safe to use across thread boundaries.

§Returns

A reference to a string slice with lifetime tied to self.

Source§

impl<const SIZE: usize> Clone for Bytes<SIZE>

Source§

fn clone(&self) -> Bytes<SIZE>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const SIZE: usize> Debug for Bytes<SIZE>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const SIZE: usize> Deref for Bytes<SIZE>

Source§

fn deref(&self) -> &Self::Target

Dereferences to the underlying byte array.

This allows Bytes to be used anywhere a [u8; SIZE] reference is expected.

§Examples
use osal_rs::utils::Bytes;
 
let bytes = Bytes::<8>::new_by_str("test");
assert_eq!(bytes[0], b't');
Source§

type Target = [u8; SIZE]

The resulting type after dereferencing.
Source§

impl<const SIZE: usize> Display for Bytes<SIZE>

Source§

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> Hash for Bytes<SIZE>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const SIZE: usize> Ord for Bytes<SIZE>

Source§

fn cmp(&self, other: &Bytes<SIZE>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const SIZE: usize> PartialEq for Bytes<SIZE>

Source§

fn eq(&self, other: &Bytes<SIZE>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const SIZE: usize> PartialOrd for Bytes<SIZE>

Source§

fn partial_cmp(&self, other: &Bytes<SIZE>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const SIZE: usize> Copy for Bytes<SIZE>

Source§

impl<const SIZE: usize> Eq for Bytes<SIZE>

Source§

impl<const SIZE: usize> StructuralPartialEq for Bytes<SIZE>

Auto Trait Implementations§

§

impl<const SIZE: usize> Freeze for Bytes<SIZE>

§

impl<const SIZE: usize> RefUnwindSafe for Bytes<SIZE>

§

impl<const SIZE: usize> Send for Bytes<SIZE>

§

impl<const SIZE: usize> Sync for Bytes<SIZE>

§

impl<const SIZE: usize> Unpin for Bytes<SIZE>

§

impl<const SIZE: usize> UnwindSafe for Bytes<SIZE>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.