base_traits/traits/
as_u32.rs

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