base_traits/traits/
as_u64.rs

1// src/traits/as_u64.rs : `AsU64`
2
3/// Trait defining instance method `as_u64() : u64` that provides a
4/// cost-free conversion into `u64`.
5///
6/// It is expected that the implementing type "is-a" `u64` in a direct
7/// manner as well as in a logical manner.
8///
9/// # Additional Implementations on Foreign Types
10///
11/// ## Built-in Types
12///
13/// If the feature `"implement-AsU64-for-built_ins"`
14/// is defined (as it is by `"default"`), then this is also implemented
15/// for the following type(s):
16/// - [`u64`];
17pub trait AsU64 {
18    fn as_u64(&self) -> u64;
19}
20
21
22#[cfg(feature = "implement-AsU64-for-built_ins")]
23mod impl_for_built_ins {
24    #![allow(non_snake_case)]
25
26
27    impl super::AsU64 for u64 {
28        #[inline]
29        fn as_u64(&self) -> u64 {
30            *self
31        }
32    }
33}
34
35
36#[cfg(test)]
37mod tests {
38    #![allow(non_snake_case)]
39
40    use super::AsU64;
41
42
43    #[cfg(feature = "implement-AsU64-for-built_ins")]
44    #[test]
45    fn TEST_u64_AsU64() {
46
47        {
48            let v : u64 = 12345678;
49            let actual = v.as_u64();
50
51            assert_eq!(12345678, actual);
52        }
53
54        {
55            let v : &u64 = &12345678;
56            let actual = v.as_u64();
57
58            assert_eq!(12345678, actual);
59        }
60    }
61}
62
63
64// ///////////////////////////// end of file //////////////////////////// //
65