9 releases

new 0.1.7 May 2, 2024
0.1.6 Apr 2, 2024
0.1.5 Mar 1, 2024
0.1.4 Feb 3, 2024
0.0.1 Oct 16, 2023

#434 in Data structures

Download history 1/week @ 2024-01-04 1/week @ 2024-02-01 11/week @ 2024-02-15 26/week @ 2024-02-22 173/week @ 2024-02-29 21/week @ 2024-03-07 23/week @ 2024-03-14 27/week @ 2024-03-21 124/week @ 2024-03-28 52/week @ 2024-04-04 4/week @ 2024-04-11

181 downloads per month
Used in lcax

MIT/Apache

31KB
320 lines

field_access

Build Status crates.io docs.rs License: Apache 2.0 License: 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.

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<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.

License

The source code of field_access is licensed under either of Apache License, Version 2.0 or MIT license at your option.

Dependencies

~120KB