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
//! Shape of tensor.
use core::fmt::Debug;
/// Shape of observation or action.
pub trait Shape: Clone + Debug {
    /// Returns the shape of Shape of an array.
    fn shape() -> &'static [usize];

    /// Returns `true` if you would like to squeeze the first dimension of the array
    /// before conversion into an numpy array in Python. The first dimension may
    /// correspond to process indices for vectorized environments.
    fn squeeze_first_dim() -> bool {
        false
    }

    /// Returns the shape as `Vec<i64>`.
    fn shape_i64vec() -> Vec<i64> {
        Self::shape().iter().map(|e| *e as i64).collect::<Vec<_>>()
    }
}

/// Defines a struct that implements [Shape].
///
/// # Example
///
/// ```
/// use border_core::{shape, Shape};
///
/// shape!(ObsShape, [4, 2]);
///
/// println!("{:?}", ObsShape::shape());
/// ```
#[macro_export]
macro_rules! shape {
    ($struct_:ident, [$($elem_:expr),+]) => {
        #[derive(Clone, Debug)]
        struct $struct_ {}
        impl border_core::Shape for $struct_ {
            fn shape() -> &'static [usize] {
                &[$($elem_),+]
            }
        }
    };
    ($struct_:ident, [$($elem_:expr),+], squeeze_first_dim) => {
        #[derive(Clone, Debug)]
        struct $struct_ {}
        impl border_core::Shape for $struct_ {
            fn shape() -> &'static [usize] {
                &[$($elem_),+]
            }
            fn squeeze_first_dim() -> bool {
                true
            }
        }
    };
}