Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Switch back to CStr::from_bytes_with_nul_unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
upsuper committed Feb 25, 2022
1 parent 17454ee commit 8357a00
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cstr"
version = "0.2.9"
version = "0.2.10"
authors = ["Xidorn Quan <[email protected]>"]
description = "Macro for building static CStr reference"
repository = "https://github.com/upsuper/cstr"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A macro for getting `&'static CStr` from literal or identifier.
This macro checks whether the given literal is valid for `CStr`
at compile time, and returns a static reference of `CStr`.

This macro can be used to to initialize constants on Rust 1.46 and above.
This macro can be used to to initialize constants on Rust 1.59 and above.

## Example

Expand Down
13 changes: 2 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This macro checks whether the given literal is valid for `CStr`
//! at compile time, and returns a static reference of `CStr`.
//!
//! This macro can be used to to initialize constants on Rust 1.46 and above.
//! This macro can be used to to initialize constants on Rust 1.59 and above.
//!
//! ## Example
//!
Expand Down Expand Up @@ -37,16 +37,7 @@ struct Error(Span, &'static str);
#[proc_macro]
pub fn cstr(input: RawTokenStream) -> RawTokenStream {
let tokens = match build_byte_str(input.into()) {
// We can't use `&*ptr` to convert the raw pointer to reference, because as of Rust 1.46,
// dereferencing raw pointer in constants is unstable.
// This is being tracked in https://github.com/rust-lang/rust/issues/51911
// So we explicitly disable the clippy lint for this expression.
Ok(s) => quote!(unsafe {
#[allow(clippy::transmute_ptr_to_ref)]
::std::mem::transmute::<_, &::std::ffi::CStr>(
#s as *const [u8] as *const ::std::ffi::CStr
)
}),
Ok(s) => quote!(unsafe { ::std::ffi::CStr::from_bytes_with_nul_unchecked(#s) }),
Err(Error(span, msg)) => quote_spanned!(span => compile_error!(#msg)),
};
tokens.into()
Expand Down

0 comments on commit 8357a00

Please sign in to comment.