Crates.io | field_access |
lib.rs | field_access |
version | 0.1.11 |
source | src |
created_at | 2023-10-16 18:43:39.318589 |
updated_at | 2024-11-04 15:24:59.672691 |
description | Dynamically access struct fields |
homepage | |
repository | https://github.com/martinohmann/field_access |
max_upload_size | |
id | 1005003 |
size | 41,417 |
A library for dynamic access to struct fields with #![no_std]
support.
Field access is enabled by the FieldAccess
trait which can be implemented
using a derive macro by the same name.
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8
}
let mut foo = Foo { a: 1 };
// Immutable field access.
if let Some(field) = foo.field("a") {
assert_eq!(field.as_u8(), Some(1));
}
// Mutable field access.
if let Some(mut field) = foo.field_mut("a") {
assert_eq!(field.replace(42u8), Some(1));
}
assert_eq!(foo.a, 42);
alloc
: Provide methods to interact with types from the Rust core allocation
and collections library including String
and Vec<T>
. This feature pulls
in the alloc
library as a dependency and is enabled by default.derive
: Provide a derive macro for the FieldAccess
trait. This feature is
enabled by default.The source code of field_access is licensed under either of Apache License, Version 2.0 or MIT license at your option.