pub trait FieldAccess: AnyFieldAccess {
// Provided methods
fn field(&self, field: &str) -> Option<Field<'_>> { ... }
fn field_mut(&mut self, field: &str) -> Option<FieldMut<'_>> { ... }
fn fields(&self) -> Fields<'_> ⓘ
where Self: Sized { ... }
}Expand description
High-level struct field access.
This trait is automatically implemented by any type implementing
AnyFieldAccess. See its documentation for more details.
Provided Methods§
Sourcefn field(&self, field: &str) -> Option<Field<'_>>
fn field(&self, field: &str) -> Option<Field<'_>>
Immutable field access.
Returns Some(_) if the field is accessible, otherwise None.
The returned Field provides methods to immutably interact with the field. See its
documentation for more.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8
}
let foo = Foo { a: 1 };
assert!(foo.field("a").is_some());
assert!(foo.field("b").is_none());Sourcefn field_mut(&mut self, field: &str) -> Option<FieldMut<'_>>
fn field_mut(&mut self, field: &str) -> Option<FieldMut<'_>>
Mutable field access.
Returns Some(_) if the field is accessible, otherwise None.
The returned FieldMut provides methods to mutably interact with the field. See its
documentation for more.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8
}
let mut foo = Foo { a: 1 };
assert!(foo.field_mut("a").is_some());
assert!(foo.field_mut("b").is_none());Sourcefn fields(&self) -> Fields<'_> ⓘwhere
Self: Sized,
fn fields(&self) -> Fields<'_> ⓘwhere
Self: Sized,
Returns an iterator over all struct fields.
The order of the items yielded by the iterator is undefined and should not be relied upon.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8,
b: u16,
c: u32
}
let foo = Foo { a: 1, b: 2, c: 3 };
let tuples: Vec<_> = foo.fields()
.map(|(name, field)| (name, field.as_u8()))
.collect();
assert_eq!(tuples, &[("a", Some(1)),
("b", Some(2)),
("c", Some(3))])