Crate pathmod_core

Crate pathmod_core 

Source
Expand description

Pathmod Core — runtime types for composable field accessors

This crate provides the runtime Accessor<T, F> type used by the derive macros in pathmod_derive and re-exported by the pathmod crate. An Accessor<T, F> is a small, Copy value that focuses into a field F within a root T.

Highlights

  • Zero-cost composition: Accessor stores a byte offset from &T to &F; composing accessors is O(1) offset addition.
  • Safe surface: All public APIs are safe. Internals use pointer arithmetic; constructors compute valid offsets for you.
  • Clear clone semantics (MVP): set_clone only requires F: Clone and does not require T: Clone, even when composed deeply.

Quick example

use pathmod_core::Accessor;

#[derive(Debug, PartialEq)]
struct Bar { x: i32 }
#[derive(Debug, PartialEq)]
struct Foo { a: i32, b: Bar }

fn acc_b() -> Accessor<Foo, Bar> {
    fn get_ref(f: &Foo) -> &Bar { &f.b }
    fn get_mut(f: &mut Foo) -> &mut Bar { &mut f.b }
    Accessor::from_fns(get_ref, get_mut)
}
fn acc_x() -> Accessor<Bar, i32> {
    fn get_ref(b: &Bar) -> &i32 { &b.x }
    fn get_mut(b: &mut Bar) -> &mut i32 { &mut b.x }
    Accessor::from_fns(get_ref, get_mut)
}

let mut foo = Foo { a: 1, b: Bar { x: 2 } };
let acc = acc_b().compose(acc_x());
acc.set_mut(&mut foo, |v| *v += 5);
assert_eq!(foo.b.x, 7);

Safety notes

  • Internally, accessors are represented by a byte offset and use unsafe pointer arithmetic to project fields. The public API is safe when accessors are constructed by the provided derive macros or from_fns.

Modules§

prelude

Structs§

Accessor
A small, copyable accessor that focuses into a field F inside a root T.

Traits§

Indexing
Indexing operations for accessors that focus Vec<E>.