Skip to content

Commit b437157

Browse files
authored
Remove ZStr and ZString. (#337)
* Remove `ZStr` and `ZString`. Use `CStr` and `CString` instead, now that nightly has then in core and alloc, respectively. Fixes #336. * Enable "core_c_str" on nightly to use core::ffi::CStr. * Don't test --no-default-features on stable. --no-default-features disables "std", and building without "std" will require nightly now. * `CString` and `NulError` are in alloc. * Add comments to the relevant Cargo.toml features. * rustfmt
1 parent ea0f3b0 commit b437157

Some content is hidden

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

46 files changed

+497
-2308
lines changed

.github/workflows/main.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,8 @@ jobs:
107107
runs-on: ${{ matrix.os }}
108108
strategy:
109109
matrix:
110-
build: [stable, nightly]
110+
build: [nightly]
111111
include:
112-
- build: stable
113-
os: ubuntu-latest
114-
rust: stable
115112
- build: nightly
116113
os: ubuntu-latest
117114
rust: nightly

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ targets = [
9191

9292
[features]
9393
default = ["std"]
94+
95+
# This enables use of std. Disabling this enables no_std, and
96+
# requires nightly Rust.
9497
std = ["io-lifetimes"]
98+
99+
# This is used in the port of std to rustix.
95100
rustc-dep-of-std = [
96101
"core",
97102
"alloc",

benches/mod.rs

+2-2
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ 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");
20+
use_feature_or_nothing("core_c_str");
21+
use_feature_or_nothing("alloc_c_string");
2422
}
2523

2624
// Gather target information.

src/zstr.rs src/cstr.rs

+18-18
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<()> {
15+
/// use rustix::cstr;
1516
/// use rustix::fs::{cwd, statat, AtFlags};
16-
/// use rustix::zstr;
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

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
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-
4+
pub use alloc::ffi::{CString, NulError};
205
#[cfg(not(feature = "std"))]
21-
pub use z_str::{FromBytesWithNulError, FromVecWithNulError, NulError, ZStr, ZString};
6+
pub use core::ffi::{CStr, FromBytesWithNulError};
227

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

0 commit comments

Comments
 (0)