From 71c3c54859611dc429660533ffdd3bfdf1f60a86 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 1 May 2024 17:46:54 +0200 Subject: [PATCH] feat(apb): allow editing inner nodes actually only if it implements Clone but whatever good enough for now --- apb/src/node.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apb/src/node.rs b/apb/src/node.rs index 53b967f..ce19419 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -16,6 +16,9 @@ impl From> for Node { } } +// TODO how do i move out of the box for a moment? i need to leave it uninitialized while i update +// the value and then put it back, i think it should be safe to do so! but i'm not sure how, so i'm +// using a clone (expensive but simple solution) impl Iterator for Node { type Item = T; @@ -31,6 +34,14 @@ impl Iterator for Node { } } +impl Node { + pub fn update(&mut self, builder: impl FnOnce(T) -> T) { + if let Node::Object(x) = self { + *x = Box::new(builder((**x).clone())); + } + } +} + impl Node { /// return reference to embedded object (or last if many are present) pub fn get(&self) -> Option<&T> {