Skip to content

Commit af05ed2

Browse files
committed
Remove ZStr and ZString.
Use `CStr` and `CString` instead, now that nightly has then in core and alloc, respectively. Fixes #336.
1 parent 67fb489 commit af05ed2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+486
-2306
lines changed

benches/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod suite {
6262
assert_eq!(
6363
libc::fstatat(
6464
libc::AT_FDCWD,
65-
rustix::zstr!("/").as_ptr() as _,
65+
rustix::cstr!("/").as_ptr() as _,
6666
s.as_mut_ptr(),
6767
0
6868
),
@@ -78,7 +78,7 @@ mod suite {
7878

7979
c.bench_function("simple statat cstr", |b| {
8080
b.iter(|| {
81-
statat(cwd(), rustix::zstr!("/"), AtFlags::empty()).unwrap();
81+
statat(cwd(), rustix::cstr!("/"), AtFlags::empty()).unwrap();
8282
})
8383
});
8484
}

build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ fn main() {
1616
// Features only used in no-std configurations.
1717
#[cfg(not(feature = "std"))]
1818
{
19-
use_feature_or_nothing("vec_into_raw_parts");
20-
use_feature_or_nothing("toowned_clone_into");
21-
use_feature_or_nothing("specialization");
22-
use_feature_or_nothing("slice_internals");
2319
use_feature_or_nothing("const_raw_ptr_deref");
2420
}
2521

src/zstr.rs renamed to src/cstr.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
/// A macro for [`ZStr`] literals.
1+
/// A macro for [`CStr`] literals.
22
///
33
/// This can make passing string literals to rustix APIs more efficient, since
44
/// 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
66
/// need to copy them into a separate buffer to NUL-terminate them.
77
///
8-
/// [`ZStr`]: crate::ffi::ZStr
8+
/// [`CStr`]: crate::ffi::CStr
99
///
1010
/// # Examples
1111
///
1212
/// ```rust,no_run
1313
/// # #[cfg(feature = "fs")]
1414
/// # fn main() -> rustix::io::Result<()> {
1515
/// use rustix::fs::{cwd, statat, AtFlags};
16-
/// use rustix::zstr;
16+
/// use rustix::cstr;
1717
///
18-
/// let metadata = statat(cwd(), zstr!("test.txt"), AtFlags::empty())?;
18+
/// let metadata = statat(cwd(), cstr!("test.txt"), AtFlags::empty())?;
1919
/// # Ok(())
2020
/// # }
2121
/// # #[cfg(not(feature = "fs"))]
2222
/// # fn main() {}
2323
/// ```
2424
#[allow(unused_macros)]
2525
#[macro_export]
26-
macro_rules! zstr {
26+
macro_rules! cstr {
2727
($str:literal) => {{
2828
// Check for NUL manually, to ensure safety.
2929
//
@@ -35,7 +35,7 @@ macro_rules! zstr {
3535
// constant-fold away.
3636
assert!(
3737
!$str.bytes().any(|b| b == b'\0'),
38-
"zstr argument contains embedded NUL bytes",
38+
"cstr argument contains embedded NUL bytes",
3939
);
4040

4141
#[allow(unsafe_code, unused_unsafe)]
@@ -47,30 +47,30 @@ macro_rules! zstr {
4747
// Safety: We have manually checked that the string does not contain
4848
// embedded NULs above, and we append or own NUL terminator here.
4949
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())
5151
}
5252
}
5353
}};
5454
}
5555

5656
#[test]
57-
fn test_zstr() {
58-
use crate::ffi::ZString;
57+
fn test_cstr() {
58+
use crate::ffi::CString;
5959
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());
6464
}
6565

6666
#[test]
6767
#[should_panic]
68-
fn test_invalid_zstr() {
69-
let _ = zstr!("hello\0world");
68+
fn test_invalid_cstr() {
69+
let _ = cstr!("hello\0world");
7070
}
7171

7272
#[test]
7373
#[should_panic]
74-
fn test_invalid_empty_zstr() {
75-
let _ = zstr!("\0");
74+
fn test_invalid_empty_cstr() {
75+
let _ = cstr!("\0");
7676
}

src/ffi/mod.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
//! Utilities related to FFI bindings.
22
3-
/// Minimal and unoptimized `strlen` implementation.
4-
///
5-
/// TODO: Optimize this by reading a `usize` at a time.
63
#[cfg(not(feature = "std"))]
7-
#[allow(unsafe_code)]
8-
unsafe fn strlen(mut s: *const u8) -> usize {
9-
let mut len = 0;
10-
while *s != b'\0' {
11-
len += 1;
12-
s = s.add(1);
13-
}
14-
len
15-
}
16-
17-
#[cfg(not(feature = "std"))]
18-
mod z_str;
19-
20-
#[cfg(not(feature = "std"))]
21-
pub use z_str::{FromBytesWithNulError, FromVecWithNulError, NulError, ZStr, ZString};
4+
pub use core::ffi::{CStr, CString, FromBytesWithNulError, NulError};
225

236
#[cfg(feature = "std")]
24-
pub use std::ffi::{CStr as ZStr, CString as ZString, FromBytesWithNulError, NulError};
7+
pub use std::ffi::{CStr, CString, FromBytesWithNulError, NulError};

0 commit comments

Comments
 (0)