fix: Fix parsing datatype inconsistent across platforms

This commit is contained in:
Vanalite 2025-09-16 17:46:57 +07:00
parent 633a6ac032
commit fd046a2d08

View File

@ -45,20 +45,41 @@ pub fn get_vulkan_gpus(lib_path: &str) -> Vec<GpuInfo> {
}
}
fn parse_c_string_i8(buf: &[i8]) -> String {
unsafe { std::ffi::CStr::from_ptr(buf.as_ptr() as *const u8) }
fn parse_c_string_u8(buf: &[u8]) -> String {
unsafe { std::ffi::CStr::from_ptr(buf.as_ptr() as *const std::ffi::c_char) }
.to_str()
.unwrap_or_default()
.to_string()
}
fn parse_c_string_u8(buf: &[u8]) -> String {
unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) }
fn parse_c_string_i8(buf: &[i8]) -> String {
unsafe { std::ffi::CStr::from_ptr(buf.as_ptr() as *const std::ffi::c_char) }
.to_str()
.unwrap_or_default()
.to_string()
}
fn parse_c_string_platform_specific(buf: &[std::ffi::c_char]) -> String {
#[cfg(target_os = "android")]
{
// Android typically uses i8 for c_char
let i8_buf = unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const i8, buf.len()) };
parse_c_string_i8(i8_buf)
}
#[cfg(target_os = "ios")]
{
// iOS typically uses i8 for c_char
let i8_buf = unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const i8, buf.len()) };
parse_c_string_i8(i8_buf)
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
// Desktop platforms typically use u8 for c_char
let u8_buf = unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len()) };
parse_c_string_u8(u8_buf)
}
}
fn get_vulkan_gpus_internal(lib_path: &str) -> Result<Vec<GpuInfo>, Box<dyn std::error::Error>> {
let entry = if lib_path.is_empty() {
unsafe { Entry::load()? }
@ -103,7 +124,7 @@ fn get_vulkan_gpus_internal(lib_path: &str) -> Result<Vec<GpuInfo>, Box<dyn std:
}
let device_info = GpuInfo {
name: parse_c_string_u8(&props.device_name),
name: parse_c_string_platform_specific(&props.device_name),
total_memory: unsafe { instance.get_physical_device_memory_properties(*device) }
.memory_heaps
.iter()
@ -112,7 +133,7 @@ fn get_vulkan_gpus_internal(lib_path: &str) -> Result<Vec<GpuInfo>, Box<dyn std:
.sum(),
vendor: Vendor::from_vendor_id(props.vendor_id),
uuid: parse_uuid(&id_props.device_uuid),
driver_version: parse_c_string_u8(&driver_props.driver_info),
driver_version: parse_c_string_platform_specific(&driver_props.driver_info),
nvidia_info: None,
vulkan_info: Some(VulkanInfo {
index: i as u64,