Skip to main content

from_str_to_array

Macro from_str_to_array 

Source
macro_rules! from_str_to_array {
    ($str:expr, $buff_name:ident, $buff_size:expr) => { ... };
}
Expand description

Converts a string to a fixed-size byte array.

This macro creates a byte array of the specified size and fills it with the bytes from the input string. If the string is shorter than the buffer, the remaining bytes are filled with spaces. If the string is longer, it is truncated to fit.

§Parameters

  • $str - The source string to convert
  • $buff_name - The identifier name for the created buffer variable
  • $buff_size - The size of the byte array to create

§Examples

use osal_rs::from_str_to_array;
 
let task_name = "MainTask";
from_str_to_array!(task_name, name_buffer, 16);
// name_buffer is now [u8; 16] containing "MainTask        "
 
// Use with C FFI
extern "C" {
    fn create_task(name: *const u8, len: usize);
}
 
unsafe {
    create_task(name_buffer.as_ptr(), name_buffer.len());
}