rust_3d/
strong_types.rs

1/*
2Copyright 2017 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Containing strong type definitions for safer usage
24
25use std::fmt;
26
27//------------------------------------------------------------------------------
28
29macro_rules! strong_usize {
30    ($NEW_NAME:ident) => {
31        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
32        pub struct $NEW_NAME {
33            pub val: usize,
34        }
35
36        impl Into<usize> for $NEW_NAME {
37            fn into(self) -> usize {
38                self.val
39            }
40        }
41
42        impl AsRef<usize> for $NEW_NAME {
43            fn as_ref(&self) -> &usize {
44                &self.val
45            }
46        }
47
48        impl AsMut<usize> for $NEW_NAME {
49            fn as_mut(&mut self) -> &mut usize {
50                &mut self.val
51            }
52        }
53
54        impl fmt::Display for $NEW_NAME {
55            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56                write!(f, "{}", self.val)
57            }
58        }
59    };
60}
61
62macro_rules! strong_f64 {
63    ($NEW_NAME:ident) => {
64        #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
65        pub struct $NEW_NAME {
66            pub val: f64,
67        }
68
69        impl Into<f64> for $NEW_NAME {
70            fn into(self) -> f64 {
71                self.val
72            }
73        }
74
75        impl AsRef<f64> for $NEW_NAME {
76            fn as_ref(&self) -> &f64 {
77                &self.val
78            }
79        }
80
81        impl AsMut<f64> for $NEW_NAME {
82            fn as_mut(&mut self) -> &mut f64 {
83                &mut self.val
84            }
85        }
86
87        impl fmt::Display for $NEW_NAME {
88            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89                write!(f, "{}", self.val)
90            }
91        }
92    };
93}
94
95//------------------------------------------------------------------------------
96
97strong_usize!(VId);
98strong_usize!(FId);
99strong_usize!(EId);
100strong_usize!(SId);
101
102strong_f64!(Deg);
103strong_f64!(Rad);