riot_wrappers::helpers::slice_to_cstr

Trait SliceToCStr

Source
pub(crate) trait SliceToCStr {
    // Required method
    fn to_cstr(&self) -> Result<&CStr, FromBytesUntilNulError>;
}
Available on riot_module_vfs only.
Expand description

Trait that eases conversions from a char slice (no matter the signedness, they are used inconsistently in RIOT) to a CStr. The result is often used with ?.to_str().ok()?.

Required Methods§

Source

fn to_cstr(&self) -> Result<&CStr, FromBytesUntilNulError>

Cast self around until it is suitable input to core::ffi::CStr::from_bytes_until_nul(), and run that function.

Note that while “the slice until any null byte” could be safely used in Rust (as a slice or even a str), its presence in C practically always indicates an error, also because that data wouldn’t be usable by other C code using its string conventions.

It is using a local error type because while the semantics of from_bytes_until_nul are the right ones considering how this is used on C fields that are treated with strlen() etc., that function is not stable yet and emulated.

Implementations on Foreign Types§

Source§

impl SliceToCStr for [i8]

Source§

impl SliceToCStr for [u8]

Implementors§