mech_range/
lib.rs

1#![feature(step_trait)]
2#![no_main]
3#![allow(warnings)]
4#[macro_use]
5extern crate mech_core;
6#[cfg(feature = "matrix")]
7extern crate nalgebra as na;
8extern crate paste;
9
10use paste::paste;
11
12#[cfg(feature = "vector3")]
13use nalgebra::Vector3;
14#[cfg(feature = "vectord")]
15use nalgebra::DVector;
16#[cfg(feature = "vector2")]
17use nalgebra::Vector2;
18#[cfg(feature = "vector4")]
19use nalgebra::Vector4;
20#[cfg(feature = "rowdvector")]
21use nalgebra::RowDVector;
22#[cfg(feature = "row_vectord")]
23use nalgebra::RowDVector;
24#[cfg(feature = "matrix1")]
25use nalgebra::Matrix1;
26#[cfg(feature = "matrix3")]
27use nalgebra::Matrix3;
28#[cfg(feature = "matrix4")]
29use nalgebra::Matrix4;
30#[cfg(feature = "row_vector3")]
31use nalgebra::RowVector3;
32#[cfg(feature = "row_vector4")]
33use nalgebra::RowVector4;
34#[cfg(feature = "row_vector2")]
35use nalgebra::RowVector2;
36#[cfg(feature = "matrixd")]
37use nalgebra::DMatrix;
38#[cfg(feature = "matrix2x3")]
39use nalgebra::Matrix2x3;
40#[cfg(feature = "matrix3x2")]
41use nalgebra::Matrix3x2;
42#[cfg(feature = "matrix2")]
43use nalgebra::Matrix2;
44
45use std::ops::*;
46#[cfg(feature = "range")]
47use num_traits::{Zero, One};
48use std::fmt::Debug;
49#[cfg(feature = "matrix")]
50use mech_core::matrix::Matrix;
51
52#[cfg(feature = "exclusive")]
53pub mod exclusive;
54#[cfg(feature = "exclusive")]
55pub mod exclusive_increment;
56#[cfg(feature = "inclusive")]
57pub mod inclusive;
58#[cfg(feature = "inclusive")]
59pub mod inclusive_increment;
60
61#[cfg(feature = "exclusive")]
62pub use self::exclusive::*;
63#[cfg(feature = "exclusive")]
64pub use self::exclusive_increment::*;
65#[cfg(feature = "inclusive")]
66pub use self::inclusive::*;
67#[cfg(feature = "inclusive")]
68pub use self::inclusive_increment::*;
69
70use mech_core::MechErrorKind2;
71
72// ----------------------------------------------------------------------------
73// Range Library
74// ----------------------------------------------------------------------------
75
76#[macro_export]
77macro_rules! register_range {
78  ($fxn_name:tt, $scalar:tt, $scalar_string:tt, $row1:tt) => {
79    paste! {
80      register_descriptor! {
81        FunctionDescriptor {
82          name: concat!(stringify!($fxn_name), "<", $scalar_string , stringify!($row1), ">") ,
83          ptr: $fxn_name::<$scalar, $row1<$scalar>>::new,
84        }
85      }
86    }
87  };
88}
89
90#[derive(Debug, Clone)]
91pub struct EmptyRangeError;
92impl MechErrorKind2 for EmptyRangeError {
93  fn name(&self) -> &str { "EmptyRange" }
94  fn message(&self) -> String {
95    "Range size must be > 0".to_string()
96  }
97}
98
99#[derive(Debug, Clone)]
100pub struct RangeSizeOverflowError;
101
102impl MechErrorKind2 for RangeSizeOverflowError {
103  fn name(&self) -> &str { "RangeSizeOverflow" }
104  fn message(&self) -> String {
105    "Range size overflow".to_string()
106  }
107}
108
109#[macro_export]
110macro_rules! range_size_to_usize {
111  // Float f32 branch
112  ($diff:expr, f32) => {{
113    let v: f32 = $diff;
114    if v < 0.0 {
115      return Err(MechError2::new(
116        RangeSizeOverflowError {},
117        None
118      ).with_compiler_loc());
119    }
120    v as usize
121  }};
122  
123  // Float f64 branch
124  ($diff:expr, f64) => {{
125    let v: f64 = $diff;
126    if v < 0.0 {
127      return Err(MechError2::new(
128        RangeSizeOverflowError {},
129        None
130      ).with_compiler_loc());
131    }
132    v as usize
133  }};
134  
135  // Integer branch
136  ($diff:expr, $ty:ty) => {{
137    $diff.try_into().map_err(|_| MechError2::new(
138      RangeSizeOverflowError {},
139      None
140    ).with_compiler_loc())?
141  }};
142}