Skip to main content

Cmp

Enum Cmp 

Source
pub enum Cmp {
    Eq,
    Ne,
    Lt,
    Le,
    Ge,
    Gt,
}
Expand description

Comparison operators enum.

Variants§

§

Eq

Equal (==, =). When version A is equal to B.

§

Ne

Not equal (!=, !, <>). When version A is not equal to B.

§

Lt

Less than (<). When version A is less than B but not equal.

§

Le

Less or equal (<=). When version A is less than or equal to B.

§

Ge

Greater or equal (>=). When version A is greater than or equal to B.

§

Gt

Greater than (>). When version A is greater than B but not equal.

Implementations§

Source§

impl Cmp

Source

pub fn from_sign<S: AsRef<str>>(sign: S) -> Result<Cmp, ()>

Get a comparison operator by it’s sign. Whitespaces are stripped from the sign string. An error is returned if the sign isn’t recognized.

The following signs are supported:

  • == or = -> Eq
  • != or ! or <> -> Ne
  • < -> Lt
  • <= -> Le
  • >= -> Ge
  • > -> Gt
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::from_sign("=="), Ok(Cmp::Eq));
assert_eq!(Cmp::from_sign("<"), Ok(Cmp::Lt));
assert_eq!(Cmp::from_sign("  >=   "), Ok(Cmp::Ge));
assert!(Cmp::from_sign("*").is_err());
Source

pub fn from_name<S: AsRef<str>>(sign: S) -> Result<Cmp, ()>

Get a comparison operator by it’s name. Names are case-insensitive, and whitespaces are stripped from the string. An error is returned if the name isn’t recognized.

§Examples
use version_compare::Cmp;

assert_eq!(Cmp::from_name("eq"), Ok(Cmp::Eq));
assert_eq!(Cmp::from_name("lt"), Ok(Cmp::Lt));
assert_eq!(Cmp::from_name("  Ge   "), Ok(Cmp::Ge));
assert!(Cmp::from_name("abc").is_err());
Source

pub fn from_ord(ord: Ordering) -> Cmp

👎Deprecated since 0.2.0:

use Cmp::from(ord) instead

Get the comparison operator from Rusts Ordering enum.

The following comparison operators are returned:

  • Ordering::Less -> Lt
  • Ordering::Equal -> Eq
  • Ordering::Greater -> Gt
Source

pub fn name<'a>(self) -> &'a str

Get the name of this comparison operator.

§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.name(), "eq");
assert_eq!(Cmp::Lt.name(), "lt");
assert_eq!(Cmp::Ge.name(), "ge");
Source

pub fn invert(self) -> Self

Get the inverted comparison operator.

This uses the following bidirectional rules:

  • Eq <-> Ne
  • Lt <-> Ge
  • Le <-> Gt
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.invert(), Cmp::Ne);
assert_eq!(Cmp::Lt.invert(), Cmp::Ge);
assert_eq!(Cmp::Gt.invert(), Cmp::Le);
Source

pub fn opposite(self) -> Self

Get the opposite comparison operator.

This uses the following bidirectional rules:

  • Eq <-> Ne
  • Lt <-> Gt
  • Le <-> Ge
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.opposite(), Cmp::Ne);
assert_eq!(Cmp::Lt.opposite(), Cmp::Gt);
assert_eq!(Cmp::Ge.opposite(), Cmp::Le);
Source

pub fn flip(self) -> Self

Get the flipped comparison operator.

This uses the following bidirectional rules:

  • Lt <-> Gt
  • Le <-> Ge
  • Other operators are returned as is.
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.flip(), Cmp::Eq);
assert_eq!(Cmp::Lt.flip(), Cmp::Gt);
assert_eq!(Cmp::Ge.flip(), Cmp::Le);
Source

pub fn sign(self) -> &'static str

Get the sign for this comparison operator.

The following signs are returned:

  • Eq -> ==
  • Ne -> !=
  • Lt -> <
  • Le -> <=
  • Ge -> >=
  • Gt -> >

Note: Some comparison operators also support other signs, such as = for Eq and ! for Ne, these are never returned by this method however as the table above is used.

§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.sign(), "==");
assert_eq!(Cmp::Lt.sign(), "<");
assert_eq!(Cmp::Ge.flip().sign(), "<=");
Source

pub fn factor(self) -> i8

Get a factor (number) for this comparison operator. These factors can be useful for quick calculations.

The following factor numbers are returned:

  • Eq or Ne -> 0
  • Lt or Le -> -1
  • Gt or Ge -> 1
§Examples
use version_compare::Version;

let a = Version::from("1.2.3").unwrap();
let b = Version::from("1.3").unwrap();

assert_eq!(a.compare(&b).factor(), -1);
assert_eq!(10 * b.compare(a).factor(), 10);
Source

pub fn ord(self) -> Option<Ordering>

Get Rust’s ordering for this comparison operator.

The following comparison operators are supported:

  • Eq -> Ordering::Equal
  • Lt -> Ordering::Less
  • Gt -> Ordering::Greater

For other comparison operators None is returned.

§Examples
use std::cmp::Ordering;
use version_compare::Version;

let a = Version::from("1.2.3").unwrap();
let b = Version::from("1.3").unwrap();

assert_eq!(a.compare(b).ord().unwrap(), Ordering::Less);

Trait Implementations§

Source§

impl Clone for Cmp

Source§

fn clone(&self) -> Cmp

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Cmp

Source§

impl Debug for Cmp

Source§

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

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

impl Eq for Cmp

Source§

impl From<Ordering> for Cmp

Source§

fn from(ord: Ordering) -> Self

Get the comparison operator from Rusts Ordering enum.

The following comparison operators are returned:

  • Ordering::Less -> Lt
  • Ordering::Equal -> Eq
  • Ordering::Greater -> Gt
Source§

impl Hash for Cmp

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 PartialEq for Cmp

Source§

fn eq(&self, other: &Cmp) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 StructuralPartialEq for Cmp

Auto Trait Implementations§

§

impl Freeze for Cmp

§

impl RefUnwindSafe for Cmp

§

impl Send for Cmp

§

impl Sync for Cmp

§

impl Unpin for Cmp

§

impl UnsafeUnpin for Cmp

§

impl UnwindSafe for Cmp

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.