const_format/macros/assertions/assertcp_macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
macro_rules! with_shared_docs {(
$(#[$before_clarification:meta])*
;clarification
$(#[$before_syntax:meta])*
;syntax
$(#[$after_syntax:meta])*
;limitations
$item:item
) => (
$(#[$before_clarification])*
///
/// [For **examples** look here](#examples)
///
/// This macro requires the `"assertcp"` feature to be exported.<br>
///
$(#[$before_syntax])*
/// # Syntax
///
/// This macro uses the same syntax
/// for the format string and formatting arguments as the
/// [`formatcp`](crate::formatcp) macro.
///
$(#[$after_syntax])*
/// # Limitations
///
/// This macro can only take constants of these types as arguments:
///
/// - `&str`
///
/// - `i*`/`u*` (all the primitive integer types).
///
/// - `char`
///
/// - `bool`
///
/// This macro also has these limitations:
///
/// - It can only use constants that involve concrete types,
/// so while a `Type::<u8>::FOO` in an argument would be fine,
/// `Type::<T>::FOO` would not be (`T` being a type parameter).
///
/// - Integer arguments must have a type inferrable from context,
/// [as described in the integer arguments section in the root module
/// ](./index.html#integer-args).
///
$item
)}
with_shared_docs! {
/// Compile-time assertion with formatting.
///
;clarification
;syntax
;limitations
///
/// # Examples
///
/// ### Passing assertion
///
/// ```rust
/// use const_format::assertcp;
///
/// use std::mem::align_of;
///
/// assertcp!(
/// align_of::<&str>() == align_of::<usize>(),
/// "The alignment of `&str`({} bytes) and `usize`({} bytes) isn't the same?!?!",
/// align_of::<&str>(),
/// align_of::<usize>(),
/// );
///
/// # fn main(){}
/// ```
///
/// ### Failing assertion
///
/// This example demonstrates a failing assertion,
/// and how the compiler error looks like as of 2023-10-14.
///
/// ```compile_fail
/// use const_format::assertcp;
///
/// const L: u64 = 2;
/// const R: u32 = 5;
///
/// assertcp!(L.pow(R) == 64, "{L} to the {R} isn't 64, it's {}", L.pow(R));
///
/// # fn main(){}
/// ```
///
/// This is the compiler output:
///
/// ```text
/// error[E0080]: evaluation of constant value failed
/// --> const_format/src/macros/assertions/assertcp_macros.rs:116:11
/// |
/// 9 | assertcp!(L.pow(R) == 64, "{L} to the {R} isn't 64, it's {}", L.pow(R));
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
/// assertion failed.
/// 2 to the 5 isn't 64, it's 32
/// ', const_format/src/macros/assertions/assertcp_macros.rs:9:11
///
/// ```
///
#[cfg_attr(feature = "__docsrs", doc(cfg(feature = "assertcp")))]
#[macro_export]
macro_rules! assertcp {
($($parameters:tt)*) => (
$crate::__assertc_inner!{
__formatcp_if_impl
($($parameters)*)
($($parameters)*)
}
);
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __assertcp_equality_inner {
(
($($parameters:tt)*)
(
$left:expr,
$right:expr
$(, $fmt_literal:expr $(,$fmt_arg:expr)*)? $(,)?
)
($($op:tt)*)
($op_str:expr)
)=>{
#[allow(non_snake_case)]
const _: () = {
use $crate::__cf_osRcTFl4A;
const ARGS_NHPMWYD3NJA:
($crate::pmr::bool, $crate::pmr::PArgument, $crate::pmr::PArgument)
= {
let left = $crate::PWrapper($left);
let right = $crate::pmr::PConvWrapper($right);
let cond = left.const_eq(&right.0);
let fmt = $crate::pmr::FormattingFlags::NEW.set_alternate(true);
(
cond,
$crate::pmr::PConvWrapper(left.0).to_pargument_debug(fmt),
right.to_pargument_debug(fmt),
)
};
$crate::__assertc_common!{
__formatcp_if_impl
($($parameters)*)
(ARGS_NHPMWYD3NJA.0 $($op)* true)
(
concat!(
"\nassertion failed: `(left ",
$op_str,
" right)`\n",
" left: `{left_NHPMWYD3NJA:#?}`\n\
right: `{right_NHPMWYD3NJA:#?}`",
$("\n", $fmt_literal, "\n")?
),
$($($fmt_arg,)*)?
left_NHPMWYD3NJA = ARGS_NHPMWYD3NJA.1,
right_NHPMWYD3NJA = ARGS_NHPMWYD3NJA.2
)
}
};
}
}
with_shared_docs! {
/// Compile-time equality assertion with formatting.
///
;clarification
;syntax
;limitations
///
/// # Examples
///
/// ### Passing assertion
///
/// ```rust
/// use const_format::assertcp_eq;
///
/// const NAME: &str = "Bob";
///
/// assertcp_eq!(NAME, "Bob", "Guessed wrong, the right value is {}", NAME);
///
/// const SUM: u8 = 1 + 2 + 3;
/// assertcp_eq!(6u8, SUM, "Guessed wrong, the right value is {}", SUM);
/// ```
///
/// ### Failing assertion
///
/// This example demonstrates a failing assertion,
/// and how the compiler error looks like as of 2023-10-14.
///
/// ```compile_fail
/// use const_format::assertcp_eq;
///
/// use std::mem::size_of;
///
/// #[repr(C)]
/// struct Type(u16, u16, u16);
///
/// assertcp_eq!(size_of::<Type>(), size_of::<[u16; 2]>(), "Whoops, `Type` is too large");
///
/// ```
///
/// This is the compiler output:
///
/// ```text
/// error[E0080]: evaluation of constant value failed
/// --> const_format/src/macros/assertions/assertcp_macros.rs:235:14
/// |
/// 12 | assertcp_eq!(size_of::<Type>(), size_of::<[u16; 2]>(), "Whoops, `Type` is too large");
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
/// assertion failed: `(left == right)`
/// left: `6`
/// right: `4`
/// Whoops, `Type` is too large
/// ', const_format/src/macros/assertions/assertcp_macros.rs:12:14
///
/// ```
///
#[cfg_attr(feature = "__docsrs", doc(cfg(feature = "assertcp")))]
#[macro_export]
macro_rules! assertcp_eq {
($($parameters:tt)*) => (
$crate::__assertcp_equality_inner!{
($($parameters)*)
($($parameters)*)
( == )
("==")
}
);
}
}
with_shared_docs! {
/// Compile-time inequality assertion with formatting.
///
;clarification
;syntax
;limitations
///
/// # Examples
///
/// ### Passing assertion
///
/// ```rust
/// use const_format::assertcp_ne;
///
/// assertcp_ne!(std::mem::size_of::<usize>(), 1usize, "Oh no, usize is tiny!");
///
/// const CHAR: char = ';';
/// assertcp_ne!(CHAR, '.', "CHAR must not be a dot!");
/// ```
///
/// ### Failing assertion
///
/// This example demonstrates a failing assertion,
/// and how the compiler error looks like as of 2023-10-14.
///
/// ```compile_fail
/// use const_format::assertcp_ne;
///
/// const NAME: &str = "";
/// assertcp_ne!(NAME, "", "NAME must not be empty!");
///
/// ```
///
/// This is the compiler output:
///
/// ```text
/// error[E0080]: evaluation of constant value failed
/// --> const_format/src/macros/assertions/assertcp_macros.rs:297:14
/// |
/// 8 | assertcp_ne!(NAME, "", "NAME must not be empty!");
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
/// assertion failed: `(left != right)`
/// left: `""`
/// right: `""`
/// NAME must not be empty!
/// ', const_format/src/macros/assertions/assertcp_macros.rs:8:14
/// ```
///
#[cfg_attr(feature = "__docsrs", doc(cfg(feature = "assertcp")))]
#[macro_export]
macro_rules! assertcp_ne {
($($parameters:tt)*) => (
$crate::__assertcp_equality_inner!{
($($parameters)*)
($($parameters)*)
( != )
("!=")
}
);
}
}