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 30, 2024
1 parent 1b24430 commit c332ede
Show file tree
Hide file tree
Showing 11 changed files with 1,148 additions and 3,001 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
- name: Check ash, ash-window and ash-rewrite
run: cargo check -p ash -p ash-rewrite -p ash-window --all-features
- name: Check ash with no_std
Expand Down
3 changes: 1 addition & 2 deletions ash-examples/src/bin/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::default::Default;
use std::error::Error;
use std::ffi;
use std::io::Cursor;
use std::mem;
use std::os::raw::c_void;
Expand Down Expand Up @@ -567,7 +566,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.create_pipeline_layout(&layout_create_info, None)
.unwrap();

let shader_entry_name = ffi::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
Expand Up @@ -2,7 +2,6 @@

use std::default::Default;
use std::error::Error;
use std::ffi;
use std::io::Cursor;
use std::mem;

Expand Down Expand Up @@ -229,7 +228,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.create_pipeline_layout(&layout_create_info, None)
.unwrap();

let shader_entry_name = ffi::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 @@ -212,11 +212,9 @@ impl ExampleBase {
.build(&event_loop)
.unwrap();
let entry = Entry::linked();
let app_name = ffi::CStr::from_bytes_with_nul_unchecked(b"VulkanTriangle\0");
let app_name = c"VulkanTriangle";

let layer_names = [ffi::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, features = ["std"] }
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 @@ -216,7 +216,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 = ffi::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 @@ -328,7 +328,7 @@ impl crate::StaticFn {
{
Ok(Self {
get_instance_proc_addr: unsafe {
let cname = ffi::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 c332ede

Please sign in to comment.