@@ -76,31 +76,31 @@ impl fmt::Debug for Error {
7676#[ allow( dead_code) ]
7777impl Error {
7878 pub ( crate ) const INVALID_UTF8 : Self =
79- const_io_error ! ( ErrorKind :: InvalidData , "stream did not contain valid UTF-8" ) ;
79+ const_error ! ( ErrorKind :: InvalidData , "stream did not contain valid UTF-8" ) ;
8080
8181 pub ( crate ) const READ_EXACT_EOF : Self =
82- const_io_error ! ( ErrorKind :: UnexpectedEof , "failed to fill whole buffer" ) ;
82+ const_error ! ( ErrorKind :: UnexpectedEof , "failed to fill whole buffer" ) ;
8383
84- pub ( crate ) const UNKNOWN_THREAD_COUNT : Self = const_io_error ! (
84+ pub ( crate ) const UNKNOWN_THREAD_COUNT : Self = const_error ! (
8585 ErrorKind :: NotFound ,
8686 "The number of hardware threads is not known for the target platform"
8787 ) ;
8888
8989 pub ( crate ) const UNSUPPORTED_PLATFORM : Self =
90- const_io_error ! ( ErrorKind :: Unsupported , "operation not supported on this platform" ) ;
90+ const_error ! ( ErrorKind :: Unsupported , "operation not supported on this platform" ) ;
9191
9292 pub ( crate ) const WRITE_ALL_EOF : Self =
93- const_io_error ! ( ErrorKind :: WriteZero , "failed to write whole buffer" ) ;
93+ const_error ! ( ErrorKind :: WriteZero , "failed to write whole buffer" ) ;
9494
9595 pub ( crate ) const ZERO_TIMEOUT : Self =
96- const_io_error ! ( ErrorKind :: InvalidInput , "cannot set a 0 duration timeout" ) ;
96+ const_error ! ( ErrorKind :: InvalidInput , "cannot set a 0 duration timeout" ) ;
9797}
9898
9999#[ stable( feature = "rust1" , since = "1.0.0" ) ]
100100impl From < alloc:: ffi:: NulError > for Error {
101101 /// Converts a [`alloc::ffi::NulError`] into a [`Error`].
102102 fn from ( _: alloc:: ffi:: NulError ) -> Error {
103- const_io_error ! ( ErrorKind :: InvalidInput , "data provided contains a nul byte" )
103+ const_error ! ( ErrorKind :: InvalidInput , "data provided contains a nul byte" )
104104 }
105105}
106106
@@ -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
@@ -592,13 +603,15 @@ impl Error {
592603 ///
593604 /// This function does not allocate.
594605 ///
595- /// You should not use this directly, and instead use the `const_io_error !`
596- /// macro: `io::const_io_error !(ErrorKind::Something, "some_message")`.
606+ /// You should not use this directly, and instead use the `const_error !`
607+ /// macro: `io::const_error !(ErrorKind::Something, "some_message")`.
597608 ///
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