I’m trying to convert some byte-slice (nul-terminated UTF-8) into a proper String. I didn’t figure out how to perform this with CString without using unsafe (which shouldn’t be necessary) so I wrote my own:
let device_name_bytes: [libc::c_uchar; VK_MAX_PHYSICAL_DEVICE_NAME_SIZE] = properties.deviceName; let device_name_len = device_name_bytes.iter().position(|&c| c == 0).unwrap_or(device_name_bytes.len()); let device_name = String::from_utf8_lossy(&device_name_bytes[0..device_name_len]).into_owned();
This works flawlessly but it wouldn’t let me decouple the lifetime of the source (
properties.deviceName) from the result (
device_name). Cow::into_owned() implicitly copies the underlying slice, but passes the lifetime from the source (if I got this right). I don’t want to create another unnecessary clone.
device_name shall be part of the return value of the surrounding function.
properties.deviceName is a field of a temporary structure which shall not survive the surrounding function.