air_utils/
lib.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#[macro_export]
18macro_rules! measure {
19    (target: $target:expr, $expr:expr, $span:literal) => ({
20        let span = tracing::span!(target=$target, $span);
21        let _enter = span.enter();
22        $expr
23    });
24    ($expr:expr, $level:expr, $span:literal, $($fields:tt)*) => ({
25        let span = tracing::span!($level, $span, $($fields)*);
26        let _enter = span.enter();
27        $expr
28    });
29    ($expr:expr, $level:expr, $span:literal) => ({
30        let span = tracing::span!($level, $span);
31        let _enter = span.enter();
32        $expr
33    });
34    ($expr:expr, $span:literal) => ({
35        let span = tracing::span!($span);
36        let _enter = span.enter();
37        $expr
38    });
39}
40
41#[macro_export]
42macro_rules! auto_checked_add {
43    [$type:ty] => {
44        impl ::num_traits::CheckedAdd for $type {
45            fn checked_add(&self, other: &Self) -> Option<Self> {
46                self.0.checked_add(other.0).map(Self)
47            }
48        }
49    };
50}
51
52#[macro_export]
53macro_rules! farewell_if_fail {
54    ($cmd:expr, $raw_prev_data:expr, $soft_limits_triggering:expr) => {
55        match $cmd {
56            Ok(result) => result,
57            // return the prev data in case of errors
58            Err(error) => {
59                return Err(farewell::from_uncatchable_error(
60                    $raw_prev_data,
61                    error,
62                    $soft_limits_triggering,
63                ))
64            }
65        };
66    };
67}