use std::ptr::NonNull;
use serde_json::Value;
use super::super::JsonTreeControl;
use super::super::JsonTreeMutVisitor;
use super::MutChildCursor;
use super::OwnedLocation;
#[must_use]
pub(in crate::value::traverse) struct MutFrame {
pub(in crate::value::traverse) location: OwnedLocation,
pub(in crate::value::traverse) depth: usize,
value: NonNull<Value>,
pub(in crate::value::traverse) entered: bool,
pub(in crate::value::traverse) finished: bool,
cursor: Option<MutChildCursor>,
}
impl MutFrame {
#[inline(always)]
pub(in crate::value::traverse) unsafe fn root(value: &mut Value) -> Self {
Self {
location: OwnedLocation::Root,
depth: 1,
value: NonNull::from(value),
entered: false,
finished: false,
cursor: None,
}
}
#[inline(always)]
pub(in crate::value::traverse) unsafe fn child(
location: OwnedLocation,
depth: usize,
value: &mut Value,
) -> Self {
Self {
location,
depth,
value: NonNull::from(value),
entered: false,
finished: false,
cursor: None,
}
}
pub(in crate::value::traverse) unsafe fn next_child(&mut self) -> Option<Self> {
if self.finished {
return None;
}
let cursor = self
.cursor
.get_or_insert_with(|| unsafe { MutChildCursor::new(self.value) });
unsafe { cursor.next(self.depth) }
}
pub(in crate::value::traverse) unsafe fn visit<V>(
&mut self,
visitor: &mut V,
) -> Result<JsonTreeControl, V::Error>
where
V: JsonTreeMutVisitor,
{
let context = self.location.context(self.depth);
let value = unsafe { self.value.as_mut() };
visitor.visit(value, context)
}
}