Skip to main content

MutationView

Struct MutationView 

Source
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>

Source

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>

Source

pub fn inplace_mode(&self) -> InplaceMode

Return the in-place mode this view carries.

§Returns

Whether this view may reuse its value’s storage.

Source

pub fn narrow_inplace_mode(self, requested: InplaceMode) -> Self

Restrict the mode, without ever upgrading it.

Disallow turns off reuse. Allow keeps the current mode, so it never grants reuse to a view that does not have it, such as a borrowed one.

§Arguments
  • requested - The mode to restrict to.
§Returns

The view with the restricted mode.

Source

pub fn as_any_view(&self) -> AnyView<'_>

Borrow the current value.

The view and its copies borrow this handle, so their last use must precede consuming it for default mutation.

§Returns

The value as an AnyView.

Source

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.

Source

pub fn is<U: AnyCompatible>(&self) -> bool

Whether the value is a U, as Any::is.

Source

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.

Source

pub fn into_untyped(self) -> MutationView<'a>

Erase the matched type while preserving the carried mode.

§Returns

The view as MutationView<'_>.

Source§

impl<T: ObjectRefCore> MutationView<'_, T>

Source

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, an Any or 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.

Trait Implementations§

Source§

impl<T: ObjectRefCore> Deref for MutationView<'_, T>

Source§

type Target = <T as ObjectRefCore>::ContainerType

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T> From<&'a MutationView<'_, T>> for AnyView<'a>

Source§

fn from(value: &'a MutationView<'_, T>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a mut Any> for MutationView<'a>

Source§

fn from(value: &'a mut Any) -> Self

Borrow an owner exclusively without acquiring a reference count.

Reuse still requires the runtime’s uniqueness and alias checks.

Auto Trait Implementations§

§

impl<'a, T = Any> !Send for MutationView<'a, T>

§

impl<'a, T = Any> !Sync for MutationView<'a, T>

§

impl<'a, T> Freeze for MutationView<'a, T>

§

impl<'a, T> RefUnwindSafe for MutationView<'a, T>

§

impl<'a, T> Unpin for MutationView<'a, T>

§

impl<'a, T> UnsafeUnpin for MutationView<'a, T>

§

impl<'a, T> UnwindSafe for MutationView<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeSubsumes<T> for T
where T: ?Sized,