rusticl/api: Shrink unsafe block

There's only two unsafe operations left here, so shrink the unsafe block to make them obvious.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18978>
This commit is contained in:
LingMan 2022-10-05 23:07:08 +02:00 committed by Marge Bot
parent 677c871a0a
commit 0d31bc5ac3

View file

@ -140,22 +140,20 @@ pub fn create_program_with_source(
// compilation, so don't convert this to a Rust `String`.
let mut source = Vec::new();
for (&string_ptr, len) in iter::zip(srcs, lengths) {
unsafe {
let arr = if *len == 0 {
CStr::from_ptr(string_ptr).to_bytes()
} else {
// The spec doesn't say how nul bytes should be handled here or
// if they are legal at all. Assume they truncate the string.
let arr = slice::from_raw_parts(string_ptr.cast(), *len);
// TODO: simplify this a bit with from_bytes_until_nul once
// that's stabilized and available in our msrv
arr.iter()
.position(|&x| x == 0)
.map_or(arr, |nul_index| &arr[..nul_index])
};
let arr = if *len == 0 {
unsafe { CStr::from_ptr(string_ptr) }.to_bytes()
} else {
// The spec doesn't say how nul bytes should be handled here or
// if they are legal at all. Assume they truncate the string.
let arr = unsafe { slice::from_raw_parts(string_ptr.cast(), *len) };
// TODO: simplify this a bit with from_bytes_until_nul once
// that's stabilized and available in our msrv
arr.iter()
.position(|&x| x == 0)
.map_or(arr, |nul_index| &arr[..nul_index])
};
source.extend_from_slice(arr);
}
source.extend_from_slice(arr);
}
Ok(cl_program::from_arc(Program::new(