Skip to content

Commit

Permalink
Use cstr literals where possible (#311)
Browse files Browse the repository at this point in the history
* 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 <jonathan.schwender@huawei.com>

* examples: prefer `ptr::cast()` over as casts

Fixes pedantic clippy lint `ptr_as_ptr`.

Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>

* Add comment explaining cast of `u16` to pointer.

Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>

---------

Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>
  • Loading branch information
jschwe authored Aug 29, 2024
1 parent d45f322 commit 1da9c17
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <pcwalton@mimiga.net>",
Expand Down
4 changes: 2 additions & 2 deletions examples/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 3 additions & 4 deletions examples/offscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vertex>() * 3,
));

Expand Down Expand Up @@ -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,
Expand Down
119 changes: 44 additions & 75 deletions examples/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/platform/generic/egl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
})
}

Expand Down
23 changes: 10 additions & 13 deletions src/platform/generic/egl/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand All @@ -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)
}
};
Expand All @@ -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() {
Expand All @@ -58,21 +56,20 @@ 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()
}
}

#[cfg(not(target_os = "windows"))]
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())))
}
16 changes: 8 additions & 8 deletions src/platform/generic/egl/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
}
}
};
Expand Down
Loading

0 comments on commit 1da9c17

Please sign in to comment.