Is it possible to modify a value of an Attribute
Skallwar opened this issue · comments
The keyword mut
in &mut Foo
and calling it a "mutable" reference are misleading. It is better to think of it as an exclusive reference, with &Foo
being a shared reference.
Nodes in a Kuchiki tree are reference-counted, so there can be multiple NodeRef
s (internally Rc<Node>
) pointing to the same node, so references are almost always shared. (Rc
implements Deref
but not DerefMut
.) There is no Node::data_mut
method because it wouldn’t be very useful since it would require a &mut Node
exclusive reference that you typically don’t have.
An exclusive reference trivially enables mutability, but it’s not the only way. Cell
, RefCell
, and Mutex
are all wrapper types that provide mutability through shared references, in exchanges for some restrictions (such as RefCell
’s runtime checking that its borrow_mut
is not used while another borrow is used, so it can if fact provide an exclusive reference to its contents).
Attributes in Kuchiki are kept in the ElementData::attributes
which is a RefCell
.
Thanks for that, it's working now