Skip to content

Commit

Permalink
refactor(hal): split into cstr_from_{bytes,chars}_until_nul
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Dec 22, 2023
1 parent c97b351 commit b717837
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
18 changes: 15 additions & 3 deletions wgpu-hal/src/auxil/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,25 @@ impl crate::TextureCopy {
/// 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: &[std::os::raw::c_char]) -> Option<&std::ffi::CStr> {
if bytes.contains(&0) {
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) {
// Safety for `CStr::from_ptr`:
// - We've ensured that the slice does contain a null terminator.
// - The range is valid to read, because the slice covers it.
// - The memory won't be changed, because the slice borrows it.
unsafe { Some(std::ffi::CStr::from_ptr(bytes.as_ptr())) }
unsafe { Some(std::ffi::CStr::from_ptr(chars.as_ptr())) }
} else {
None
}
Expand Down
12 changes: 6 additions & 6 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,10 @@ impl PhysicalDeviceCapabilities {
}

pub fn supports_extension(&self, extension: &CStr) -> bool {
use crate::auxil::cstr_from_bytes_until_nul;
use crate::auxil::cstr_from_chars_until_nul;
self.supported_extensions
.iter()
.any(|ep| cstr_from_bytes_until_nul(&ep.extension_name) == Some(extension))
.any(|ep| cstr_from_chars_until_nul(&ep.extension_name) == Some(extension))
}

/// Map `requested_features` to the list of Vulkan extension strings required to create the logical device.
Expand Down Expand Up @@ -1070,14 +1070,14 @@ impl super::Instance {
&self,
phd: vk::PhysicalDevice,
) -> Option<crate::ExposedAdapter<super::Api>> {
use crate::auxil::cstr_from_bytes_until_nul;
use crate::auxil::cstr_from_chars_until_nul;
use crate::auxil::db;

let (phd_capabilities, phd_features) = self.shared.inspect(phd);

let info = wgt::AdapterInfo {
name: {
cstr_from_bytes_until_nul(&phd_capabilities.properties.device_name)
cstr_from_chars_until_nul(&phd_capabilities.properties.device_name)
.and_then(|info| info.to_str().ok())
.unwrap_or("?")
.to_owned()
Expand All @@ -1096,7 +1096,7 @@ impl super::Instance {
phd_capabilities
.driver
.as_ref()
.and_then(|driver| cstr_from_bytes_until_nul(&driver.driver_name))
.and_then(|driver| cstr_from_chars_until_nul(&driver.driver_name))
.and_then(|name| name.to_str().ok())
.unwrap_or("?")
.to_owned()
Expand All @@ -1105,7 +1105,7 @@ impl super::Instance {
phd_capabilities
.driver
.as_ref()
.and_then(|driver| cstr_from_bytes_until_nul(&driver.driver_info))
.and_then(|driver| cstr_from_chars_until_nul(&driver.driver_info))
.and_then(|name| name.to_str().ok())
.unwrap_or("?")
.to_owned()
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/vulkan/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl super::Instance {
// Only keep available extensions.
extensions.retain(|&ext| {
if instance_extensions.iter().any(|inst_ext| {
crate::auxil::cstr_from_bytes_until_nul(&inst_ext.extension_name) == Some(ext)
crate::auxil::cstr_from_chars_until_nul(&inst_ext.extension_name) == Some(ext)
}) {
true
} else {
Expand Down

0 comments on commit b717837

Please sign in to comment.