From 0d31bc5ac3b3505a86ea646d738256d616b6ef8a Mon Sep 17 00:00:00 2001 From: LingMan <18294-LingMan@users.noreply.gitlab.freedesktop.org> Date: Wed, 5 Oct 2022 23:07:08 +0200 Subject: [PATCH] rusticl/api: Shrink unsafe block There's only two unsafe operations left here, so shrink the unsafe block to make them obvious. Part-of: --- src/gallium/frontends/rusticl/api/program.rs | 30 +++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/gallium/frontends/rusticl/api/program.rs b/src/gallium/frontends/rusticl/api/program.rs index 518512f..52dee61 100644 --- a/src/gallium/frontends/rusticl/api/program.rs +++ b/src/gallium/frontends/rusticl/api/program.rs @@ -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]) - }; - - source.extend_from_slice(arr); - } + 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); } Ok(cl_program::from_arc(Program::new( -- 2.7.4