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 presentPatch::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>
impl<T> Patch<T>
Sourcepub fn is_some(&self) -> bool
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());Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn is_none(&self) -> bool
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§impl<T> Patch<T>
impl<T> Patch<T>
Source§impl<T> Patch<T>
impl<T> Patch<T>
Sourcepub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Patch<U>
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);Sourcepub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U
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);Sourcepub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
pub fn map_or_else<U, D, F>(self, default: D, f: F) -> 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);Sourcepub fn and_then<U, F>(self, f: F) -> Patch<U>
pub fn and_then<U, F>(self, f: F) -> 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);Sourcepub fn filter<P>(self, predicate: P) -> Patch<T>
pub fn filter<P>(self, predicate: P) -> Patch<T>
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>
impl<T> Patch<T>
Sourcepub fn unwrap_or(self, default: T) -> T
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);Sourcepub fn unwrap_or_else<F>(self, f: F) -> Twhere
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(self, f: F) -> Twhere
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>
impl<T: Default> Patch<T>
Sourcepub fn unwrap_or_default(self) -> T
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>
impl<T> Patch<T>
Sourcepub fn or(self, other: Patch<T>) -> Patch<T>
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));Sourcepub fn or_else<F>(self, f: F) -> Patch<T>
pub fn or_else<F>(self, f: F) -> 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));Sourcepub fn xor(self, other: Patch<T>) -> Patch<T>
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);Sourcepub fn zip<U>(self, other: Patch<U>) -> Patch<(T, U)>
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§impl<T> Patch<T>
impl<T> Patch<T>
Sourcepub fn insert(&mut self, value: T) -> &mut T
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);Sourcepub fn get_or_insert(&mut self, value: T) -> &mut T
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);Sourcepub fn get_or_insert_with<F>(&mut self, f: F) -> &mut Twhere
F: FnOnce() -> T,
pub fn get_or_insert_with<F>(&mut self, f: F) -> &mut Twhere
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§impl<T: Default> Patch<T>
impl<T: Default> Patch<T>
Sourcepub fn get_or_insert_default(&mut self) -> &mut T
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> Patch<T>
impl<T> Patch<T>
Sourcepub fn into_option(self) -> Option<T>
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<'de, T> Deserialize<'de> for Patch<T>where
T: Deserialize<'de>,
Available on crate feature serde only.
impl<'de, T> Deserialize<'de> for Patch<T>where
T: Deserialize<'de>,
serde only.