macro_rules! unwrap_ctx {
($e:expr $(,)?) => { ... };
}
Expand description
For unwrapping Result
s in const contexts with some error message.
The error type must have a method with this signature:
pub const fn panic(&self) -> ! {
The Results returned by Parser
methods can all be used with this macro.
§Example
§Basic
ⓘ
use konst::{Parser, unwrap_ctx};
let mut parser = Parser::from_str("hello world");
parser = unwrap_ctx!(parser.strip_prefix("hello "));
assert_eq!(parser.bytes(), "world".as_bytes());
§Defining error type
use konst::unwrap_ctx;
const UNWRAPPED: u32 = {
let res: Result<u32, FooError> = Ok(100);
unwrap_ctx!(res)
};
assert_eq!(UNWRAPPED, 100);
use std::fmt::{self, Display};
#[derive(Debug, Clone, PartialEq)]
pub struct FooError(usize);
impl FooError {
pub const fn panic(&self) -> ! {
let offset = self.0;
[/*Foo errored at offset*/][offset]
// Alternatively, using the `const_panic` crate (requires Rust 1.57.0):
//
// const_panic::concat_panic!("Foo errored at offset: ", self.0)
// Alternatively, using the panic macro (requires Rust 1.57.0).
// Unfortunately, formatted `panic!`s are not supported const contexts.
//
// panic!("Foo error")
}
}
impl Display for FooError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl std::error::Error for FooError {}
If res
was an error instead, this is the error message you would see:
error: any use of this value will cause an error
--> src/result.rs:55:9
|
6 | / const UNWRAPPED: u32 = {
7 | | let res: Result<u32, FooError> = Err(FooError(100));
8 | | // let res: Result<u32, FooError> = Ok(100);
9 | | unwrap_ctx!(res)
10 | | };
| |__-
...
23 | [/*Foo errored at offset*/][offset]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| index out of bounds: the length is 0 but the index is 100
| inside `FooError::panic` at src/result.rs:23:9
| inside `UNWRAPPED` at src/result_macros_.rs:6:35
|
= note: `#[deny(const_err)]` on by default