Skip to content

Commit

Permalink
generator: Emit new c"" CStr literals
Browse files Browse the repository at this point in the history
These will land in Rust 1.76 and automatically append a `\0` terminator
in the compiler without having to have a checked or `unsafe`-unchecked
constructor on `CStr`.

Hacking in an invalid `\0` anywhere in the string is disallowed with a
compiler error.

Note that `proc-macro`, and by extension `proc-macro2` only has support
for parsing this literal, but not for emitting it yet.
  • Loading branch information
MarijnS95 committed Mar 24, 2024
1 parent 01bfed9 commit 1b9d5be
Show file tree
Hide file tree
Showing 10 changed files with 1,158 additions and 3,545 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
- run: cargo check --workspace --all-targets --all-features

check_msrv:
name: Check ash, ash-window and ash-rewrite MSRV (1.69.0)
name: Check ash, ash-window and ash-rewrite MSRV (1.77.0)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@1.69.0
- uses: dtolnay/rust-toolchain@1.77.0
- run: cargo check -p ash -p ash-rewrite -p ash-window --all-features

# TODO: add a similar job for the rewrite once that generates code
Expand Down
3 changes: 1 addition & 2 deletions ash-examples/src/bin/texture.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![warn(unused_qualifications)]

use std::default::Default;
use std::ffi::CStr;
use std::io::Cursor;
use std::mem;
use std::os::raw::c_void;
Expand Down Expand Up @@ -566,7 +565,7 @@ fn main() {
.create_pipeline_layout(&layout_create_info, None)
.unwrap();

let shader_entry_name = CStr::from_bytes_with_nul_unchecked(b"main\0");
let shader_entry_name = c"main";
let shader_stage_create_infos = [
vk::PipelineShaderStageCreateInfo {
module: vertex_shader_module,
Expand Down
3 changes: 1 addition & 2 deletions ash-examples/src/bin/triangle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![warn(unused_qualifications)]

use std::default::Default;
use std::ffi::CStr;
use std::io::Cursor;
use std::mem;

Expand Down Expand Up @@ -228,7 +227,7 @@ fn main() {
.create_pipeline_layout(&layout_create_info, None)
.unwrap();

let shader_entry_name = CStr::from_bytes_with_nul_unchecked(b"main\0");
let shader_entry_name = c"main";
let shader_stage_create_infos = [
vk::PipelineShaderStageCreateInfo {
module: vertex_shader_module,
Expand Down
6 changes: 2 additions & 4 deletions ash-examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,9 @@ impl ExampleBase {
.build(&event_loop)
.unwrap();
let entry = Entry::linked();
let app_name = CStr::from_bytes_with_nul_unchecked(b"VulkanTriangle\0");
let app_name = c"VulkanTriangle";

let layer_names = [CStr::from_bytes_with_nul_unchecked(
b"VK_LAYER_KHRONOS_validation\0",
)];
let layer_names = [c"VK_LAYER_KHRONOS_validation"];
let layers_names_raw: Vec<*const c_char> = layer_names
.iter()
.map(|raw_name| raw_name.as_ptr())
Expand Down
2 changes: 1 addition & 1 deletion ash-window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ categories = [
"rendering::graphics-api"
]
edition = "2021"
rust-version = "1.69.0"
rust-version = "1.77.0"

[dependencies]
ash = { path = "../ash", version = "0.37", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion ash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ categories = [
"rendering::graphics-api"
]
edition = "2021"
rust-version = "1.69.0"
rust-version = "1.77.0"

[dependencies]
libloading = { version = "0.8", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions ash/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Entry {
#[inline]
pub unsafe fn try_enumerate_instance_version(&self) -> VkResult<Option<u32>> {
let enumerate_instance_version: Option<vk::PFN_vkEnumerateInstanceVersion> = {
let name = CStr::from_bytes_with_nul_unchecked(b"vkEnumerateInstanceVersion\0");
let name = c"vkEnumerateInstanceVersion";
mem::transmute((self.static_fn.get_instance_proc_addr)(
vk::Instance::null(),
name.as_ptr(),
Expand Down Expand Up @@ -332,7 +332,7 @@ impl vk::StaticFn {
{
Ok(Self {
get_instance_proc_addr: unsafe {
let cname = CStr::from_bytes_with_nul_unchecked(b"vkGetInstanceProcAddr\0");
let cname = c"vkGetInstanceProcAddr";
let val = _f(cname);
if val.is_null() {
return Err(MissingEntryPoint);
Expand Down
Loading

0 comments on commit 1b9d5be

Please sign in to comment.