Patch

Enum Patch 

Source
pub enum Patch<T> {
    Some(T),
    Empty,
    None,
}
Expand description

A tri-state type for partial update semantics.

Patch<T> distinguishes between three states:

  • Patch::Some(T) — a value is present
  • Patch::Empty — no value was provided (field absent)
  • Patch::None — value was explicitly set to null

This is particularly useful for PATCH/UPDATE operations where you need to distinguish between “don’t change this field” and “clear this field”.

§Examples

use value_extra::Patch;

let some = Patch::Some(42);
let empty: Patch<i32> = Patch::Empty;
let none: Patch<i32> = Patch::None;

assert!(some.is_some());
assert!(empty.is_empty());
assert!(none.is_none());

Variants§

§

Some(T)

A value is present.

§

Empty

No value was provided (field absent).

§

None

Value was explicitly set to null.

Implementations§

Source§

impl<T> Patch<T>

Source

pub fn is_some(&self) -> bool

Returns true if the patch contains a value.

§Examples
use value_extra::Patch;

assert!(Patch::Some(42).is_some());
assert!(!Patch::<i32>::Empty.is_some());
assert!(!Patch::<i32>::None.is_some());
Source

pub fn is_empty(&self) -> bool

Returns true if the patch is empty (field absent).

§Examples
use value_extra::Patch;

assert!(Patch::<i32>::Empty.is_empty());
assert!(!Patch::Some(42).is_empty());
assert!(!Patch::<i32>::None.is_empty());
Source

pub fn is_none(&self) -> bool

Returns true if the patch is explicitly null.

§Examples
use value_extra::Patch;

assert!(Patch::<i32>::None.is_none());
assert!(!Patch::Some(42).is_none());
assert!(!Patch::<i32>::Empty.is_none());
Source

pub fn is_null(&self) -> bool

Alias for is_none. Returns true if explicitly null.

This alias may be clearer in JSON/API contexts where “null” is the term used.

Source

pub fn contains<U>(&self, x: &U) -> bool
where T: PartialEq<U>,

Returns true if the patch contains the given value.

§Examples
use value_extra::Patch;

assert!(Patch::Some(42).contains(&42));
assert!(!Patch::Some(42).contains(&0));
assert!(!Patch::<i32>::Empty.contains(&42));
Source§

impl<T> Patch<T>

Source

pub fn as_ref(&self) -> Patch<&T>

Converts from &Patch<T> to Patch<&T>.

§Examples
use value_extra::Patch;

let patch = Patch::Some(String::from("hello"));
assert_eq!(patch.as_ref(), Patch::Some(&String::from("hello")));
Source

pub fn as_mut(&mut self) -> Patch<&mut T>

Converts from &mut Patch<T> to Patch<&mut T>.

§Examples
use value_extra::Patch;

let mut patch = Patch::Some(42);
if let Patch::Some(v) = patch.as_mut() {
    *v = 100;
}
assert_eq!(patch, Patch::Some(100));
Source§

impl<T: Deref> Patch<T>

Source

pub fn as_deref(&self) -> Patch<&<T as Deref>::Target>

Converts from &Patch<T> to Patch<&T::Target>.

Useful for converting Patch<String> to Patch<&str>.

§Examples
use value_extra::Patch;

let patch = Patch::Some(String::from("hello"));
assert_eq!(patch.as_deref(), Patch::Some("hello"));
Source§

impl<T> Patch<T>

Source

pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Patch<U>

Maps a Patch<T> to Patch<U> by applying a function to the contained value.

Empty and None are propagated unchanged.

§Examples
use value_extra::Patch;

let patch = Patch::Some(21);
assert_eq!(patch.map(|x| x * 2), Patch::Some(42));

let empty: Patch<i32> = Patch::Empty;
assert_eq!(empty.map(|x| x * 2), Patch::Empty);
Source

pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U

Returns the provided default if Empty or None, otherwise applies f to the value.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(21).map_or(0, |x| x * 2), 42);
assert_eq!(Patch::<i32>::Empty.map_or(0, |x| x * 2), 0);
assert_eq!(Patch::<i32>::None.map_or(0, |x| x * 2), 0);
Source

pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
where D: FnOnce() -> U, F: FnOnce(T) -> U,

Computes a default from a closure if Empty or None, otherwise applies f.

§Examples
use value_extra::Patch;

let result = Patch::Some(21).map_or_else(|| 0, |x| x * 2);
assert_eq!(result, 42);
Source

pub fn and_then<U, F>(self, f: F) -> Patch<U>
where F: FnOnce(T) -> Patch<U>,

Calls f with the value if Some, returning the result.

Empty and None are propagated unchanged.

§Examples
use value_extra::Patch;

fn parse_positive(s: String) -> Patch<u32> {
    s.parse::<u32>().ok().filter(|&n| n > 0).into()
}

assert_eq!(Patch::Some("42".to_string()).and_then(parse_positive), Patch::Some(42));
assert_eq!(Patch::Some("0".to_string()).and_then(parse_positive), Patch::None);
assert_eq!(Patch::<String>::Empty.and_then(parse_positive), Patch::Empty);
Source

pub fn filter<P>(self, predicate: P) -> Patch<T>
where P: FnOnce(&T) -> bool,

Returns Patch::None if the predicate returns false, otherwise returns self.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(42).filter(|&x| x > 0), Patch::Some(42));
assert_eq!(Patch::Some(-1).filter(|&x| x > 0), Patch::None);
assert_eq!(Patch::<i32>::Empty.filter(|&x| x > 0), Patch::Empty);
Source§

impl<T> Patch<T>

Source

pub fn expect(self, msg: &str) -> T

Returns the contained value, panicking with a custom message if not Some.

§Panics

Panics if the patch is Empty or None.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(42).expect("should have value"), 42);
Source

pub fn unwrap(self) -> T

Returns the contained value, panicking if not Some.

§Panics

Panics if the patch is Empty or None.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(42).unwrap(), 42);
Source

pub fn unwrap_or(self, default: T) -> T

Returns the contained value or a default.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(42).unwrap_or(0), 42);
assert_eq!(Patch::<i32>::Empty.unwrap_or(0), 0);
assert_eq!(Patch::<i32>::None.unwrap_or(0), 0);
Source

pub fn unwrap_or_else<F>(self, f: F) -> T
where F: FnOnce() -> T,

Returns the contained value or computes it from a closure.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(42).unwrap_or_else(|| 0), 42);
assert_eq!(Patch::<i32>::Empty.unwrap_or_else(|| 100), 100);
Source§

impl<T: Default> Patch<T>

Source

pub fn unwrap_or_default(self) -> T

Returns the contained value or the default for T.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(42).unwrap_or_default(), 42);
assert_eq!(Patch::<i32>::Empty.unwrap_or_default(), 0);
Source§

impl<T> Patch<T>

Source

pub fn or(self, other: Patch<T>) -> Patch<T>

Returns self if it contains a value, otherwise returns other.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(1).or(Patch::Some(2)), Patch::Some(1));
assert_eq!(Patch::<i32>::Empty.or(Patch::Some(2)), Patch::Some(2));
assert_eq!(Patch::<i32>::None.or(Patch::Some(2)), Patch::Some(2));
Source

pub fn or_else<F>(self, f: F) -> Patch<T>
where F: FnOnce() -> Patch<T>,

Returns self if it contains a value, otherwise calls f.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(1).or_else(|| Patch::Some(2)), Patch::Some(1));
assert_eq!(Patch::<i32>::Empty.or_else(|| Patch::Some(2)), Patch::Some(2));
Source

pub fn xor(self, other: Patch<T>) -> Patch<T>

Returns Some if exactly one of self or other is Some.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(1).xor(Patch::<i32>::Empty), Patch::Some(1));
assert_eq!(Patch::<i32>::None.xor(Patch::Some(2)), Patch::Some(2));
assert_eq!(Patch::Some(1).xor(Patch::Some(2)), Patch::None);
assert_eq!(Patch::<i32>::Empty.xor(Patch::<i32>::None), Patch::Empty);
assert_eq!(Patch::<i32>::None.xor(Patch::<i32>::None), Patch::None);
Source

pub fn zip<U>(self, other: Patch<U>) -> Patch<(T, U)>

Zips self with another Patch.

Returns Some((a, b)) if both are Some, otherwise propagates Empty or None.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(1).zip(Patch::Some("a")), Patch::Some((1, "a")));
assert_eq!(Patch::Some(1).zip(Patch::<&str>::Empty), Patch::Empty);
assert_eq!(Patch::<i32>::None.zip(Patch::Some("a")), Patch::None);
Source

pub fn zip_with<U, R, F>(self, other: Patch<U>, f: F) -> Patch<R>
where F: FnOnce(T, U) -> R,

Zips self with another Patch using a function.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(1).zip_with(Patch::Some(2), |a, b| a + b), Patch::Some(3));
Source§

impl<T> Patch<T>

Source

pub fn insert(&mut self, value: T) -> &mut T

Inserts a value, returning a mutable reference to it.

§Examples
use value_extra::Patch;

let mut patch = Patch::<i32>::Empty;
let v = patch.insert(42);
assert_eq!(*v, 42);
Source

pub fn get_or_insert(&mut self, value: T) -> &mut T

Inserts a value if Empty or None, returning a mutable reference.

§Examples
use value_extra::Patch;

let mut patch = Patch::<i32>::Empty;
assert_eq!(*patch.get_or_insert(42), 42);

let mut patch = Patch::Some(1);
assert_eq!(*patch.get_or_insert(42), 1);
Source

pub fn get_or_insert_with<F>(&mut self, f: F) -> &mut T
where F: FnOnce() -> T,

Inserts a value computed from a closure if Empty or None.

§Examples
use value_extra::Patch;

let mut patch = Patch::<i32>::Empty;
assert_eq!(*patch.get_or_insert_with(|| 42), 42);
Source

pub fn take(&mut self) -> Patch<T>

Takes the value out, leaving Empty in its place.

§Examples
use value_extra::Patch;

let mut patch = Patch::Some(42);
let taken = patch.take();
assert_eq!(taken, Patch::Some(42));
assert_eq!(patch, Patch::Empty);
Source

pub fn replace(&mut self, value: T) -> Patch<T>

Replaces the value, returning the old one.

§Examples
use value_extra::Patch;

let mut patch = Patch::Some(42);
let old = patch.replace(100);
assert_eq!(old, Patch::Some(42));
assert_eq!(patch, Patch::Some(100));
Source§

impl<T: Default> Patch<T>

Source

pub fn get_or_insert_default(&mut self) -> &mut T

Inserts the default value if Empty or None, returning a mutable reference.

§Examples
use value_extra::Patch;

let mut patch = Patch::<i32>::Empty;
assert_eq!(*patch.get_or_insert_default(), 0);
Source§

impl<T: Clone> Patch<&T>

Source

pub fn cloned(self) -> Patch<T>

Maps a Patch<&T> to a Patch<T> by cloning.

§Examples
use value_extra::Patch;

let value = String::from("hello");
let patch = Patch::Some(&value);
assert_eq!(patch.cloned(), Patch::Some(String::from("hello")));
Source§

impl<T: Copy> Patch<&T>

Source

pub fn copied(self) -> Patch<T>

Maps a Patch<&T> to a Patch<T> by copying.

§Examples
use value_extra::Patch;

let value = 42;
let patch = Patch::Some(&value);
assert_eq!(patch.copied(), Patch::Some(42));
Source§

impl<T> Patch<T>

Source

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

Converts the patch to an Option, treating Empty and None as Option::None.

§Examples
use value_extra::Patch;

assert_eq!(Patch::Some(42).into_option(), Some(42));
assert_eq!(Patch::<i32>::Empty.into_option(), None);
assert_eq!(Patch::<i32>::None.into_option(), None);

Trait Implementations§

Source§

impl<T: Clone> Clone for Patch<T>

Source§

fn clone(&self) -> Patch<T>

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: Debug> Debug for Patch<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Patch<T>

Source§

fn default() -> Patch<T>

Returns Patch::Empty.

Source§

impl<'de, T> Deserialize<'de> for Patch<T>
where T: Deserialize<'de>,

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> From<Option<Option<T>>> for Patch<T>

Source§

fn from(value: Option<Option<T>>) -> Patch<T>

Converts Option<Option<T>> to Patch<T>.

This is the canonical representation for serde:

  • NonePatch::Empty (field absent)
  • Some(None)Patch::None (explicitly null)
  • Some(Some(v))Patch::Some(v)
Source§

impl<T> From<Option<T>> for Patch<T>

Source§

fn from(opt: Option<T>) -> Patch<T>

Converts Option<T> to Patch<T>.

  • Some(v)Patch::Some(v)
  • NonePatch::None
Source§

impl<T> From<Patch<T>> for Option<Option<T>>

Source§

fn from(patch: Patch<T>) -> Option<Option<T>>

Converts Patch<T> to Option<Option<T>>.

  • Patch::EmptyNone
  • Patch::NoneSome(None)
  • Patch::Some(v)Some(Some(v))
Source§

impl<T> From<Patch<T>> for Option<T>

Source§

fn from(patch: Patch<T>) -> Option<T>

Converts Patch<T> to Option<T>.

Both Empty and None become Option::None.

Source§

impl<T> From<T> for Patch<T>

Source§

fn from(value: T) -> Patch<T>

Creates a Patch::Some from a value.

Source§

impl<T: Hash> Hash for Patch<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord> Ord for Patch<T>

Source§

fn cmp(&self, other: &Patch<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for Patch<T>

Source§

fn eq(&self, other: &Patch<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for Patch<T>

Source§

fn partial_cmp(&self, other: &Patch<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Serialize for Patch<T>
where T: Serialize,

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Copy> Copy for Patch<T>

Source§

impl<T: Eq> Eq for Patch<T>

Source§

impl<T> StructuralPartialEq for Patch<T>

Auto Trait Implementations§

§

impl<T> Freeze for Patch<T>
where T: Freeze,

§

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

§

impl<T> Send for Patch<T>
where T: Send,

§

impl<T> Sync for Patch<T>
where T: Sync,

§

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

§

impl<T> UnwindSafe for Patch<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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,