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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// src/traits/as_f64.rs : `AsF64`
/// Trait defining instance method `as_f64() : f64` that provides a
/// cost-free conversion into `f64`.
///
/// It is expected that the implementing type "is-a" `f64` in a direct
/// manner as well as in a logical manner.
///
/// # Additional Implementations on Foreign Types
///
/// ## Built-in Types
///
/// If the feature `"implement-AsF64-for-built_ins"`
/// is defined (as it is by `"default"`), then this is also implemented
/// for the following type(s):
/// - [`f64`];
pub trait AsF64 {
fn as_f64(&self) -> f64;
}
#[cfg(all(not(test), not(feature = "nostd")))]
impl<T : AsF64 + ?Sized> AsF64 for Box<T> {
fn as_f64(&self) -> f64 {
(**self).as_f64()
}
}
#[cfg(all(not(test), not(feature = "nostd")))]
impl<T : AsF64 + ?Sized> AsF64 for std::rc::Rc<T> {
fn as_f64(&self) -> f64 {
(**self).as_f64()
}
}
#[cfg(feature = "implement-AsF64-for-built_ins")]
mod impl_for_built_ins {
#![allow(non_snake_case)]
impl super::AsF64 for f64 {
#[inline]
fn as_f64(&self) -> f64 {
*self
}
}
impl super::AsF64 for &f64 {
#[inline]
fn as_f64(&self) -> f64 {
**self
}
}
}
#[cfg(test)]
mod tests {
#![allow(non_snake_case)]
use super::AsF64;
use std::rc::Rc;
#[allow(unused)]
fn as_AsF64<T : AsF64>(t : &T) -> &impl AsF64 {
t
}
mod TEST_CUSTOM_TYPE {
#![allow(non_snake_case)]
use super::*;
#[derive(Debug)]
struct CustomType {
value : f64,
}
impl AsF64 for CustomType {
fn as_f64(&self) -> f64 {
self.value
}
}
#[test]
fn TEST_WHEN_ZERO() {
let ct = CustomType { value : 0.0 };
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
#[test]
fn TEST_WHEN_NONZERO() {
let ct = CustomType { value : 1.0 };
assert_ne!(0.0, ct.as_f64());
let ct = &ct;
assert_ne!(0.0, ct.as_f64());
}
#[test]
fn TEST_WHEN_ZERO_IN_Box() {
{
let ct = Box::new(CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
{
let ct = &Box::new(CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
{
let ct = Box::new(&CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
{
let ct = &Box::new(&CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
}
#[test]
fn TEST_WHEN_ZERO_IN_Rc() {
{
let ct = Rc::new(CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
{
let ct = &Rc::new(CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
{
let ct = Rc::new(&CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
{
let ct = &Rc::new(&CustomType { value : 0.0 });
assert_eq!(0.0, ct.as_f64());
let ct = &ct;
assert_eq!(0.0, ct.as_f64());
}
}
}
#[cfg(feature = "implement-AsF64-for-built_ins")]
mod TEST_BUILTIN_TYPES {
#![allow(non_snake_case)]
use super::*;
mod TEST_f64 {
#![allow(non_snake_case)]
use super::*;
#[test]
fn TEST_ZERO() {
let v = 0.0f64;
assert_eq!(0.0, v.as_f64());
let ie = as_AsF64(&v);
assert_eq!(0.0, ie.as_f64());
}
#[test]
fn TEST_NONZERO() {
let v = -123.456f64;
assert_ne!(0.0, v.as_f64());
let ie = as_AsF64(&v);
assert_ne!(0.0, ie.as_f64());
}
}
}
}
// ///////////////////////////// end of file //////////////////////////// //