Enum later_operator::ComparisonOperator

source ·
pub enum ComparisonOperator {
    Ne,
    Lt,
    Le,
    Eq,
    Ge,
    Gt,
}
Expand description

§Comparison Operators.

This enum holds a comparison operator — !=, <, <=, ==, >=, > — that can be used to programmatically compare two values.

Variants can be parsed from string slices using FromStr or TryFrom<&str>, or byte slices via TryFrom<&[u8]>, and converted to a string slice or byte slice using ComparisonOperator::as_str or ComparisonOperator::as_bytes respectively.

If the serde crate feature is enabled, it can also be de/serialized from/to its string representations.

Use ComparisonOperator::compare to thusly compare any two values (so long as the type implements PartialOrd).

§Examples

use later_operator::ComparisonOperator;

// Parse from a string, then compare two arbitrary values.
let op = ComparisonOperator::try_from("<=").unwrap();
assert!(op.compare(&3_u8, &u8::MAX)); // 3 <= 255

// Re-stringify the operator for printing or whatever.
assert_eq!(
    format!("3 {op} 255"),
    "3 <= 255",
);

// Leading/trailing whitespace is ignored when parsing.
assert_eq!(
    ComparisonOperator::try_from("==").unwrap(),
    ComparisonOperator::try_from(" ==\n").unwrap(),
);

// But the value has to make sense or it will fail.
assert!(ComparisonOperator::try_from("~").is_err());

When the optional serde crate feature is enabled, ComparisonOperator can be de/serialized as a string too:

use later_operator::ComparisonOperator;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
struct AffectedVersion {
    operator: ComparisonOperator,
    version: String,
}

Variants§

§

Ne

§Not Equal To.

§

Lt

§Less Than.

§

Le

§Less Than or Equal To.

§

Eq

§Equal To.

§

Ge

§Greater Than or Equal To.

§

Gt

§Greater Than.

Implementations§

source§

impl ComparisonOperator

source

pub const fn as_bytes(self) -> &'static [u8]

§As Bytes.

Return the operator as a byte slice.

§Examples
use later_operator::ComparisonOperator;

assert_eq!(ComparisonOperator::Eq.as_bytes(), b"==");
source

pub const fn as_str(self) -> &'static str

§As Str.

Return the operator as a string slice.

§Examples
use later_operator::ComparisonOperator;

assert_eq!(ComparisonOperator::Eq.as_str(), "==");
source

pub const fn is_empty(self) -> bool

§Is Empty.

This method is added only for consistency; it always returns false.

source

pub const fn len(self) -> usize

§Length.

Return the byte length of the operator’s string representation.

source§

impl ComparisonOperator

source

pub const fn is_ne(self) -> bool

§Is Not Equal?

Returns true for ComparisonOperator::Ne.

source

pub const fn is_lt(self) -> bool

§Is Less Than?

Returns true for ComparisonOperator::Lt.

source

pub const fn is_le(self) -> bool

§Is Less Than/Equal To?

Returns true for ComparisonOperator::Le.

source

pub const fn is_eq(self) -> bool

§Is Equal?

Returns true for ComparisonOperator::Eq.

source

pub const fn is_ge(self) -> bool

§Is Greater Than/Equal To?

Returns true for ComparisonOperator::Ge.

source

pub const fn is_gt(self) -> bool

§Is Greater Than?

Returns true for ComparisonOperator::Gt.

source§

impl ComparisonOperator

source

pub fn compare<T: PartialOrd>(self, lhs: &T, rhs: &T) -> bool

§Compare.

Compare lhs to rhs using a given operator.

§Examples
use later_operator::ComparisonOperator;

let a: u32 = 50;
let b: u32 = 60;

assert_eq!(ComparisonOperator::Ne.compare(&a, &b), true);  // a != b
assert_eq!(ComparisonOperator::Lt.compare(&a, &b), true);  // a < b
assert_eq!(ComparisonOperator::Le.compare(&a, &b), true);  // a <= b
assert_eq!(ComparisonOperator::Eq.compare(&a, &b), false); // a == b
assert_eq!(ComparisonOperator::Ge.compare(&a, &b), false); // a >= b
assert_eq!(ComparisonOperator::Gt.compare(&a, &b), false); // a > b

Trait Implementations§

source§

impl AsRef<[u8]> for ComparisonOperator

source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<str> for ComparisonOperator

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<str> for ComparisonOperator

source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
source§

impl Clone for ComparisonOperator

source§

fn clone(&self) -> ComparisonOperator

Returns a copy 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 Debug for ComparisonOperator

source§

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

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

impl<'de> Deserialize<'de> for ComparisonOperator

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 Display for ComparisonOperator

source§

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

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

impl FromStr for ComparisonOperator

source§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(src: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for ComparisonOperator

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 Ord for ComparisonOperator

source§

fn cmp(&self, other: &ComparisonOperator) -> 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 + PartialOrd,

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

impl PartialEq<ComparisonOperator> for str

source§

fn eq(&self, other: &ComparisonOperator) -> 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 PartialEq<str> for ComparisonOperator

source§

fn eq(&self, other: &str) -> 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 PartialEq for ComparisonOperator

source§

fn eq(&self, other: &ComparisonOperator) -> 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 PartialOrd for ComparisonOperator

source§

fn partial_cmp(&self, other: &ComparisonOperator) -> 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 Serialize for ComparisonOperator

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 TryFrom<&[u8]> for ComparisonOperator

source§

type Error = Error

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

fn try_from(src: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&str> for ComparisonOperator

source§

type Error = Error

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

fn try_from(src: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for ComparisonOperator

source§

impl Eq for ComparisonOperator

source§

impl StructuralPartialEq for ComparisonOperator

Auto Trait Implementations§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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>,