1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

/// Derive Copy and Clone using the parameters (and bounds) as specified in []
macro_rules! copy_and_clone {
    ([$($parm:tt)*] $type_:ty) => {
        impl<$($parm)*> Copy for $type_ { }
        impl<$($parm)*> Clone for $type_ {
            #[inline(always)]
            fn clone(&self) -> Self { *self }
        }
    };
    ($type_:ty) => {
        copy_and_clone!{ [] $type_ }
    }
}

macro_rules! clone_bounds {
    ([$($parmbounds:tt)*] $typename:ident [$($parm:tt)*] {
        @copy {
            $($copyfield:ident,)*
        }
        $($field:ident,)*
    }) => {
        impl<$($parmbounds)*> Clone for $typename<$($parm)*> {
            fn clone(&self) -> Self {
                $typename {
                $(
                    $copyfield: self.$copyfield,
                )*
                $(
                    $field: self.$field.clone(),
                )*
                }
            }
        }
    };
}

/// This assertion is always enabled but only verbose (formatting when
/// debug assertions are enabled).
#[cfg(debug_assertions)]
macro_rules! ndassert {
    ($e:expr, $($t:tt)*) => { assert!($e, $($t)*) }
}

#[cfg(not(debug_assertions))]
macro_rules! ndassert {
    ($e:expr, $($_ignore:tt)*) => { assert!($e) }
}

macro_rules! expand_if {
    (@bool [true] $($body:tt)*) => { $($body)* };
    (@bool [false] $($body:tt)*) => { };
    (@nonempty [$($if_present:tt)+] $($body:tt)*) => {
        $($body)*
    };
    (@nonempty [] $($body:tt)*) => { };
}

// Macro to insert more informative out of bounds message in debug builds
#[cfg(debug_assertions)]
macro_rules! debug_bounds_check {
    ($self_:ident, $index:expr) => {
        if let None = $index.index_checked(&$self_.dim, &$self_.strides) {
            panic!("ndarray: index {:?} is out of bounds for array of shape {:?}",
                   $index, $self_.shape());
        }
    };
}

#[cfg(not(debug_assertions))]
macro_rules! debug_bounds_check {
    ($self_:ident, $index:expr) => { };
}