Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8/// Wraps a `const fn` stripping the "constness" when compiled under loom.
9///
10/// `loom` works by tracking additional state alongside each type. This has the annoying limitation that
11/// many methods that are `const` in `core` cannot be `const` in `loom` because of this additional tracking.
12///
13/// As you can imagine this makes writing `const` functions that use `loom` types difficult.
14///
15/// # Example
16///
17/// ```rust
18/// # use util::loom_const_fn;
19///
20/// struct Something { str: &'static str }
21///
22/// impl Something {
23/// // `Something::new` will be const in regular use and non-const when running in loom
24/// loom_const_fn! {
25/// pub const fn new() -> Self {
26/// Self { str: "Hello World" }
27/// }
28/// }
29/// }
30/// ```
31#[macro_export]
32macro_rules! loom_const_fn {
33 (
34 $(#[$meta:meta])*
35 $vis:vis const unsafe fn $name:ident($($arg:ident: $T:ty),*) -> $Ret:ty $body:block
36 ) => {
37 $(#[$meta])*
38 #[cfg(not(loom))]
39 $vis const unsafe fn $name($($arg: $T),*) -> $Ret $body
40
41 $(#[$meta])*
42 #[cfg(loom)]
43 #[inline]
44 $vis unsafe fn $name($($arg: $T),*) -> $Ret $body
45 };
46 (
47 $(#[$meta:meta])*
48 $vis:vis const fn $name:ident($($arg:ident: $T:ty),*) -> $Ret:ty $body:block
49 ) => {
50 $(#[$meta])*
51 #[cfg(not(loom))]
52 $vis const fn $name($($arg: $T),*) -> $Ret $body
53
54 $(#[$meta])*
55 #[cfg(loom)]
56 #[inline]
57 $vis fn $name($($arg: $T),*) -> $Ret $body
58 }
59}