ndarray/dimension/
axis.rs

1// Copyright 2016 bluss and ndarray developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9/// An axis index.
10///
11/// An axis one of an array’s “dimensions”; an *n*-dimensional array has *n*
12/// axes.  Axis *0* is the array’s outermost axis and *n*-1 is the innermost.
13///
14/// All array axis arguments use this type to make the code easier to write
15/// correctly and easier to understand.
16/// 
17/// For example: in a method like `index_axis(axis, index)` the code becomes
18/// self-explanatory when it's called like `.index_axis(Axis(1), i)`; it's
19/// evident which integer is the axis number and which is the index.
20///
21/// Note: This type does **not** implement From/Into usize and similar trait
22/// based conversions, because we want to preserve code readability and quality.
23///
24/// `Axis(1)` in itself is a very clear code style and the style that should be
25/// avoided is code like `1.into()`.
26#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
27pub struct Axis(pub usize);
28
29impl Axis {
30    /// Return the index of the axis.
31    #[inline(always)]
32    pub fn index(self) -> usize {
33        self.0
34    }
35}