ndarray/
macro_utils.rs

1/// Derive Copy and Clone using the parameters (and bounds) as specified in []
2macro_rules! copy_and_clone {
3    ([$($parm:tt)*] $type_:ty) => {
4        impl<$($parm)*> Copy for $type_ { }
5        impl<$($parm)*> Clone for $type_ {
6            #[inline(always)]
7            fn clone(&self) -> Self { *self }
8        }
9    };
10    ($type_:ty) => {
11        copy_and_clone!{ [] $type_ }
12    }
13}
14
15macro_rules! clone_bounds {
16    ([$($parmbounds:tt)*] $typename:ident [$($parm:tt)*] {
17        @copy {
18            $($copyfield:ident,)*
19        }
20        $($field:ident,)*
21    }) => {
22        impl<$($parmbounds)*> Clone for $typename<$($parm)*> {
23            fn clone(&self) -> Self {
24                $typename {
25                $(
26                    $copyfield: self.$copyfield,
27                )*
28                $(
29                    $field: self.$field.clone(),
30                )*
31                }
32            }
33        }
34    };
35}
36
37/// This assertion is always enabled but only verbose (formatting when
38/// debug assertions are enabled).
39#[cfg(debug_assertions)]
40macro_rules! ndassert {
41    ($e:expr, $($t:tt)*) => { assert!($e, $($t)*) }
42}
43
44#[cfg(not(debug_assertions))]
45macro_rules! ndassert {
46    ($e:expr, $($_ignore:tt)*) => {
47        assert!($e)
48    };
49}
50
51macro_rules! expand_if {
52    (@bool [true] $($body:tt)*) => { $($body)* };
53    (@bool [false] $($body:tt)*) => { };
54    (@nonempty [$($if_present:tt)+] $($body:tt)*) => {
55        $($body)*
56    };
57    (@nonempty [] $($body:tt)*) => { };
58}
59
60// Macro to insert more informative out of bounds message in debug builds
61#[cfg(debug_assertions)]
62macro_rules! debug_bounds_check {
63    ($self_:ident, $index:expr) => {
64        if $index.index_checked(&$self_.dim, &$self_.strides).is_none() {
65            panic!(
66                "ndarray: index {:?} is out of bounds for array of shape {:?}",
67                $index,
68                $self_.shape()
69            );
70        }
71    };
72}
73
74#[cfg(not(debug_assertions))]
75macro_rules! debug_bounds_check {
76    ($self_:ident, $index:expr) => {};
77}