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
/// A data structure the exposes the number of fields it has.
///
/// This trait can be derived:
///
/// ```
/// use field_count::FieldCount;
///
/// #[derive(FieldCount)]
/// struct MyStruct
/// {
///    first_field: i32,
///    second_field: String,
///    third_field: u16,
/// }
///
/// println!("{}", MyStruct::field_count()); // 3
/// ```
pub trait FieldCount {
    /// Get the number of fields on a struct.
    fn field_count() -> usize;
}

// Export derive macro from derive crate.
pub use field_count_derive::*;

#[cfg(test)]
mod tests {
    use super::FieldCount;

    #[test]
    fn test_derive_field_count_for_struct() {
        assert_eq!(MyStruct::field_count(), 3);
    }

    #[test]
    fn test_derive_field_count_for_generic_struct() {
        assert_eq!(MyGenericStruct::<u32>::field_count(), 1);
    }

    #[test]
    fn test_derive_field_count_as_trait() {
        assert_eq!(field_count::<MyStruct>(), 3);
    }

    fn field_count<T: FieldCount>() -> usize {
        T::field_count()
    }

    #[derive(FieldCount)]
    struct MyStruct {
        _first_field: i32,
        _second_field: String,
        _third_field: u16,
    }

    #[derive(FieldCount)]
    struct MyGenericStruct<T> {
        _generic_field: T,
    }
}