pub trait Meta {
type Data;
}Expand description
Defines constant metadata to be stored alongside the primary item in a KeyCell.
§Purpose
Metadata is constant data associated with a particular KeyCell. Its
primary purpose is to provide references to other KeyCells. If these other
KeyCells were instead stored inside the original KeyCell, then any
access to those would require us to clone them out before use.
§Examples
The following example shows how we will need to clone out other because
there is no way to access it after we have split this into a &KeyCell<_>
and a &mut Key.
use mutcy::{Key, KeyCell, KeyMut};
use std::rc::Rc;
struct A {
other: Rc<KeyCell<i32>>,
}
fn function(this: KeyMut<A>) {
// We require this clone...
let other = this.other.clone();
// because we can no longer access `this`...
let (_this, key) = Key::split(this);
// to increment `other`.
*other.borrow_mut(key) += 1;
}Metadata solves the issue of requiring clones.
use mutcy::{Key, KeyCell, KeyMut, Meta};
use std::rc::Rc;
struct A {
// ...
}
impl Meta for A {
type Data = Rc<KeyCell<i32>>;
}
fn function(this: KeyMut<A>) {
// We don't need to clone anything before splitting...
let (input, key) = Key::split(this);
// because we can now access the `KeyCell` metadata via `meta`.
*input.meta().borrow_mut(key) += 1;
}