@@ -76,31 +76,31 @@ impl fmt::Debug for Error {
76
76
#[ allow( dead_code) ]
77
77
impl Error {
78
78
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" ) ;
80
80
81
81
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" ) ;
83
83
84
- pub ( crate ) const UNKNOWN_THREAD_COUNT : Self = const_io_error ! (
84
+ pub ( crate ) const UNKNOWN_THREAD_COUNT : Self = const_error ! (
85
85
ErrorKind :: NotFound ,
86
86
"The number of hardware threads is not known for the target platform"
87
87
) ;
88
88
89
89
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" ) ;
91
91
92
92
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" ) ;
94
94
95
95
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" ) ;
97
97
}
98
98
99
99
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
100
100
impl From < alloc:: ffi:: NulError > for Error {
101
101
/// Converts a [`alloc::ffi::NulError`] into a [`Error`].
102
102
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" )
104
104
}
105
105
}
106
106
@@ -151,27 +151,38 @@ pub type RawOsError = sys::RawOsError;
151
151
// (For the sake of being explicit: the alignment requirement here only matters
152
152
// if `error/repr_bitpacked.rs` is in use — for the unpacked repr it doesn't
153
153
// matter at all)
154
+ #[ doc( hidden) ]
155
+ #[ unstable( feature = "io_const_error_internals" , issue = "none" ) ]
154
156
#[ repr( align( 4 ) ) ]
155
157
#[ 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 ,
165
161
}
166
162
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
+ ) )
175
186
}
176
187
177
188
// As with `SimpleMessage`: `#[repr(align(4))]` here is just because
@@ -592,13 +603,15 @@ impl Error {
592
603
///
593
604
/// This function does not allocate.
594
605
///
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")`.
597
608
///
598
609
/// This function should maybe change to `from_static_message<const MSG: &'static
599
610
/// str>(kind: ErrorKind)` in the future, when const generics allow that.
600
611
#[ 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 {
602
615
Self { repr : Repr :: new_simple_message ( msg) }
603
616
}
604
617
0 commit comments