Skip to content

Commit b9358d7

Browse files
authored
Remove c_str macros (#174)
1 parent 9d3b15c commit b9358d7

File tree

7 files changed

+10
-91
lines changed

7 files changed

+10
-91
lines changed

phper-macros/src/lib.rs

-28
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,6 @@ mod utils;
2424

2525
use proc_macro::TokenStream;
2626

27-
/// C style string end with '\0'.
28-
///
29-
/// # Examples
30-
///
31-
/// ```no_test
32-
/// use std::ffi::CStr;
33-
///
34-
/// assert_eq!(c_str!("foo"), unsafe {
35-
/// CStr::from_ptr("foo\0".as_ptr().cast())
36-
/// });
37-
/// ```
38-
#[proc_macro]
39-
pub fn c_str(input: TokenStream) -> TokenStream {
40-
utils::c_str(input)
41-
}
42-
43-
/// C style string end with '\0'.
44-
///
45-
/// # Examples
46-
///
47-
/// ```no_test
48-
/// assert_eq!(c_str_ptr!("foo"), "foo\0".as_ptr().cast());
49-
/// ```
50-
#[proc_macro]
51-
pub fn c_str_ptr(input: TokenStream) -> TokenStream {
52-
utils::c_str_ptr(input)
53-
}
54-
5527
/// PHP module entry, wrap the `phper::modules::Module` write operation.
5628
///
5729
/// # Examples

phper-macros/src/utils.rs

-20
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,3 @@
77
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
88
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
// See the Mulan PSL v2 for more details.
10-
11-
use proc_macro::TokenStream;
12-
use quote::quote;
13-
use syn::{parse_macro_input, Expr};
14-
15-
pub(crate) fn c_str(input: TokenStream) -> TokenStream {
16-
let input = parse_macro_input!(input as Expr);
17-
let result = quote! {
18-
unsafe { ::std::ffi::CStr::from_ptr(::core::concat!(#input, "\0").as_ptr().cast()) }
19-
};
20-
result.into()
21-
}
22-
23-
pub(crate) fn c_str_ptr(input: TokenStream) -> TokenStream {
24-
let input = parse_macro_input!(input as Expr);
25-
let result = quote! {
26-
::core::concat!(#input, "\0").as_ptr() as *const ::std::os::raw::c_char
27-
};
28-
result.into()
29-
}

phper-macros/tests/integration.rs

-25
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,3 @@
77
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
88
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
// See the Mulan PSL v2 for more details.
10-
11-
use phper_macros::*;
12-
use std::ffi::CStr;
13-
14-
#[test]
15-
fn test_c_str() {
16-
assert_eq!(c_str!("foo"), unsafe {
17-
CStr::from_ptr("foo\0".as_ptr().cast())
18-
});
19-
20-
assert_eq!(
21-
{
22-
#[allow(unused_unsafe)]
23-
unsafe {
24-
c_str!("bar")
25-
}
26-
},
27-
unsafe { CStr::from_ptr("bar\0".as_ptr().cast()) }
28-
);
29-
}
30-
31-
#[test]
32-
fn test_c_str_ptr() {
33-
assert_eq!(c_str_ptr!("foo"), "foo\0".as_ptr().cast());
34-
}

phper/src/ini.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Apis relate to [zend_ini_entry_def].
1212
13-
use crate::{c_str, sys::*};
13+
use crate::sys::*;
1414
use std::{
1515
ffi::{c_int, CStr},
1616
mem::zeroed,
@@ -100,13 +100,7 @@ impl FromIniValue for bool {
100100
#[allow(clippy::useless_conversion)]
101101
fn from_ini_value(name: &str) -> Self {
102102
let s = <Option<&CStr>>::from_ini_value(name);
103-
[
104-
Some(c_str!("1")),
105-
Some(c_str!("true")),
106-
Some(c_str!("on")),
107-
Some(c_str!("On")),
108-
]
109-
.contains(&s)
103+
[Some(c"1"), Some(c"true"), Some(c"on"), Some(c"On")].contains(&s)
110104
}
111105
}
112106

phper/src/modules.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//! Apis relate to [zend_module_entry].
1212
1313
use crate::{
14-
c_str_ptr,
1514
classes::{ClassEntity, InterfaceEntity},
1615
constants::Constant,
1716
errors::Throwable,
@@ -105,10 +104,10 @@ unsafe extern "C" fn module_info(zend_module: *mut zend_module_entry) {
105104

106105
php_info_print_table_start();
107106
if !module.version.as_bytes().is_empty() {
108-
php_info_print_table_row(2, c_str_ptr!("version"), module.version.as_ptr());
107+
php_info_print_table_row(2, c"version", module.version.as_ptr());
109108
}
110109
if !module.author.as_bytes().is_empty() {
111-
php_info_print_table_row(2, c_str_ptr!("authors"), module.author.as_ptr());
110+
php_info_print_table_row(2, c"authors", module.author.as_ptr());
112111
}
113112
for (key, value) in &module.infos {
114113
php_info_print_table_row(2, key.as_ptr(), value.as_ptr());

phper/src/types.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Apis relate to PHP types.
1212
13-
use crate::{c_str, sys::*};
13+
use crate::sys::*;
1414
use derive_more::From;
1515
use std::{
1616
ffi::CStr,
@@ -133,10 +133,10 @@ impl TypeInfo {
133133
let t = get_base_type_by_raw(self.t);
134134

135135
if t == IS_UNDEF {
136-
return c_str!("undef");
136+
return c"undef";
137137
}
138138
if t == IS_REFERENCE {
139-
return c_str!("reference");
139+
return c"reference";
140140
}
141141

142142
let s = zend_get_type_by_const(t as c_int);
@@ -145,10 +145,10 @@ impl TypeInfo {
145145
// Compact with PHP7.
146146
let bs = s.to_bytes();
147147
if bs == b"boolean" {
148-
return c_str!("bool");
148+
return c"bool";
149149
}
150150
if bs == b"integer" {
151-
return c_str!("int");
151+
return c"int";
152152
}
153153

154154
s

tests/integration/src/ini.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// See the Mulan PSL v2 for more details.
1010

1111
use phper::{
12-
c_str,
1312
ini::{ini_get, Policy},
1413
modules::Module,
1514
};
@@ -33,7 +32,7 @@ pub fn integrate(module: &mut Module) {
3332
assert_eq!(ini_get::<f64>("INTEGRATE_INI_DOUBLE"), 200.);
3433
assert_eq!(
3534
ini_get::<Option<&CStr>>("INTEGRATE_INI_STRING"),
36-
Some(c_str!("something"))
35+
Some(c"something")
3736
);
3837
Ok::<_, Infallible>(())
3938
});

0 commit comments

Comments
 (0)