Skip to content

Commit

Permalink
WIP: refactor(hal): migrate from cstr_from_bytes_until_nul to `CStr…
Browse files Browse the repository at this point in the history
…::from_bytes_until_nul`

Not mergeable until MSRV is Rust 1.69.
  • Loading branch information
ErichDonGubler committed Dec 22, 2023
1 parent 9a6a25e commit 12f5b62
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
20 changes: 0 additions & 20 deletions wgpu-hal/src/auxil/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,6 @@ impl crate::TextureCopy {
}
}

/// Construct a `CStr` from a byte slice, up to the first zero byte.
///
/// Return a `CStr` extending from the start of `bytes` up to and
/// including the first zero byte. If there is no zero byte in
/// `bytes`, return `None`.
///
/// This can be removed when `CStr::from_bytes_until_nul` is stabilized.
/// ([#95027](https://github.com/rust-lang/rust/issues/95027))
#[allow(dead_code)]
pub(crate) fn cstr_from_bytes_until_nul(bytes: &[u8]) -> Option<&std::ffi::CStr> {
// Use a more safe implementation of [what `std` does].
//
// [what `std` does]: https://doc.rust-lang.org/1.69.0/src/core/ffi/c_str.rs.html#329-340
if let Some(nul_pos) = bytes.iter().copied().position(|b| b == 0) {
Some(std::ffi::CStr::from_bytes_with_nul(bytes.get(..nul_pos + 1).unwrap()).unwrap())
} else {
None
}
}

#[allow(dead_code)]
pub(crate) fn cstr_from_chars_until_nul(chars: &[std::os::raw::c_char]) -> Option<&std::ffi::CStr> {
if chars.contains(&0) {
Expand Down
9 changes: 4 additions & 5 deletions wgpu-hal/src/vulkan/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ impl Drop for super::InstanceShared {
impl crate::Instance<super::Api> for super::Instance {
unsafe fn init(desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> {
profiling::scope!("Init Vulkan Backend");
use crate::auxil::cstr_from_bytes_until_nul;

let entry = unsafe {
profiling::scope!("Load vk library");
Expand Down Expand Up @@ -631,9 +630,9 @@ impl crate::Instance<super::Api> for super::Instance {
instance_layers: &'layers [vk::LayerProperties],
name: &CStr,
) -> Option<&'layers vk::LayerProperties> {
instance_layers
.iter()
.find(|inst_layer| cstr_from_bytes_until_nul(&inst_layer.layer_name) == Some(name))
instance_layers.iter().find(|inst_layer| {
CStr::from_bytes_until_nul(&inst_layer.layer_name).ok() == Some(name)
})
}

let nv_optimus_layer = CStr::from_bytes_with_nul(b"VK_LAYER_NV_optimus\0").unwrap();
Expand All @@ -656,7 +655,7 @@ impl crate::Instance<super::Api> for super::Instance {
// Put the callback data on the heap, to ensure it will never be
// moved.
let callback_data = Box::new(super::DebugUtilsMessengerUserData {
validation_layer_description: cstr_from_bytes_until_nul(
validation_layer_description: CStr::from_bytes_until_nul(
&layer_properties.description,
)
.unwrap()
Expand Down

0 comments on commit 12f5b62

Please sign in to comment.