base_traits/traits/
as_i32.rs

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