From 1da9c17b9b34b8ae0cdfb10a7340085ad232a06f Mon Sep 17 00:00:00 2001 From: Jonathan Schwender <55576758+jschwe@users.noreply.github.com> Date: Thu, 29 Aug 2024 10:41:59 +0800 Subject: [PATCH] Use cstr literals where possible (#311) * Use cstr literals where possible Convert usage of byte str and normal str with a `\0` to cstr literals where possible. Since CStr::as_ptr() returns `*const c_char` this also allows removing a nice amount of pointer casts. On Windows this also allows removing casts to [`LPCSTR`], which is defined as a `*const CHAR`, where `CHAR` is defined as `c_char` and `c_char` is taken from the std library if the `std` feature is enabled. [`LPCSTR`]: https://docs.rs/winapi/latest/winapi/um/winnt/type.LPCSTR.html Signed-off-by: Jonathan Schwender * examples: prefer `ptr::cast()` over as casts Fixes pedantic clippy lint `ptr_as_ptr`. Signed-off-by: Jonathan Schwender * Add comment explaining cast of `u16` to pointer. Signed-off-by: Jonathan Schwender --------- Signed-off-by: Jonathan Schwender --- Cargo.toml | 2 +- examples/common/mod.rs | 4 +- examples/offscreen.rs | 7 +- examples/threads.rs | 119 ++++++++++------------------ src/platform/generic/egl/context.rs | 4 +- src/platform/generic/egl/device.rs | 23 +++--- src/platform/generic/egl/ffi.rs | 16 ++-- src/platform/windows/wgl/context.rs | 43 +++++----- src/platform/windows/wgl/device.rs | 23 +++--- 9 files changed, 104 insertions(+), 137 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4228b42a..abcbb717 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "surfman" license = "MIT OR Apache-2.0 OR MPL-2.0" -edition = "2018" +edition = "2021" version = "0.9.8" authors = [ "Patrick Walton ", diff --git a/examples/common/mod.rs b/examples/common/mod.rs index b241eb53..9417cd7a 100644 --- a/examples/common/mod.rs +++ b/examples/common/mod.rs @@ -68,7 +68,7 @@ impl Shader { gl::ShaderSource( shader, 1, - &(source.as_ptr() as *const GLchar), + &(source.as_ptr().cast()), &(source.len() as GLint), ); ck(); @@ -116,7 +116,7 @@ impl Buffer { gl::BufferData( gl::ARRAY_BUFFER, data.len() as isize, - data.as_ptr() as *const c_void, + data.as_ptr().cast(), gl::STATIC_DRAW, ); ck(); diff --git a/examples/offscreen.rs b/examples/offscreen.rs index 80ada2de..4eeeb42f 100644 --- a/examples/offscreen.rs +++ b/examples/offscreen.rs @@ -188,7 +188,7 @@ impl TriVertexArray { ck(); let vertex_buffer = Buffer::from_data(slice::from_raw_parts( - TRI_VERTICES.as_ptr() as *const u8, + TRI_VERTICES.as_ptr().cast(), mem::size_of::() * 3, )); @@ -251,10 +251,9 @@ impl TriProgram { let program = Program::new(vertex_shader, fragment_shader); unsafe { let position_attribute = - gl::GetAttribLocation(program.object, b"aPosition\0".as_ptr() as *const GLchar); + gl::GetAttribLocation(program.object, c"aPosition".as_ptr().cast()); ck(); - let color_attribute = - gl::GetAttribLocation(program.object, "aColor\0".as_ptr() as *const GLchar); + let color_attribute = gl::GetAttribLocation(program.object, c"aColor".as_ptr().cast()); ck(); TriProgram { program, diff --git a/examples/threads.rs b/examples/threads.rs index d7f2a75e..aff16f3a 100644 --- a/examples/threads.rs +++ b/examples/threads.rs @@ -811,26 +811,21 @@ impl BlitProgram { let program = Program::new(vertex_shader, fragment_shader); unsafe { let position_attribute = - gl::GetAttribLocation(program.object, b"aPosition\0".as_ptr() as *const GLchar); + gl::GetAttribLocation(program.object, c"aPosition".as_ptr().cast()); ck(); let transform_uniform = - gl::GetUniformLocation(program.object, b"uTransform\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uTransform".as_ptr().cast()); ck(); let translation_uniform = - gl::GetUniformLocation(program.object, b"uTranslation\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uTranslation".as_ptr().cast()); ck(); - let tex_transform_uniform = gl::GetUniformLocation( - program.object, - b"uTexTransform\0".as_ptr() as *const GLchar, - ); + let tex_transform_uniform = + gl::GetUniformLocation(program.object, c"uTexTransform".as_ptr().cast()); ck(); - let tex_translation_uniform = gl::GetUniformLocation( - program.object, - b"uTexTranslation\0".as_ptr() as *const GLchar, - ); + let tex_translation_uniform = + gl::GetUniformLocation(program.object, c"uTexTranslation".as_ptr().cast()); ck(); - let source_uniform = - gl::GetUniformLocation(program.object, b"uSource\0".as_ptr() as *const GLchar); + let source_uniform = gl::GetUniformLocation(program.object, c"uSource".as_ptr().cast()); ck(); BlitProgram { program, @@ -883,49 +878,36 @@ impl GridProgram { let program = Program::new(vertex_shader, fragment_shader); unsafe { let position_attribute = - gl::GetAttribLocation(program.object, b"aPosition\0".as_ptr() as *const GLchar); + gl::GetAttribLocation(program.object, c"aPosition".as_ptr().cast()); ck(); let transform_uniform = - gl::GetUniformLocation(program.object, b"uTransform\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uTransform".as_ptr().cast()); ck(); let translation_uniform = - gl::GetUniformLocation(program.object, b"uTranslation\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uTranslation".as_ptr().cast()); ck(); - let tex_transform_uniform = gl::GetUniformLocation( - program.object, - b"uTexTransform\0".as_ptr() as *const GLchar, - ); + let tex_transform_uniform = + gl::GetUniformLocation(program.object, c"uTexTransform".as_ptr().cast()); ck(); - let tex_translation_uniform = gl::GetUniformLocation( - program.object, - b"uTexTranslation\0".as_ptr() as *const GLchar, - ); + let tex_translation_uniform = + gl::GetUniformLocation(program.object, c"uTexTranslation".as_ptr().cast()); ck(); - let gridline_color_uniform = gl::GetUniformLocation( - program.object, - b"uGridlineColor\0".as_ptr() as *const GLchar, - ); + let gridline_color_uniform = + gl::GetUniformLocation(program.object, c"uGridlineColor".as_ptr().cast()); ck(); let bg_color_uniform = - gl::GetUniformLocation(program.object, b"uBGColor\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uBGColor".as_ptr().cast()); ck(); - let radius_uniform = - gl::GetUniformLocation(program.object, b"uRadius\0".as_ptr() as *const GLchar); + let radius_uniform = gl::GetUniformLocation(program.object, c"uRadius".as_ptr().cast()); ck(); - let camera_position_uniform = gl::GetUniformLocation( - program.object, - b"uCameraPosition\0".as_ptr() as *const GLchar, - ); + let camera_position_uniform = + gl::GetUniformLocation(program.object, c"uCameraPosition".as_ptr().cast()); ck(); - let light_position_uniform = gl::GetUniformLocation( - program.object, - b"uLightPosition\0".as_ptr() as *const GLchar, - ); + let light_position_uniform = + gl::GetUniformLocation(program.object, c"uLightPosition".as_ptr().cast()); ck(); - let sphere_position_uniform = gl::GetUniformLocation( - program.object, - b"uSpherePosition\0".as_ptr() as *const GLchar, - ); + let sphere_position_uniform = + gl::GetUniformLocation(program.object, c"uSpherePosition".as_ptr().cast()); ck(); GridProgram { program, @@ -985,55 +967,42 @@ impl CheckProgram { let program = Program::new(vertex_shader, fragment_shader); unsafe { let position_attribute = - gl::GetAttribLocation(program.object, b"aPosition\0".as_ptr() as *const GLchar); + gl::GetAttribLocation(program.object, c"aPosition".as_ptr().cast()); ck(); let transform_uniform = - gl::GetUniformLocation(program.object, b"uTransform\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uTransform".as_ptr().cast()); ck(); let translation_uniform = - gl::GetUniformLocation(program.object, b"uTranslation\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uTranslation".as_ptr().cast()); ck(); - let tex_transform_uniform = gl::GetUniformLocation( - program.object, - b"uTexTransform\0".as_ptr() as *const GLchar, - ); + let tex_transform_uniform = + gl::GetUniformLocation(program.object, c"uTexTransform".as_ptr().cast()); ck(); - let tex_translation_uniform = gl::GetUniformLocation( - program.object, - b"uTexTranslation\0".as_ptr() as *const GLchar, - ); + let tex_translation_uniform = + gl::GetUniformLocation(program.object, c"uTexTranslation".as_ptr().cast()); ck(); let rotation_uniform = - gl::GetUniformLocation(program.object, b"uRotation\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uRotation".as_ptr().cast()); ck(); let color_a_uniform = - gl::GetUniformLocation(program.object, b"uColorA\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uColorA".as_ptr().cast()); ck(); let color_b_uniform = - gl::GetUniformLocation(program.object, b"uColorB\0".as_ptr() as *const GLchar); + gl::GetUniformLocation(program.object, c"uColorB".as_ptr().cast()); ck(); - let viewport_origin_uniform = gl::GetUniformLocation( - program.object, - b"uViewportOrigin\0".as_ptr() as *const GLchar, - ); + let viewport_origin_uniform = + gl::GetUniformLocation(program.object, c"uViewportOrigin".as_ptr().cast()); ck(); - let radius_uniform = - gl::GetUniformLocation(program.object, b"uRadius\0".as_ptr() as *const GLchar); + let radius_uniform = gl::GetUniformLocation(program.object, c"uRadius".as_ptr().cast()); ck(); - let camera_position_uniform = gl::GetUniformLocation( - program.object, - b"uCameraPosition\0".as_ptr() as *const GLchar, - ); + let camera_position_uniform = + gl::GetUniformLocation(program.object, c"uCameraPosition".as_ptr().cast()); ck(); - let light_position_uniform = gl::GetUniformLocation( - program.object, - b"uLightPosition\0".as_ptr() as *const GLchar, - ); + let light_position_uniform = + gl::GetUniformLocation(program.object, c"uLightPosition".as_ptr().cast()); ck(); - let sphere_position_uniform = gl::GetUniformLocation( - program.object, - b"uSpherePosition\0".as_ptr() as *const GLchar, - ); + let sphere_position_uniform = + gl::GetUniformLocation(program.object, c"uSpherePosition".as_ptr().cast()); ck(); CheckProgram { program, diff --git a/src/platform/generic/egl/context.rs b/src/platform/generic/egl/context.rs index 86c43dfc..34e12dd8 100644 --- a/src/platform/generic/egl/context.rs +++ b/src/platform/generic/egl/context.rs @@ -16,7 +16,7 @@ use crate::{Gl, SurfaceInfo}; use std::ffi::CString; use std::mem; -use std::os::raw::{c_char, c_void}; +use std::os::raw::c_void; use std::ptr; use std::thread; @@ -593,7 +593,7 @@ pub(crate) unsafe fn egl_config_from_id( pub(crate) fn get_proc_address(symbol_name: &str) -> *const c_void { EGL_FUNCTIONS.with(|egl| unsafe { let symbol_name: CString = CString::new(symbol_name).unwrap(); - egl.GetProcAddress(symbol_name.as_ptr() as *const u8 as *const c_char) as *const c_void + egl.GetProcAddress(symbol_name.as_ptr()).cast() }) } diff --git a/src/platform/generic/egl/device.rs b/src/platform/generic/egl/device.rs index 31f34aa1..cc0c8897 100644 --- a/src/platform/generic/egl/device.rs +++ b/src/platform/generic/egl/device.rs @@ -4,17 +4,15 @@ use crate::egl::Egl; -use std::ffi::CString; +use std::ffi::{CStr, CString}; use std::mem; -use std::os::raw::{c_char, c_void}; +use std::os::raw::c_void; #[cfg(not(target_os = "windows"))] use libc::{dlopen, dlsym, RTLD_LAZY}; #[cfg(target_os = "windows")] use winapi::shared::minwindef::HMODULE; #[cfg(target_os = "windows")] -use winapi::shared::ntdef::LPCSTR; -#[cfg(target_os = "windows")] use winapi::um::libloaderapi; thread_local! { @@ -25,7 +23,7 @@ thread_local! { lazy_static! { static ref EGL_LIBRARY: EGLLibraryWrapper = { unsafe { - let module = libloaderapi::LoadLibraryA(&b"libEGL.dll\0"[0] as *const u8 as LPCSTR); + let module = libloaderapi::LoadLibraryA(c"libEGL.dll".as_ptr()); EGLLibraryWrapper(module) } }; @@ -34,7 +32,7 @@ lazy_static! { #[cfg(not(any(target_os = "windows", target_os = "macos")))] lazy_static! { static ref EGL_LIBRARY: EGLLibraryWrapper = { - for soname in [b"libEGL.so.1\0".as_ptr(), b"libEGL.so\0".as_ptr()] { + for soname in [c"libEGL.so.1".as_ptr(), c"libEGL.so".as_ptr()] { unsafe { let handle = dlopen(soname as *const _, RTLD_LAZY); if !handle.is_null() { @@ -58,8 +56,8 @@ unsafe impl Sync for EGLLibraryWrapper {} fn get_proc_address(symbol_name: &str) -> *const c_void { unsafe { let symbol_name: CString = CString::new(symbol_name).unwrap(); - let symbol_ptr = symbol_name.as_ptr() as *const u8 as LPCSTR; - libloaderapi::GetProcAddress(EGL_LIBRARY.0, symbol_ptr) as *const c_void + let symbol_ptr = symbol_name.as_ptr(); + libloaderapi::GetProcAddress(EGL_LIBRARY.0, symbol_ptr).cast() } } @@ -67,12 +65,11 @@ fn get_proc_address(symbol_name: &str) -> *const c_void { fn get_proc_address(symbol_name: &str) -> *const c_void { unsafe { let symbol_name: CString = CString::new(symbol_name).unwrap(); - let symbol_ptr = symbol_name.as_ptr() as *const u8 as *const c_char; - dlsym(EGL_LIBRARY.0, symbol_ptr) as *const c_void + let symbol_ptr = symbol_name.as_ptr(); + dlsym(EGL_LIBRARY.0, symbol_ptr).cast_const() } } -pub(crate) unsafe fn lookup_egl_extension(name: &'static [u8]) -> *mut c_void { - EGL_FUNCTIONS - .with(|egl| mem::transmute(egl.GetProcAddress(&name[0] as *const u8 as *const c_char))) +pub(crate) unsafe fn lookup_egl_extension(name: &CStr) -> *mut c_void { + EGL_FUNCTIONS.with(|egl| mem::transmute(egl.GetProcAddress(name.as_ptr()))) } diff --git a/src/platform/generic/egl/ffi.rs b/src/platform/generic/egl/ffi.rs index 310e2c6e..8ed4d110 100644 --- a/src/platform/generic/egl/ffi.rs +++ b/src/platform/generic/egl/ffi.rs @@ -82,15 +82,15 @@ lazy_static! { use std::mem::transmute as cast; unsafe { EGLExtensionFunctions { - CreateImageKHR: cast(get(b"eglCreateImageKHR\0")), - DestroyImageKHR: cast(get(b"eglDestroyImageKHR\0")), - ImageTargetTexture2DOES: cast(get(b"glEGLImageTargetTexture2DOES\0")), + CreateImageKHR: cast(get(c"eglCreateImageKHR")), + DestroyImageKHR: cast(get(c"eglDestroyImageKHR")), + ImageTargetTexture2DOES: cast(get(c"glEGLImageTargetTexture2DOES")), - CreateDeviceANGLE: cast(get(b"eglCreateDeviceANGLE\0")), - GetNativeClientBufferANDROID: cast(get(b"eglGetNativeClientBufferANDROID\0")), - QueryDeviceAttribEXT: cast(get(b"eglQueryDeviceAttribEXT\0")), - QueryDisplayAttribEXT: cast(get(b"eglQueryDisplayAttribEXT\0")), - QuerySurfacePointerANGLE: cast(get(b"eglQuerySurfacePointerANGLE\0")), + CreateDeviceANGLE: cast(get(c"eglCreateDeviceANGLE")), + GetNativeClientBufferANDROID: cast(get(c"eglGetNativeClientBufferANDROID")), + QueryDeviceAttribEXT: cast(get(c"eglQueryDeviceAttribEXT")), + QueryDisplayAttribEXT: cast(get(c"eglQueryDisplayAttribEXT")), + QuerySurfacePointerANGLE: cast(get(c"eglQuerySurfacePointerANGLE")), } } }; diff --git a/src/platform/windows/wgl/context.rs b/src/platform/windows/wgl/context.rs index 4f180163..dc8c31b6 100644 --- a/src/platform/windows/wgl/context.rs +++ b/src/platform/windows/wgl/context.rs @@ -148,7 +148,7 @@ pub struct NativeContext(pub HGLRC); thread_local! { static OPENGL_LIBRARY: HMODULE = { unsafe { - libloaderapi::LoadLibraryA(&b"opengl32.dll\0"[0] as *const u8 as LPCSTR) + libloaderapi::LoadLibraryA(c"opengl32.dll".as_ptr()) } }; } @@ -640,7 +640,7 @@ impl NativeContext { fn extension_loader_thread() -> WGLExtensionFunctions { unsafe { let instance = libloaderapi::GetModuleHandleA(ptr::null_mut()); - let window_class_name = &b"SurfmanFalseWindow\0"[0] as *const u8 as LPCSTR; + let window_class_name = c"SurfmanFalseWindow".as_ptr(); let window_class = WNDCLASSA { style: CS_OWNDC, lpfnWndProc: Some(extension_loader_window_proc), @@ -657,9 +657,14 @@ fn extension_loader_thread() -> WGLExtensionFunctions { assert_ne!(window_class_atom, 0); let mut extension_functions = WGLExtensionFunctions::default(); + // The `lpClassName` parameter of `CreateWindowExA()` takes either + // a pointer to a null-terminated c string, or an `ATOM` / `u16` encoded + // in the lower bytes of the pointer type. We do the latter by forcing an + // `as` cast of the ATOM to the pointer type `LPCSTR`. + let lpClassName = window_class_atom as LPCSTR; let window = winuser::CreateWindowExA( 0, - window_class_atom as LPCSTR, + lpClassName, window_class_name, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, @@ -732,9 +737,8 @@ extern "system" fn extension_loader_window_proc( let create_struct = lParam as *mut CREATESTRUCTA; let wgl_extension_functions = (*create_struct).lpCreateParams as *mut WGLExtensionFunctions; - (*wgl_extension_functions).GetExtensionsStringARB = mem::transmute( - wglGetProcAddress(&b"wglGetExtensionsStringARB\0"[0] as *const u8 as LPCSTR), - ); + (*wgl_extension_functions).GetExtensionsStringARB = + mem::transmute(wglGetProcAddress(c"wglGetExtensionsStringARB".as_ptr())); let extensions = match (*wgl_extension_functions).GetExtensionsStringARB { Some(wglGetExtensionsStringARB) => { CStr::from_ptr(wglGetExtensionsStringARB(dc)).to_string_lossy() @@ -748,44 +752,43 @@ extern "system" fn extension_loader_window_proc( (*wgl_extension_functions).pixel_format_functions = Some(WGLPixelFormatExtensionFunctions { ChoosePixelFormatARB: mem::transmute(wglGetProcAddress( - &b"wglChoosePixelFormatARB\0"[0] as *const u8 as LPCSTR, + c"wglChoosePixelFormatARB".as_ptr(), )), GetPixelFormatAttribivARB: mem::transmute(wglGetProcAddress( - &b"wglGetPixelFormatAttribivARB\0"[0] as *const u8 as LPCSTR, + c"wglGetPixelFormatAttribivARB".as_ptr(), )), }); continue; } if extension == "WGL_ARB_create_context" { - (*wgl_extension_functions).CreateContextAttribsARB = - mem::transmute(wglGetProcAddress( - &b"wglCreateContextAttribsARB\0"[0] as *const u8 as LPCSTR, - )); + (*wgl_extension_functions).CreateContextAttribsARB = mem::transmute( + wglGetProcAddress(c"wglCreateContextAttribsARB".as_ptr()), + ); continue; } if extension == "WGL_NV_DX_interop" { (*wgl_extension_functions).dx_interop_functions = Some(WGLDXInteropExtensionFunctions { DXCloseDeviceNV: mem::transmute(wglGetProcAddress( - &b"wglDXCloseDeviceNV\0"[0] as *const u8 as LPCSTR, + c"wglDXCloseDeviceNV".as_ptr(), )), DXLockObjectsNV: mem::transmute(wglGetProcAddress( - &b"wglDXLockObjectsNV\0"[0] as *const u8 as LPCSTR, + c"wglDXLockObjectsNV".as_ptr(), )), DXOpenDeviceNV: mem::transmute(wglGetProcAddress( - &b"wglDXOpenDeviceNV\0"[0] as *const u8 as LPCSTR, + c"wglDXOpenDeviceNV".as_ptr(), )), DXRegisterObjectNV: mem::transmute(wglGetProcAddress( - &b"wglDXRegisterObjectNV\0"[0] as *const u8 as LPCSTR, + c"wglDXRegisterObjectNV".as_ptr(), )), DXSetResourceShareHandleNV: mem::transmute(wglGetProcAddress( - &b"wglDXSetResourceShareHandleNV\0"[0] as *const u8 as LPCSTR, + c"wglDXSetResourceShareHandleNV".as_ptr(), )), DXUnlockObjectsNV: mem::transmute(wglGetProcAddress( - &b"wglDXUnlockObjectsNV\0"[0] as *const u8 as LPCSTR, + c"wglDXUnlockObjectsNV".as_ptr(), )), DXUnregisterObjectNV: mem::transmute(wglGetProcAddress( - &b"wglDXUnregisterObjectNV\0"[0] as *const u8 as LPCSTR, + c"wglDXUnregisterObjectNV".as_ptr(), )), }); continue; @@ -872,7 +875,7 @@ fn get_proc_address(symbol_name: &str) -> *const c_void { unsafe { // https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions#Windows let symbol_name: CString = CString::new(symbol_name).unwrap(); - let symbol_ptr = symbol_name.as_ptr() as *const u8 as LPCSTR; + let symbol_ptr = symbol_name.as_ptr(); let addr = wglGetProcAddress(symbol_ptr) as *const c_void; if !addr.is_null() { return addr; diff --git a/src/platform/windows/wgl/device.rs b/src/platform/windows/wgl/device.rs index 64c8794a..e014b435 100644 --- a/src/platform/windows/wgl/device.rs +++ b/src/platform/windows/wgl/device.rs @@ -6,6 +6,7 @@ use super::connection::Connection; use super::context::WGL_EXTENSION_FUNCTIONS; use crate::{Error, GLApi}; +use std::ffi::CStr; use std::marker::PhantomData; use std::mem; use std::os::raw::{c_int, c_void}; @@ -14,7 +15,7 @@ use std::sync::mpsc::{self, Sender}; use std::thread::{self, JoinHandle}; use winapi::shared::dxgi::{IDXGIAdapter, IDXGIDevice}; use winapi::shared::minwindef::{self, FALSE, UINT}; -use winapi::shared::ntdef::{HANDLE, LPCSTR}; +use winapi::shared::ntdef::HANDLE; use winapi::shared::windef::{HBRUSH, HDC, HWND}; use winapi::shared::winerror::{self, S_OK}; use winapi::um::d3d11::{D3D11CreateDevice, ID3D11Device, ID3D11DeviceContext, D3D11_SDK_VERSION}; @@ -28,8 +29,8 @@ pub(crate) const HIDDEN_WINDOW_SIZE: c_int = 16; const INTEL_PCI_ID: UINT = 0x8086; -static NVIDIA_GPU_SELECT_SYMBOL: &[u8] = b"NvOptimusEnablement\0"; -static AMD_GPU_SELECT_SYMBOL: &[u8] = b"AmdPowerXpressRequestHighPerformance\0"; +static NVIDIA_GPU_SELECT_SYMBOL: &CStr = c"NvOptimusEnablement"; +static AMD_GPU_SELECT_SYMBOL: &CStr = c"AmdPowerXpressRequestHighPerformance"; /// Represents a hardware display adapter that can be used for rendering (including the CPU). /// @@ -76,14 +77,12 @@ impl Adapter { unsafe { let current_module = libloaderapi::GetModuleHandleA(ptr::null()); assert!(!current_module.is_null()); - let nvidia_gpu_select_variable: *mut i32 = libloaderapi::GetProcAddress( - current_module, - NVIDIA_GPU_SELECT_SYMBOL.as_ptr() as LPCSTR, - ) as *mut i32; - let amd_gpu_select_variable: *mut i32 = libloaderapi::GetProcAddress( - current_module, - AMD_GPU_SELECT_SYMBOL.as_ptr() as LPCSTR, - ) as *mut i32; + let nvidia_gpu_select_variable: *mut i32 = + libloaderapi::GetProcAddress(current_module, NVIDIA_GPU_SELECT_SYMBOL.as_ptr()) + as *mut i32; + let amd_gpu_select_variable: *mut i32 = + libloaderapi::GetProcAddress(current_module, AMD_GPU_SELECT_SYMBOL.as_ptr()) + as *mut i32; if nvidia_gpu_select_variable.is_null() || amd_gpu_select_variable.is_null() { println!( "surfman: Could not find the NVIDIA and/or AMD GPU selection symbols. \ @@ -308,7 +307,7 @@ impl HiddenWindow { fn thread(sender: Sender) { unsafe { let instance = libloaderapi::GetModuleHandleA(ptr::null_mut()); - let window_class_name = &b"SurfmanHiddenWindow\0"[0] as *const u8 as LPCSTR; + let window_class_name = c"SurfmanHiddenWindow".as_ptr(); let mut window_class = mem::zeroed(); if winuser::GetClassInfoA(instance, window_class_name, &mut window_class) == FALSE { window_class = WNDCLASSA {