Skip to content

Commit 912956c

Browse files
committed
add cstr support
1 parent 9a58fa8 commit 912956c

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

dtrace-parser/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl Integer {
156156
pub enum DataType {
157157
Integer(Integer),
158158
Pointer(Integer),
159+
CString,
159160
String,
160161
}
161162

@@ -246,7 +247,7 @@ impl DataType {
246247
match self {
247248
DataType::Integer(int) => int.to_c_type(),
248249
DataType::Pointer(int) => format!("{}*", int.to_c_type()),
249-
DataType::String => String::from("char*"),
250+
DataType::CString | DataType::String => String::from("char*"),
250251
}
251252
}
252253

@@ -255,7 +256,7 @@ impl DataType {
255256
match self {
256257
DataType::Integer(int) => int.to_rust_ffi_type(),
257258
DataType::Pointer(int) => format!("*const {}", int.to_rust_ffi_type()),
258-
DataType::String => format!("*const {RUST_TYPE_PREFIX}char"),
259+
DataType::CString | DataType::String => format!("*const {RUST_TYPE_PREFIX}char"),
259260
}
260261
}
261262

@@ -265,6 +266,7 @@ impl DataType {
265266
DataType::Integer(int) => int.to_rust_type(),
266267
DataType::Pointer(int) => format!("*const {}", int.to_rust_type()),
267268
DataType::String => String::from("&str"),
269+
DataType::CString => String::from("&::core::ffi::CStr"),
268270
}
269271
}
270272
}

probe-test-attr/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ mod test {
8282

8383
/// Constant pointers to integer types are also supported
8484
fn work_with_pointer(_buffer: *const u8, _: u64) {}
85+
86+
/// Avoid expensive string allocation with cstring support
87+
fn cstring(_: &CStr, _: CString) {}
8588
}
8689

8790
fn main() {
@@ -106,5 +109,6 @@ fn main() {
106109
test::arg_as_tuple!(|| (arg.x, &arg.buffer[..]));
107110
test::not_json_serializable!(|| Whoops::NoBueno(0));
108111
test::work_with_pointer!(|| (buffer.as_ptr(), buffer.len() as u64));
112+
test::cstring!(|| (c"hello world", c"and when owned".to_owned()));
109113
}
110114
}

usdt-attr-macro/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ fn is_simple_type(ident: &syn::Ident) -> bool {
303303
| "i64"
304304
| "String"
305305
| "str"
306+
| "CString"
307+
| "CStr"
306308
| "usize"
307309
| "isize"
308310
)
@@ -363,6 +365,8 @@ fn data_type_from_path(path: &syn::Path, pointer: bool) -> DataType {
363365
}))
364366
} else if path.is_ident("String") || path.is_ident("str") {
365367
DataType::Native(DType::String)
368+
} else if path.is_ident("CString") || path.is_ident("CStr") {
369+
DataType::Native(DType::CString)
366370
} else if path.is_ident("isize") {
367371
DataType::Native(variant(Integer {
368372
sign: Sign::Signed,
@@ -462,6 +466,10 @@ mod tests {
462466
#[case("String", DType::String)]
463467
#[case("&&str", DType::String)]
464468
#[case("&String", DType::String)]
469+
#[case("&CStr", DType::CString)]
470+
#[case("CString", DType::CString)]
471+
#[case("&&CStr", DType::CString)]
472+
#[case("&CString", DType::CString)]
465473
fn test_parse_probe_argument_native(#[case] name: &str, #[case] ty: dtrace_parser::DataType) {
466474
let arg = syn::parse_str(name).unwrap();
467475
let out = parse_probe_argument(&arg, 0, 0).unwrap();

usdt-impl/src/common.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ pub fn construct_type_check(
6767
}
6868
}
6969
DataType::Native(dtrace_parser::DataType::String) => quote! { _: impl AsRef<str> },
70+
DataType::Native(dtrace_parser::DataType::CString) => {
71+
quote! { _: impl AsRef<::core::ffi::CStr> }
72+
}
7073
_ => {
7174
let arg = typ.to_rust_type();
7275
quote! { _: impl ::std::borrow::Borrow<#arg> }
@@ -181,6 +184,10 @@ fn asm_type_convert(typ: &DataType, input: TokenStream) -> (TokenStream, TokenSt
181184
},
182185
quote! { .as_ptr() as usize },
183186
),
187+
DataType::Native(dtrace_parser::DataType::CString) => (
188+
quote! { #input.as_ref() as &::core::ffi::CStr },
189+
quote! { .as_ptr() as usize },
190+
),
184191
DataType::Native(dtrace_parser::DataType::String) => (
185192
quote! {
186193
[(#input.as_ref() as &str).as_bytes(), &[0_u8]].concat()
@@ -397,5 +404,15 @@ mod tests {
397404
quote! { [(foo.as_ref() as &str).as_bytes(), &[0_u8]].concat() }.to_string()
398405
);
399406
assert_eq!(post.to_string(), quote! { .as_ptr() as usize }.to_string());
407+
408+
let (out, post) = asm_type_convert(
409+
&DataType::Native(dtrace_parser::DataType::CString),
410+
TokenStream::from_str("foo").unwrap(),
411+
);
412+
assert_eq!(
413+
out.to_string(),
414+
quote! { foo.as_ref() as &::core::ffi::CStr }.to_string()
415+
);
416+
assert_eq!(post.to_string(), quote! { .as_ptr() as usize }.to_string());
400417
}
401418
}

0 commit comments

Comments
 (0)