lkml.org 
[lkml]   [2022]   [Dec]   [2]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v2 26/28] rust: build_assert: add `build_{error,assert}!` macros
On Fri, Dec 02, 2022 at 05:14:57PM +0100, ojeda@kernel.org wrote:
> From: Gary Guo <gary@garyguo.net>
>
> Add the `build_error!` and `build_assert!` macros which leverage
> the previously introduced `build_error` crate. Do so in a new
> module, called `build_assert`.
>
> The former fails the build if the code path calling it can possibly
> be executed. The latter asserts that a boolean expression is `true`
> at compile time.
>
> In particular, `build_assert!` can be used in some contexts where
> `static_assert!` cannot:
>
> fn f1<const N: usize>() {
> static_assert!(N > 1);` // Error.
> build_assert!(N > 1); // Build-time check.
> assert!(N > 1); // Run-time check.
> }
>
> #[inline]
> fn f2(n: usize) {
> static_assert!(n > 1); // Error.
> build_assert!(n > 1); // Build-time check.
> assert!(n > 1); // Run-time check.
> }
>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> [Reworded, adapted for upstream and applied latest changes]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Wei Liu <wei.liu@kernel.org>

\
 
 \ /
  Last update: 2022-12-02 19:35    [W:0.202 / U:0.012 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site