@@ -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
0 commit comments