#field #derive #macro-derive #optional #struct #is-some #test-struct

macro has-some-field

Check whether a struct has any field that is_some()

1 unstable release

0.1.0 Jan 28, 2025

#581 in Procedural macros

Download history 91/week @ 2025-01-26 22/week @ 2025-02-02

113 downloads per month

MIT/Apache

8KB
57 lines

has-some-field

Check whether a struct has any field that is_some().

Usage

Use the HasSomeField derive macro on a struct with optional fields.

  • has_some_field() - Returns whether any of the fields is_some().
  • some_field_count() - Returns the number of optional fields that is_some().

Example

#[derive(HasSomeField)]
struct TestStruct {
    a: Option<u8>,
    b: Option<u8>,
}
let test_struct = TestStruct {
    a: Some(5),
    b: Some(5),
};
assert_eq!(test_struct.some_field_count(), 2);
let test_struct = TestStruct { a: None, b: None };
assert!(!test_struct.has_some_field());
let test_struct = TestStruct {
    a: Some(5),
    b: None,
};
assert!(test_struct.has_some_field());

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any Contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~210–650KB
~15K SLoC