Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 7 pull requests #135525

Merged
merged 18 commits into from
Jan 15, 2025
Merged
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
e7bd340
Switch missing_abi lint to warn-by-default.
m-ou-se Oct 31, 2024
585c976
Update tests.
m-ou-se Oct 31, 2024
2f5a3d4
Convert `struct FromBytesWithNulError` into enum
nyurik Dec 10, 2024
86b86fa
Rename `pos` to `position`
nyurik Jan 11, 2025
9cbd177
Detect unstable lint docs that dont enable their feature
compiler-errors Jan 12, 2025
3d54764
ci: Enable opt-dist for dist-aarch64-linux builds
mrkajetanp Dec 3, 2024
3d9dcf4
Un-spaghettify the control flow of generate_lint_output
compiler-errors Jan 12, 2025
a911da1
Use a C-safe return type for `__rust_[ui]128_*` overflowing intrinsics
tgross35 Dec 15, 2024
f6a2db8
Update compiler-builtins to 0.1.143
tgross35 Jan 15, 2025
c89ee08
Make sure we actually use the right trivial lifetime substs when eage…
compiler-errors Jan 15, 2025
58d6301
Update ReadDir::next in std::sys::pal::unix::fs to use `&raw const (*…
zachs18 Dec 23, 2024
285df03
Rollup merge of #132397 - m-ou-se:warn-missing-abi, r=Nadrieril
jhpratt Jan 15, 2025
f9d8b65
Rollup merge of #133807 - mrkajetanp:ci-aarch64-opt-dist, r=Kobzol
jhpratt Jan 15, 2025
229c91b
Rollup merge of #134143 - nyurik:err-nul, r=dtolnay
jhpratt Jan 15, 2025
bf4aeeb
Rollup merge of #134338 - tgross35:overflowing-c-safe-ret, r=bjorn3,a…
jhpratt Jan 15, 2025
56eb7bd
Rollup merge of #134678 - zachs18:offset-ptr-update, r=tgross35
jhpratt Jan 15, 2025
f35ff74
Rollup merge of #135424 - compiler-errors:unstable-lint, r=ehuss
jhpratt Jan 15, 2025
8e91327
Rollup merge of #135520 - compiler-errors:mono-impossible-drop-with-l…
jhpratt Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 20 additions & 32 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
@@ -124,37 +124,25 @@ pub struct CStr {
///
/// let _: FromBytesWithNulError = CStr::from_bytes_with_nul(b"f\0oo").unwrap_err();
/// ```
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[stable(feature = "core_c_str", since = "1.64.0")]
pub struct FromBytesWithNulError {
kind: FromBytesWithNulErrorKind,
}

#[derive(Clone, PartialEq, Eq, Debug)]
enum FromBytesWithNulErrorKind {
InteriorNul(usize),
pub enum FromBytesWithNulError {
/// Data provided contains an interior nul byte at byte `position`.
InteriorNul {
/// The position of the interior nul byte.
position: usize,
},
/// Data provided is not nul terminated.
NotNulTerminated,
}

// FIXME: const stability attributes should not be required here, I think
impl FromBytesWithNulError {
const fn interior_nul(pos: usize) -> FromBytesWithNulError {
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::InteriorNul(pos) }
}
const fn not_nul_terminated() -> FromBytesWithNulError {
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::NotNulTerminated }
}
}

#[stable(feature = "frombyteswithnulerror_impls", since = "1.17.0")]
impl Error for FromBytesWithNulError {
#[allow(deprecated)]
fn description(&self) -> &str {
match self.kind {
FromBytesWithNulErrorKind::InteriorNul(..) => {
"data provided contains an interior nul byte"
}
FromBytesWithNulErrorKind::NotNulTerminated => "data provided is not nul terminated",
match self {
Self::InteriorNul { .. } => "data provided contains an interior nul byte",
Self::NotNulTerminated => "data provided is not nul terminated",
}
}
}
@@ -199,8 +187,8 @@ impl fmt::Display for FromBytesWithNulError {
#[allow(deprecated, deprecated_in_future)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.description())?;
if let FromBytesWithNulErrorKind::InteriorNul(pos) = self.kind {
write!(f, " at byte pos {pos}")?;
if let Self::InteriorNul { position } = self {
write!(f, " at byte pos {position}")?;
}
Ok(())
}
@@ -349,25 +337,25 @@ impl CStr {
/// use std::ffi::CStr;
///
/// let cstr = CStr::from_bytes_with_nul(b"hello\0");
/// assert!(cstr.is_ok());
/// assert_eq!(cstr, Ok(c"hello"));
/// ```
///
/// Creating a `CStr` without a trailing nul terminator is an error:
///
/// ```
/// use std::ffi::CStr;
/// use std::ffi::{CStr, FromBytesWithNulError};
///
/// let cstr = CStr::from_bytes_with_nul(b"hello");
/// assert!(cstr.is_err());
/// assert_eq!(cstr, Err(FromBytesWithNulError::NotNulTerminated));
/// ```
///
/// Creating a `CStr` with an interior nul byte is an error:
///
/// ```
/// use std::ffi::CStr;
/// use std::ffi::{CStr, FromBytesWithNulError};
///
/// let cstr = CStr::from_bytes_with_nul(b"he\0llo\0");
/// assert!(cstr.is_err());
/// assert_eq!(cstr, Err(FromBytesWithNulError::InteriorNul { position: 2 }));
/// ```
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
#[rustc_const_stable(feature = "const_cstr_methods", since = "1.72.0")]
@@ -379,8 +367,8 @@ impl CStr {
// of the byte slice.
Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
}
Some(nul_pos) => Err(FromBytesWithNulError::interior_nul(nul_pos)),
None => Err(FromBytesWithNulError::not_nul_terminated()),
Some(position) => Err(FromBytesWithNulError::InteriorNul { position }),
None => Err(FromBytesWithNulError::NotNulTerminated),
}
}