pub struct MutationView<'a, T = Any> { /* private fields */ }Expand description
A borrowed value that a structural mutation may rewrite in place.
Mutation handlers receive a MutationView instead of an owned value, so
matching a node does not retain it. Inspect the node through the view,
then pass the view to StructuralMutatorView::default_mutate to rewrite
its children, or rewrite one field with field.
When the node is uniquely owned and the view carries
InplaceMode::Allow, its storage is reused instead of copied.
T is the matched type: MutationView<'_, Array<Any>> derefs to the
array’s node, and the default MutationView<'_> matches every value. The
view is move-only: it cannot be copied or cloned, and consuming it passes
on the same mode. A view made from &mut Any may reuse storage;
one made with borrowed never does.
See also structural_mutate.
Implementations§
Source§impl<'a> MutationView<'a>
impl<'a> MutationView<'a>
Sourcepub fn borrowed(value: AnyView<'a>) -> Self
pub fn borrowed(value: AnyView<'a>) -> Self
Create a view of a borrowed value that never reuses its storage.
The mutation result is always a new value or Unchanged. To allow reuse,
make the view from &mut Any instead.
§Arguments
value- The value to view.
§Returns
A view with InplaceMode::Disallow.
Source§impl<'a, T> MutationView<'a, T>
impl<'a, T> MutationView<'a, T>
Sourcepub fn inplace_mode(&self) -> InplaceMode
pub fn inplace_mode(&self) -> InplaceMode
Return the in-place mode this view carries.
§Returns
Whether this view may reuse its value’s storage.
Sourcepub fn narrow_inplace_mode(self, requested: InplaceMode) -> Self
pub fn narrow_inplace_mode(self, requested: InplaceMode) -> Self
Sourcepub fn as_any_view(&self) -> AnyView<'_>
pub fn as_any_view(&self) -> AnyView<'_>
Sourcepub fn downcast_ref<U: DowncastRefFromAny>(&self) -> Option<&U>
pub fn downcast_ref<U: DowncastRefFromAny>(&self) -> Option<&U>
Borrow the value as U without changing ownership or mode.
U is anything AnyView::downcast_ref borrows: a managed reference such as
Array<i64>, an object type such as ArrayObj, or Any, borrowed in place
without a reference-count bump. The check is strict, and the reference borrows
this view and cannot survive its consumption.
§Returns
The borrowed value, or None when the value is not a U.
Sourcepub fn is<U: AnyCompatible>(&self) -> bool
pub fn is<U: AnyCompatible>(&self) -> bool
Whether the value is a U, as Any::is.
Sourcepub fn downcast<U: ContainerElement>(self) -> Result<MutationView<'a, U>, Self>
pub fn downcast<U: ContainerElement>(self) -> Result<MutationView<'a, U>, Self>
Narrow the matched type without acquiring an owning reference.
The check is the strict one of match_any!: only a value that fits U as
is, elements included, matches. The view keeps its mode.
§Returns
The view typed as MutationView<'_, U>.
§Errors
Returns the view unchanged when the value is not a U, so another type can
be tried.
Sourcepub fn into_untyped(self) -> MutationView<'a>
pub fn into_untyped(self) -> MutationView<'a>
Source§impl<T: ObjectRefCore> MutationView<'_, T>
impl<T: ObjectRefCore> MutationView<'_, T>
Sourcepub fn field<U: MutationField>(
&mut self,
selector: impl for<'node> FnOnce(&'node T::ContainerType) -> &'node U,
) -> MutationView<'_, U>
pub fn field<U: MutationField>( &mut self, selector: impl for<'node> FnOnce(&'node T::ContainerType) -> &'node U, ) -> MutationView<'_, U>
Borrow a direct owning field of the node as a view of its own, to rewrite it
with StructuralMutatorView::mutate.
The field view exclusively borrows this view. It may reuse the field’s
storage only when this view may and the node remains uniquely owned. This
does not write the result back into the node: keep the node Unchanged or
rebuild it from the result.
§Arguments
selector- Returns a reference to one of the node’s own owning fields, anAnyor a managed reference.
§Returns
A view of the field.
§Panics
Panics if selector returns a reference outside the node.
let second = mutator.mutate(self, pair.field(|pair| &pair.second))?;See also structural_mutate.