Skip to main content

UnchangedOr

Struct UnchangedOr 

Source
pub struct UnchangedOr<T: ContainerElement = Any> { /* private fields */ }
Expand description

Either a replacement value of type T, or Unchanged to keep the original.

Structural map and mutation callbacks return Result<UnchangedOr<Any>>. It is like Option<T> whose None means “keep what was there”, stored in one FFI value. UnchangedOr<Any> converts with .into() from Unchanged, from a managed reference or scalar, and from a typed UnchangedOr<T>. T is an FFI value type or Any. Converting the wrapper into Any keeps the marker; use value_or_unchanged or into_option when an actual value is needed.

§Examples

use tvm_ffi::UnchangedOr;

fn double_even(value: i64) -> UnchangedOr<i64> {
    if value % 2 == 0 {
        UnchangedOr::changed(value * 2)
    } else {
        UnchangedOr::unchanged()
    }
}

assert_eq!(double_even(4).value_or_unchanged(4), 8);
assert_eq!(double_even(3).value_or_unchanged(3), 3);

Implementations§

Source§

impl<T: ContainerElement> UnchangedOr<T>

Source

pub fn unchanged() -> Self

Create a result that keeps the original value, without borrowing or cloning it.

§Returns

An unchanged result.

Source

pub fn changed(value: T) -> Self

Create a result that replaces the original value.

§Arguments
  • value - The replacement.
§Returns

A result holding value.

Source

pub fn is_unchanged(&self) -> bool

Return whether this result keeps the original value.

§Returns

true if the result is unchanged.

Source

pub fn unchanged_or_same_as(&self, original: &T) -> bool

Return whether this result is unchanged or holds original itself.

An object counts as the same only when it is the same object, not an equal copy. Use it to tell whether anything was rewritten.

§Arguments
  • original - The original value.
§Returns

true if the result is unchanged or holds original.

use tvm_ffi::{Array, UnchangedOr};
let original = Array::from_vec(vec![1_i64]);
assert!(UnchangedOr::changed(original.clone()).unchanged_or_same_as(&original));

See also UnchangedOr.

Source

pub fn into_option(self) -> Option<T>

Move out the replacement.

§Returns

The replacement, or None when the result is unchanged.

Source

pub fn value_or_unchanged(self, original: T) -> T

Move out the replacement, or original when the result is unchanged.

original is the value the callback was given; to produce it only when needed, use into_option().unwrap_or_else(..).

§Arguments
  • original - The original value, kept when the result is unchanged.
§Returns

The replacement, or original.

Source

pub fn map<U: ContainerElement>( self, convert: impl FnOnce(T) -> U, ) -> UnchangedOr<U>

Convert only the replacement, preserving an unchanged result.

§Arguments
  • convert - Converts the replacement; called only when there is one.
§Returns

The converted result.

Trait Implementations§

Source§

impl<T: ContainerElement> AnyCompatible for UnchangedOr<T>

Source§

unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny)

Write a cell that borrows src into data. Read more
Source§

unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny)

Write a cell that owns src into data. Read more
Source§

unsafe fn check_any_strict(data: &TVMFFIAny) -> bool

Whether data holds a Self as is, without conversion. Read more
Source§

unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self

Copy a Self out of a cell that holds one, retaining any object. Read more
Source§

unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self

Move a Self out of an owning cell that holds one. Read more
Source§

unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()>

Convert data into a Self by the FFI rules, such as an integer into a float; Err if it does not convert. Read more
Source§

fn type_str() -> String

The type name in conversion errors and type schemas, such as int, ffi.String or Array<int>.
Source§

fn type_schema() -> String

The type schema of the type: the JSON object that describes it, which the type schemas of functions and fields embed. Read more
Source§

unsafe fn is_undefined(field: *const Self) -> bool

Whether field, which holds a value of this type or all-zero bytes, holds all-zero bytes that are no value of this type, as a null object reference, a string of type index None or a zero device type: a field that a creator left undefined for reflection to set. An undefined field is no value; a nullable reference is Option<T>, whose None is a defined value. Read more
Source§

fn get_mismatch_type_info(data: &TVMFFIAny) -> String

The name of the mismatched value data in conversion errors; by default its runtime type key.
Source§

fn to_any(&self) -> Any

Borrow self into an owned Any, increfing object-backed values. Read more
Source§

impl<T: ContainerElement> Clone for UnchangedOr<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: ObjectRefCore> From<T> for UnchangedOr<Any>

A managed reference replaces the original value.

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl From<Unchanged> for UnchangedOr<Any>

Unchanged.into() keeps the original value.

Source§

fn from(_: Unchanged) -> Self

Converts to this type from the input type.
Source§

impl<T: ObjectRefCore> From<UnchangedOr<T>> for UnchangedOr<Any>

A typed result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<T>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<bool>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<f32>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<f64>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<i16>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<i32>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<i64>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<i64>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<i8>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<i8>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<isize>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<isize>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<u16>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<u16>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<u32>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<u32>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<u64>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<u64>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<u8>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<UnchangedOr<usize>> for UnchangedOr<Any>

A typed scalar result keeps its unchanged marker or replacement.

Source§

fn from(value: UnchangedOr<usize>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for UnchangedOr<Any>

A scalar replaces the original value.

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: ContainerElement> IntoArg<UnchangedOr<T>> for &'a UnchangedOr<T>

Source§

type Output = &'a UnchangedOr<T>

T when owned, &T when borrowed.
Source§

fn into_arg(self) -> &'a UnchangedOr<T>

Convert into a T or a &T.
Source§

fn into_owned(self) -> UnchangedOr<T>

Convert into an owned T, cloning only a borrowed value.
Source§

impl<T: ContainerElement> IntoArg<UnchangedOr<T>> for UnchangedOr<T>

Source§

type Output = UnchangedOr<T>

T when owned, &T when borrowed.
Source§

fn into_arg(self) -> UnchangedOr<T>

Convert into a T or a &T.
Source§

fn into_owned(self) -> UnchangedOr<T>

Convert into an owned T, cloning only a borrowed value.
Source§

impl<'b, T: ContainerElement> TryFrom<&'b Any> for UnchangedOr<T>

Source§

type Error = Error

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

fn try_from(value: &'b Any) -> Result<Self>

Performs the conversion.
Source§

impl<T: ContainerElement> TryFrom<Any> for UnchangedOr<T>

Source§

type Error = Error

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

fn try_from(value: Any) -> Result<Self>

Performs the conversion.
Source§

impl<'a, T: ContainerElement> TryFrom<AnyView<'a>> for UnchangedOr<T>

Source§

type Error = Error

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

fn try_from(value: AnyView<'a>) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

§

impl<T> Freeze for UnchangedOr<T>

§

impl<T> RefUnwindSafe for UnchangedOr<T>
where T: RefUnwindSafe,

§

impl<T = Any> !Send for UnchangedOr<T>

§

impl<T = Any> !Sync for UnchangedOr<T>

§

impl<T> Unpin for UnchangedOr<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for UnchangedOr<T>

§

impl<T> UnwindSafe for UnchangedOr<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> ContainerElement for T
where T: AnyCompatible,

Source§

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