https://github.com/martinohmann/field_access
Dynamic access to struct fields
https://github.com/martinohmann/field_access
access dynamic fields rust struct
Last synced: about 1 month ago
JSON representation
Dynamic access to struct fields
- Host: GitHub
- URL: https://github.com/martinohmann/field_access
- Owner: martinohmann
- License: apache-2.0
- Created: 2023-10-12T18:30:30.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-04T15:25:02.000Z (over 1 year ago)
- Last Synced: 2024-11-04T16:20:57.367Z (over 1 year ago)
- Topics: access, dynamic, fields, rust, struct
- Language: Rust
- Homepage:
- Size: 140 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# field_access
[](https://github.com/martinohmann/field_access/actions?query=workflow%3Aci)
[](https://crates.io/crates/field_access)
[](https://docs.rs/field_access)
[](https://opensource.org/licenses/Apache-2.0)
[](https://opensource.org/licenses/MIT)
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.
```rust
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);
```
## Cargo features
- `alloc`: Provide methods to interact with types from the Rust core allocation
and collections library including `String` and `Vec`. 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.
## License
The source code of field_access is licensed under either of
[Apache License, Version 2.0](https://github.com/martinohmann/field_access/blob/main/LICENSE-APACHE) or
[MIT license](https://github.com/martinohmann/field_access/blob/main/LICENSE-MIT) at
your option.