Struct SetVersion

Source
pub struct SetVersion { /* private fields */ }
Expand description

A SetVer version specification.

§Implementation details

This struct is implemented using HashSet from the standard library. Therefore, it is not usable in no-std environments right now.

Implementations§

Source§

impl SetVersion

Source

pub fn setver_compare(&self, other: &SetVersion) -> f32

Implements the SetVer comparison function, as per the specification.

The only returned values are 0, 1, Infinity and NaN.

Source

pub fn is_subset(&self, other: &SetVersion) -> bool

Returns whether this SetVer version is a subset of the other version, according to standard set laws.

use setver::SetVersion;
let first_version: SetVersion = "{}".parse().unwrap();
let second_version: SetVersion = "{{}}".parse().unwrap();
assert!(first_version.is_subset(&second_version));
Source

pub fn is_strict_subset(&self, other: &SetVersion) -> bool

Returns whether this SetVer version is a strict subset of the other version, according to standard set laws.

Source

pub fn is_superset(&self, other: &SetVersion) -> bool

Returns whether this SetVer version is a superset of the other version, according to standard set laws.

Source

pub fn is_strict_superset(&self, other: &SetVersion) -> bool

Returns whether this SetVer version is a strict superset of the other version, according to standard set laws.

Source

pub fn add_child_version(&mut self, child: Rc<SetVersion>) -> &mut Self

Adds the given version as a child version. This is useful when constructing a parent version for one or many previous child versions.

Examples found in repository?
examples/number_to_setver.rs (line 20)
5fn main() {
6	let mut args = args();
7	args.next();
8
9	if let Some(number_str) = args.next() {
10		let number = number_str.trim().parse::<usize>().expect("version is not an integer");
11
12		// This is exactly how natural numbers (including zero) work in set theory.
13		// Some dynamic programming makes this ~ 1/2 O(n^2).
14		let mut previous_number_setvers = Vec::with_capacity(number);
15		previous_number_setvers.push(Rc::new(SetVersion::default()));
16
17		for n in 1..=number {
18			let mut n_version = SetVersion::default();
19			for i in 0..n {
20				n_version.add_child_version(previous_number_setvers[i].clone());
21			}
22			previous_number_setvers.push(Rc::new(n_version));
23		}
24
25		let number_version = previous_number_setvers.last().unwrap();
26		println!("{}", number_version);
27	} else {
28		eprintln!("usage: number_to_setver NUMBER");
29	}
30}
Source

pub fn to_integralternative(&self) -> u128

Implements the Integralternative. This is the same as converting a SetVersion into an u128.

§Panics

For obvious reasons (if you know how the integralternative works), SetVersions with more than 128 braces in their text representation cannot be stored in a u128. In this case, this function will panic. Use to_integralternative_bytes instead.

Examples found in repository?
examples/alternative_representations.rs (line 27)
4fn main() {
5	let mut args = args();
6	args.next();
7	if let Some(mut version) = args.next() {
8		if &version == "-" {
9			version = String::new();
10			std::io::stdin().read_line(&mut version).expect("couldn't read setver from stdin");
11			version = version.trim().to_owned();
12		}
13
14		let canonicalized = version.parse::<SetVersion>().expect("invalid setver version");
15		let canonicalized_str = canonicalized.to_string();
16		let original_width = version.len().max("direct".len());
17		let canonical_width = canonicalized_str.len().max("canonicalized".len());
18		println!(
19			"                    {:>original_width$} {:>canonical_width$}
20set representation  {:>original_width$} {:>canonical_width$}
21integralternative   {:>original_width$} {:>canonical_width$}",
22			"direct",
23			"canonicalized",
24			version,
25			canonicalized_str,
26			SetVersion::string_to_integralternative(&version),
27			canonicalized.to_integralternative(),
28			original_width = original_width,
29			canonical_width = canonical_width
30		);
31	} else {
32		eprintln!("usage: alternative_representations SETVER_VERSION");
33	}
34}
Source

pub fn string_to_integralternative(setver: &str) -> u128

Implements the Integralternative and operates on a string. This is convenient if the canonicalized form of the given setver spec is different from the string.

§Panics

For obvious reasons (if you know how the integralternative works), SetVersions with more than 128 braces in their text representation cannot be stored in a u128. In this case, this function will panic. Use to_integralternative_bytes instead.

Examples found in repository?
examples/alternative_representations.rs (line 26)
4fn main() {
5	let mut args = args();
6	args.next();
7	if let Some(mut version) = args.next() {
8		if &version == "-" {
9			version = String::new();
10			std::io::stdin().read_line(&mut version).expect("couldn't read setver from stdin");
11			version = version.trim().to_owned();
12		}
13
14		let canonicalized = version.parse::<SetVersion>().expect("invalid setver version");
15		let canonicalized_str = canonicalized.to_string();
16		let original_width = version.len().max("direct".len());
17		let canonical_width = canonicalized_str.len().max("canonicalized".len());
18		println!(
19			"                    {:>original_width$} {:>canonical_width$}
20set representation  {:>original_width$} {:>canonical_width$}
21integralternative   {:>original_width$} {:>canonical_width$}",
22			"direct",
23			"canonicalized",
24			version,
25			canonicalized_str,
26			SetVersion::string_to_integralternative(&version),
27			canonicalized.to_integralternative(),
28			original_width = original_width,
29			canonical_width = canonical_width
30		);
31	} else {
32		eprintln!("usage: alternative_representations SETVER_VERSION");
33	}
34}
Source

pub fn to_integralternative_bytes(&self) -> Vec<u8>

Implements the Integralternative. The return value is not as practical as the one of to_integralternative, but it can always represent the integralternative and will never panic.

The returned bytes are in LSB-first order (little-endian).

Source

pub fn string_to_integralternative_bytes(setver: &str) -> Vec<u8>

Implements the Integralternative but operates directly on a string. This is convenient if the canonicalized form of the given setver spec is different from the string. The return value is not as practical as the one of to_integralternative, but it can always represent the integralternative and will never panic.

The returned bytes are in LSB-first order (little-endian).

Trait Implementations§

Source§

impl Clone for SetVersion

Source§

fn clone(&self) -> SetVersion

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 Debug for SetVersion

Source§

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

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

impl Default for SetVersion

Source§

fn default() -> SetVersion

Returns the “default value” for a type. Read more
Source§

impl Display for SetVersion

Source§

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

The stringified version is always in canonical form, meaning that small sets are printed first.

Source§

impl From<&SetVersion> for String

Source§

fn from(this: &SetVersion) -> Self

Converts to this type from the input type.
Source§

impl From<&SetVersion> for u128

Source§

fn from(this: &SetVersion) -> Self

Converts to this type from the input type.
Source§

impl FromStr for SetVersion

Source§

type Err = SetVerParseError

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

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

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

impl Ord for SetVersion

Source§

fn cmp(&self, other: &SetVersion) -> 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<&str> for SetVersion

Source§

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

Checks whether the string parses to the same setver. If the string is not a setver version, they are not equal.

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<u128> for SetVersion

Source§

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

Checks whether the integer is the canonical integralternative of this setver.

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 SetVersion

Source§

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

Source§

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

Source§

impl StructuralPartialEq for SetVersion

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, 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> ToString for T
where T: Display + ?Sized,

Source§

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.