Skip to content

Commit 4c8ebb1

Browse files
committed
ffi: simplify FromBytesWithNulError in core::c_str
`FromBytesWithNulError` is a struct that only contains `kind: FromBytesWithNulErrorKind`, which is an enum. This representation is somewhat redundant. Make `FromBytesWithNulError` an enum that is equivalent to current `FromBytesWithNulErrorKind` in order to simplify. Additionally, add docs for the enum members. Note that this change aligns `FromBytesWithNulError` more closely with `CStrConvertError` in the Linux kernel [1], which will utilize rustc's `CStr` in the future. [1]: https://github.com/Rust-for-Linux/linux/blob/ae7851c29747fa3765ecb722fe722117a346f988/rust/kernel/str.rs#L130-L138
1 parent 4cc494b commit 4c8ebb1

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

library/core/src/ffi/c_str.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,37 +125,33 @@ pub struct CStr {
125125
/// ```
126126
#[derive(Clone, PartialEq, Eq, Debug)]
127127
#[stable(feature = "core_c_str", since = "1.64.0")]
128-
pub struct FromBytesWithNulError {
129-
kind: FromBytesWithNulErrorKind,
130-
}
131-
132-
#[derive(Clone, PartialEq, Eq, Debug)]
133-
enum FromBytesWithNulErrorKind {
128+
pub enum FromBytesWithNulError {
129+
/// Supplied bytes contain an interior `NUL`. The inner value is the
130+
/// position of the `NUL`.
134131
InteriorNul(usize),
132+
/// Supplied bytes are not terminated by `NUL`.
135133
NotNulTerminated,
136134
}
137135

138136
// FIXME: const stability attributes should not be required here, I think
139137
impl FromBytesWithNulError {
140138
#[rustc_const_stable(feature = "const_cstr_methods", since = "1.72.0")]
141139
const fn interior_nul(pos: usize) -> FromBytesWithNulError {
142-
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::InteriorNul(pos) }
140+
FromBytesWithNulError::InteriorNul(pos)
143141
}
144142
#[rustc_const_stable(feature = "const_cstr_methods", since = "1.72.0")]
145143
const fn not_nul_terminated() -> FromBytesWithNulError {
146-
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::NotNulTerminated }
144+
FromBytesWithNulError::NotNulTerminated
147145
}
148146
}
149147

150148
#[stable(feature = "frombyteswithnulerror_impls", since = "1.17.0")]
151149
impl Error for FromBytesWithNulError {
152150
#[allow(deprecated)]
153151
fn description(&self) -> &str {
154-
match self.kind {
155-
FromBytesWithNulErrorKind::InteriorNul(..) => {
156-
"data provided contains an interior nul byte"
157-
}
158-
FromBytesWithNulErrorKind::NotNulTerminated => "data provided is not nul terminated",
152+
match self {
153+
Self::InteriorNul(..) => "data provided contains an interior nul byte",
154+
Self::NotNulTerminated => "data provided is not nul terminated",
159155
}
160156
}
161157
}
@@ -200,7 +196,7 @@ impl fmt::Display for FromBytesWithNulError {
200196
#[allow(deprecated, deprecated_in_future)]
201197
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
202198
f.write_str(self.description())?;
203-
if let FromBytesWithNulErrorKind::InteriorNul(pos) = self.kind {
199+
if let Self::InteriorNul(pos) = self {
204200
write!(f, " at byte pos {pos}")?;
205201
}
206202
Ok(())

0 commit comments

Comments
 (0)