Skip to content

Commit a6f3f18

Browse files
joboetgitbot
authored and
gitbot
committed
std: expose const_io_error! as const_error!
ACP: rust-lang/libs-team#205 Tracking issue: rust-lang#133448
1 parent ef0c227 commit a6f3f18

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

std/src/io/error.rs

+31-18
Original file line numberDiff line numberDiff line change
@@ -151,27 +151,38 @@ pub type RawOsError = sys::RawOsError;
151151
// (For the sake of being explicit: the alignment requirement here only matters
152152
// if `error/repr_bitpacked.rs` is in use — for the unpacked repr it doesn't
153153
// matter at all)
154+
#[doc(hidden)]
155+
#[unstable(feature = "io_const_error_internals", issue = "none")]
154156
#[repr(align(4))]
155157
#[derive(Debug)]
156-
pub(crate) struct SimpleMessage {
157-
kind: ErrorKind,
158-
message: &'static str,
159-
}
160-
161-
impl SimpleMessage {
162-
pub(crate) const fn new(kind: ErrorKind, message: &'static str) -> Self {
163-
Self { kind, message }
164-
}
158+
pub struct SimpleMessage {
159+
pub kind: ErrorKind,
160+
pub message: &'static str,
165161
}
166162

167-
/// Creates and returns an `io::Error` for a given `ErrorKind` and constant
168-
/// message. This doesn't allocate.
169-
pub(crate) macro const_io_error($kind:expr, $message:expr $(,)?) {
170-
$crate::io::error::Error::from_static_message({
171-
const MESSAGE_DATA: $crate::io::error::SimpleMessage =
172-
$crate::io::error::SimpleMessage::new($kind, $message);
173-
&MESSAGE_DATA
174-
})
163+
/// Creates a new I/O error from a known kind of error and a string literal.
164+
///
165+
/// Contrary to [`Error::new`], this macro does not allocate and can be used in
166+
/// `const` contexts.
167+
///
168+
/// # Example
169+
/// ```
170+
/// #![feature(io_const_error)]
171+
/// use std::io::{const_error, Error, ErrorKind};
172+
///
173+
/// const FAIL: Error = const_error!(ErrorKind::Unsupported, "tried something that never works");
174+
///
175+
/// fn not_here() -> Result<(), Error> {
176+
/// Err(FAIL)
177+
/// }
178+
/// ```
179+
#[rustc_macro_transparency = "semitransparent"]
180+
#[unstable(feature = "io_const_error", issue = "133448")]
181+
#[allow_internal_unstable(hint_must_use, io_const_error_internals)]
182+
pub macro const_error($kind:expr, $message:expr $(,)?) {
183+
$crate::hint::must_use($crate::io::Error::from_static_message(
184+
const { &$crate::io::SimpleMessage { kind: $kind, message: $message } },
185+
))
175186
}
176187

177188
// As with `SimpleMessage`: `#[repr(align(4))]` here is just because
@@ -598,7 +609,9 @@ impl Error {
598609
/// This function should maybe change to `from_static_message<const MSG: &'static
599610
/// str>(kind: ErrorKind)` in the future, when const generics allow that.
600611
#[inline]
601-
pub(crate) const fn from_static_message(msg: &'static SimpleMessage) -> Error {
612+
#[doc(hidden)]
613+
#[unstable(feature = "io_const_error_internals", issue = "none")]
614+
pub const fn from_static_message(msg: &'static SimpleMessage) -> Error {
602615
Self { repr: Repr::new_simple_message(msg) }
603616
}
604617

std/src/io/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,15 @@ mod tests;
301301
pub use core::io::{BorrowedBuf, BorrowedCursor};
302302
use core::slice::memchr;
303303

304-
pub(crate) use error::const_io_error;
305-
306304
#[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
307305
pub use self::buffered::WriterPanicked;
308306
#[unstable(feature = "raw_os_error_ty", issue = "107792")]
309307
pub use self::error::RawOsError;
308+
#[doc(hidden)]
309+
#[unstable(feature = "io_const_error_internals", issue = "none")]
310+
pub use self::error::SimpleMessage;
311+
#[unstable(feature = "io_const_error", issue = "133448")]
312+
pub use self::error::const_error;
310313
#[stable(feature = "is_terminal", since = "1.70.0")]
311314
pub use self::stdio::IsTerminal;
312315
pub(crate) use self::stdio::attempt_print_to_stderr;

std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@
340340
#![feature(fmt_internals)]
341341
#![feature(hasher_prefixfree_extras)]
342342
#![feature(hashmap_internals)]
343+
#![feature(hint_must_use)]
343344
#![feature(ip)]
344345
#![feature(lazy_get)]
345346
#![feature(maybe_uninit_slice)]

0 commit comments

Comments
 (0)