@@ -151,27 +151,37 @@ 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
+ pub macro const_error ( $kind: expr, $message: expr $( , ) ?) {
182
+ $crate:: hint:: must_use ( $crate:: io:: Error :: from_static_message (
183
+ const { & $crate:: io:: SimpleMessage { kind : $kind, message : $message } } ,
184
+ ) )
175
185
}
176
186
177
187
// As with `SimpleMessage`: `#[repr(align(4))]` here is just because
@@ -598,7 +608,9 @@ impl Error {
598
608
/// This function should maybe change to `from_static_message<const MSG: &'static
599
609
/// str>(kind: ErrorKind)` in the future, when const generics allow that.
600
610
#[ inline]
601
- pub ( crate ) const fn from_static_message ( msg : & ' static SimpleMessage ) -> Error {
611
+ #[ doc( hidden) ]
612
+ #[ unstable( feature = "io_const_error_internals" , issue = "none" ) ]
613
+ pub const fn from_static_message ( msg : & ' static SimpleMessage ) -> Error {
602
614
Self { repr : Repr :: new_simple_message ( msg) }
603
615
}
604
616
0 commit comments