3 releases (breaking)
Uses new Rust 2024
| new 0.3.0 | Mar 7, 2026 |
|---|---|
| 0.2.0 | Oct 21, 2025 |
| 0.1.1 |
|
| 0.1.0 | Oct 16, 2025 |
#1075 in Rust patterns
89 downloads per month
Used in 2 crates
(via motiongfx)
24KB
386 lines
Field Path
Field Path provides a lightweight and type-safe abstraction for referencing and accessing nested fields within structs.
The crate is designed to make it easier to generically inspect or mutate fields without relying on heavy reflection systems or unsafe code. It does this through a combination of field identifiers and accessors that preserve type information.
Core Concepts
-
Field: Represents a unique, type-safe identifier for a field path within a struct. -
Accessor: A generic wrapper providing read and write access to a field. -
FieldAccessorRegistry: A mapping between fields and their accessors for lookup and dynamic use.(only available with the "registry" feature, enabled by default)
Together, these components allow building flexible systems that can access or manipulate struct data without tightly coupling to specific types.
Example
use field_path::registry::FieldAccessorRegistry;
use field_path::field;
use field_path::accessor;
#[derive(Default)]
struct Vec2<T> {
pub x: T,
pub y: T,
}
let mut registry = FieldAccessorRegistry::default();
let field = field!(<Vec2<f32>>::x);
// Register accessors.
registry.register_typed(field, accessor!(<Vec2<f32>>::x));
// Access field generically.
let mut v = Vec2::default();
let accessor =
registry.get::<Vec2<f32>, f32>(&field.untyped()).unwrap();
*accessor.get_mut(&mut v) = 42.0;
assert_eq!(accessor.get_ref(&v), &42.0);
Join the community!
You can join us on the Voxell discord server.
License
field_path is dual-licensed under either:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
This means you can select the license you prefer! This dual-licensing approach is the de-facto standard in the Rust ecosystem and there are very good reasons to include both.
Dependencies
~150KB