foyer_common/
assert.rs

1// Copyright 2025 foyer Project Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/// Use `debug_assert!` by default. Use `assert!` when feature "strict_assertions" is enabled.
16#[macro_export]
17macro_rules! strict_assert {
18    ($($arg:tt)*) => {
19        #[cfg(feature = "strict_assertions")]
20        assert!($($arg)*);
21        #[cfg(not(feature = "strict_assertions"))]
22        debug_assert!($($arg)*);
23    }
24}
25
26/// Use `debug_assert_eq!` by default. Use `assert_eq!` when feature "strict_assertions" is enabled.
27#[macro_export]
28macro_rules! strict_assert_eq {
29    ($($arg:tt)*) => {
30        #[cfg(feature = "strict_assertions")]
31        assert_eq!($($arg)*);
32        #[cfg(not(feature = "strict_assertions"))]
33        debug_assert_eq!($($arg)*);
34    }
35}
36
37/// Use `debug_assert_ne!` by default. Use `assert_ne!` when feature "strict_assertions" is enabled.
38#[macro_export]
39macro_rules! strict_assert_ne {
40    ($($arg:tt)*) => {
41        #[cfg(feature = "strict_assertions")]
42        assert_ne!($($arg)*);
43        #[cfg(not(feature = "strict_assertions"))]
44        debug_assert_ne!($($arg)*);
45    }
46}
47
48/// Extend functions for [`Option`].
49pub trait OptionExt<T>: Sized {
50    /// Use `unwrap_unchecked` by default. Use `unwrap` when feature "strict_assertions" is enabled.
51    ///
52    /// # Safety
53    ///
54    /// See [`Option::unwrap_unchecked`].
55    unsafe fn strict_unwrap_unchecked(self) -> T;
56}
57
58impl<T> OptionExt<T> for Option<T> {
59    unsafe fn strict_unwrap_unchecked(self) -> T {
60        #[cfg(feature = "strict_assertions")]
61        {
62            self.unwrap()
63        }
64        #[cfg(not(feature = "strict_assertions"))]
65        {
66            unsafe { self.unwrap_unchecked() }
67        }
68    }
69}