1
- /// A macro for [`ZStr `] literals.
1
+ /// A macro for [`CStr `] literals.
2
2
///
3
3
/// This can make passing string literals to rustix APIs more efficient, since
4
4
/// most underlying system calls with string arguments expect NUL-terminated
5
- /// strings, and passing strings to rustix as `ZStr `s means that rustix doesn't
5
+ /// strings, and passing strings to rustix as `CStr `s means that rustix doesn't
6
6
/// need to copy them into a separate buffer to NUL-terminate them.
7
7
///
8
- /// [`ZStr `]: crate::ffi::ZStr
8
+ /// [`CStr `]: crate::ffi::CStr
9
9
///
10
10
/// # Examples
11
11
///
12
12
/// ```rust,no_run
13
13
/// # #[cfg(feature = "fs")]
14
14
/// # fn main() -> rustix::io::Result<()> {
15
15
/// use rustix::fs::{cwd, statat, AtFlags};
16
- /// use rustix::zstr ;
16
+ /// use rustix::cstr ;
17
17
///
18
- /// let metadata = statat(cwd(), zstr !("test.txt"), AtFlags::empty())?;
18
+ /// let metadata = statat(cwd(), cstr !("test.txt"), AtFlags::empty())?;
19
19
/// # Ok(())
20
20
/// # }
21
21
/// # #[cfg(not(feature = "fs"))]
22
22
/// # fn main() {}
23
23
/// ```
24
24
#[ allow( unused_macros) ]
25
25
#[ macro_export]
26
- macro_rules! zstr {
26
+ macro_rules! cstr {
27
27
( $str: literal) => { {
28
28
// Check for NUL manually, to ensure safety.
29
29
//
@@ -35,7 +35,7 @@ macro_rules! zstr {
35
35
// constant-fold away.
36
36
assert!(
37
37
!$str. bytes( ) . any( |b| b == b'\0' ) ,
38
- "zstr argument contains embedded NUL bytes" ,
38
+ "cstr argument contains embedded NUL bytes" ,
39
39
) ;
40
40
41
41
#[ allow( unsafe_code, unused_unsafe) ]
@@ -47,30 +47,30 @@ macro_rules! zstr {
47
47
// Safety: We have manually checked that the string does not contain
48
48
// embedded NULs above, and we append or own NUL terminator here.
49
49
unsafe {
50
- $crate:: ffi:: ZStr :: from_bytes_with_nul_unchecked( concat!( $str, "\0 " ) . as_bytes( ) )
50
+ $crate:: ffi:: CStr :: from_bytes_with_nul_unchecked( concat!( $str, "\0 " ) . as_bytes( ) )
51
51
}
52
52
}
53
53
} } ;
54
54
}
55
55
56
56
#[ test]
57
- fn test_zstr ( ) {
58
- use crate :: ffi:: ZString ;
57
+ fn test_cstr ( ) {
58
+ use crate :: ffi:: CString ;
59
59
use alloc:: borrow:: ToOwned ;
60
- assert_eq ! ( zstr !( "" ) , & * ZString :: new( "" ) . unwrap( ) ) ;
61
- assert_eq ! ( zstr !( "" ) . to_owned( ) , ZString :: new( "" ) . unwrap( ) ) ;
62
- assert_eq ! ( zstr !( "hello" ) , & * ZString :: new( "hello" ) . unwrap( ) ) ;
63
- assert_eq ! ( zstr !( "hello" ) . to_owned( ) , ZString :: new( "hello" ) . unwrap( ) ) ;
60
+ assert_eq ! ( cstr !( "" ) , & * CString :: new( "" ) . unwrap( ) ) ;
61
+ assert_eq ! ( cstr !( "" ) . to_owned( ) , CString :: new( "" ) . unwrap( ) ) ;
62
+ assert_eq ! ( cstr !( "hello" ) , & * CString :: new( "hello" ) . unwrap( ) ) ;
63
+ assert_eq ! ( cstr !( "hello" ) . to_owned( ) , CString :: new( "hello" ) . unwrap( ) ) ;
64
64
}
65
65
66
66
#[ test]
67
67
#[ should_panic]
68
- fn test_invalid_zstr ( ) {
69
- let _ = zstr ! ( "hello\0 world" ) ;
68
+ fn test_invalid_cstr ( ) {
69
+ let _ = cstr ! ( "hello\0 world" ) ;
70
70
}
71
71
72
72
#[ test]
73
73
#[ should_panic]
74
- fn test_invalid_empty_zstr ( ) {
75
- let _ = zstr ! ( "\0 " ) ;
74
+ fn test_invalid_empty_cstr ( ) {
75
+ let _ = cstr ! ( "\0 " ) ;
76
76
}
0 commit comments