rustc_semver

Enum RustcVersion

Source
pub enum RustcVersion {
    Normal(NormalVersion),
    Special(SpecialVersion),
}
Expand description

RustcVersion represents a version of the Rust Compiler.

This struct only supports the NormalVersion format

major.minor.patch

and 3 special formats represented by the SpecialVersion enum.

A version can be created with one of the functions RustcVersion::new or RustcVersion::parse. The RustcVersion::new method only supports the normal version format.

You can compare two versions, just as you would expect:

use rustc_semver::RustcVersion;

assert!(RustcVersion::new(1, 34, 0) > RustcVersion::parse("1.10").unwrap());
assert!(RustcVersion::new(1, 34, 0) > RustcVersion::parse("0.9").unwrap());

This comparison is semver conform according to the semver definition of precedence. However, if you want to check whether one version meets another version according to the Caret Requirements, you should use RustcVersion::meets.

Variants§

Implementations§

Source§

impl RustcVersion

Source

pub const fn new(major: u32, minor: u32, patch: u32) -> Self

RustcVersion::new is a const constructor for a RustcVersion.

This function is primarily used to construct constants, for everything else use RustcVersion::parse.

This function only allows to construct normal versions. For special versions, construct them directly with the SpecialVersion enum.

§Examples
use rustc_semver::RustcVersion;

const MY_FAVORITE_RUST: RustcVersion = RustcVersion::new(1, 48, 0);

assert!(MY_FAVORITE_RUST > RustcVersion::new(1, 0, 0))
Source

pub fn parse(version: &str) -> Result<Self>

RustcVersion::parse parses a RustcVersion.

This function can parse all normal and special versions. It is possbile to omit parts of the version, like the patch or minor version part. So 1, 1.0, and 1.0.0 are all valid inputs and will result in the same version.

§Errors

This function returns an Error, if the passed string is not a valid RustcVersion

§Examples
use rustc_semver::{SpecialVersion, RustcVersion};

let ver = RustcVersion::new(1, 0, 0);

assert_eq!(RustcVersion::parse("1").unwrap(), ver);
assert_eq!(RustcVersion::parse("1.0").unwrap(), ver);
assert_eq!(RustcVersion::parse("1.0.0").unwrap(), ver);
assert_eq!(
    RustcVersion::parse("1.0.0-alpha").unwrap(),
    RustcVersion::Special(SpecialVersion::Alpha)
);
Source

pub fn meets(self, other: Self) -> bool

RustcVersion::meets implements a semver conform version check according to the Caret Requirements.

Note that SpecialVersions only meet themself and no other version meets a SpecialVersion. This is because according to semver, special versions are considered unstable and “might not satisfy the intended compatibility requirements as denoted by [their] associated normal version”.

§Examples
use rustc_semver::RustcVersion;

assert!(RustcVersion::new(1, 30, 0).meets(RustcVersion::parse("1.29").unwrap()));
assert!(!RustcVersion::new(1, 30, 0).meets(RustcVersion::parse("1.31").unwrap()));

assert!(RustcVersion::new(0, 2, 1).meets(RustcVersion::parse("0.2").unwrap()));
assert!(!RustcVersion::new(0, 3, 0).meets(RustcVersion::parse("0.2").unwrap()));

Trait Implementations§

Source§

impl Clone for RustcVersion

Source§

fn clone(&self) -> RustcVersion

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 RustcVersion

Source§

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

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

impl Display for RustcVersion

Source§

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

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

impl Ord for RustcVersion

Source§

fn cmp(&self, other: &Self) -> 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 PartialEq for RustcVersion

Source§

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

Source§

fn partial_cmp(&self, other: &Self) -> 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 Copy for RustcVersion

Source§

impl Eq for RustcVersion

Source§

impl StructuralPartialEq for RustcVersion

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, 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.