mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 18:07:58 +00:00
Support stable rust for compiling the runtime (#13580)
* Support stable rust for compiling the runtime This pull request brings support for compiling the runtime with stable Rust. This requires at least rust 1.68.0 to work on stable. The code is written in a way that it is backwards compatible and should automatically work when someone compiles with 1.68.0+ stable. * We always support nightlies! * 🤦 * Sort by version * Review feedback * Review feedback * Fix version parsing * Apply suggestions from code review Co-authored-by: Koute <koute@users.noreply.github.com> --------- Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// The version of rustc/cargo.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Version {
|
||||
pub major: u32,
|
||||
pub minor: u32,
|
||||
pub patch: u32,
|
||||
pub is_nightly: bool,
|
||||
pub year: u32,
|
||||
pub month: u32,
|
||||
pub day: u32,
|
||||
}
|
||||
|
||||
impl Version {
|
||||
/// Returns if `self` is a stable version.
|
||||
pub fn is_stable(&self) -> bool {
|
||||
!self.is_nightly
|
||||
}
|
||||
|
||||
/// Return if `self` is a nightly version.
|
||||
pub fn is_nightly(&self) -> bool {
|
||||
self.is_nightly
|
||||
}
|
||||
|
||||
/// Extract from the given `version` string.
|
||||
pub fn extract(version: &str) -> Option<Self> {
|
||||
let mut is_nightly = false;
|
||||
let version_parts = version
|
||||
.trim()
|
||||
.split(" ")
|
||||
.nth(1)?
|
||||
.split(".")
|
||||
.filter_map(|v| {
|
||||
if let Some(rest) = v.strip_suffix("-nightly") {
|
||||
is_nightly = true;
|
||||
rest.parse().ok()
|
||||
} else {
|
||||
v.parse().ok()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<u32>>();
|
||||
|
||||
if version_parts.len() != 3 {
|
||||
return None
|
||||
}
|
||||
|
||||
let date = version.split(" ").nth(3)?;
|
||||
|
||||
let date_parts = date
|
||||
.split("-")
|
||||
.filter_map(|v| v.trim().strip_suffix(")").unwrap_or(v).parse().ok())
|
||||
.collect::<Vec<u32>>();
|
||||
|
||||
if date_parts.len() != 3 {
|
||||
return None
|
||||
}
|
||||
|
||||
Some(Version {
|
||||
major: version_parts[0],
|
||||
minor: version_parts[1],
|
||||
patch: version_parts[2],
|
||||
is_nightly,
|
||||
year: date_parts[0],
|
||||
month: date_parts[1],
|
||||
day: date_parts[2],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Ordering is done in the following way:
|
||||
///
|
||||
/// 1. `stable` > `nightly`
|
||||
/// 2. Then compare major, minor and patch.
|
||||
/// 3. Last compare the date.
|
||||
impl Ord for Version {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
if self == other {
|
||||
return Ordering::Equal
|
||||
}
|
||||
|
||||
// Ensure that `stable > nightly`
|
||||
if self.is_stable() && other.is_nightly() {
|
||||
return Ordering::Greater
|
||||
} else if self.is_nightly() && other.is_stable() {
|
||||
return Ordering::Less
|
||||
}
|
||||
|
||||
let to_compare = [
|
||||
(self.major, other.major),
|
||||
(self.minor, other.minor),
|
||||
(self.patch, other.patch),
|
||||
(self.year, other.year),
|
||||
(self.month, other.month),
|
||||
(self.day, other.day),
|
||||
];
|
||||
|
||||
to_compare
|
||||
.iter()
|
||||
.find_map(|(l, r)| if l != r { l.partial_cmp(&r) } else { None })
|
||||
// We already checked this right at the beginning, so we should never return here
|
||||
// `Equal`.
|
||||
.unwrap_or(Ordering::Equal)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Version {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn version_compare_and_extract_works() {
|
||||
let version_1_66_0 = Version::extract("cargo 1.66.0 (d65d197ad 2022-11-15)").unwrap();
|
||||
let version_1_66_1 = Version::extract("cargo 1.66.1 (d65d197ad 2022-11-15)").unwrap();
|
||||
let version_1_66_0_nightly =
|
||||
Version::extract("cargo 1.66.0-nightly (d65d197ad 2022-10-15)").unwrap();
|
||||
let version_1_66_0_nightly_older_date =
|
||||
Version::extract("cargo 1.66.0-nightly (d65d197ad 2022-10-14)").unwrap();
|
||||
let version_1_65_0 = Version::extract("cargo 1.65.0 (d65d197ad 2022-10-15)").unwrap();
|
||||
let version_1_65_0_older_date =
|
||||
Version::extract("cargo 1.65.0 (d65d197ad 2022-10-14)").unwrap();
|
||||
|
||||
assert!(version_1_66_1 > version_1_66_0);
|
||||
assert!(version_1_66_1 > version_1_65_0);
|
||||
assert!(version_1_66_1 > version_1_66_0_nightly);
|
||||
assert!(version_1_66_1 > version_1_66_0_nightly_older_date);
|
||||
assert!(version_1_66_1 > version_1_65_0_older_date);
|
||||
|
||||
assert!(version_1_66_0 > version_1_65_0);
|
||||
assert!(version_1_66_0 > version_1_66_0_nightly);
|
||||
assert!(version_1_66_0 > version_1_66_0_nightly_older_date);
|
||||
assert!(version_1_66_0 > version_1_65_0_older_date);
|
||||
|
||||
assert!(version_1_65_0 > version_1_66_0_nightly);
|
||||
assert!(version_1_65_0 > version_1_66_0_nightly_older_date);
|
||||
assert!(version_1_65_0 > version_1_65_0_older_date);
|
||||
|
||||
let mut versions = vec![
|
||||
version_1_66_0,
|
||||
version_1_66_0_nightly,
|
||||
version_1_66_0_nightly_older_date,
|
||||
version_1_65_0_older_date,
|
||||
version_1_65_0,
|
||||
version_1_66_1,
|
||||
];
|
||||
versions.sort_by(|a, b| b.cmp(a));
|
||||
|
||||
let expected_versions_order = vec![
|
||||
version_1_66_1,
|
||||
version_1_66_0,
|
||||
version_1_65_0,
|
||||
version_1_65_0_older_date,
|
||||
version_1_66_0_nightly,
|
||||
version_1_66_0_nightly_older_date,
|
||||
];
|
||||
assert_eq!(expected_versions_order, versions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_with_newline() {
|
||||
let version_1_66_0 = Version::extract("cargo 1.66.0 (d65d197ad 2022-11-15)\n").unwrap();
|
||||
assert_eq!(
|
||||
Version {
|
||||
major: 1,
|
||||
minor: 66,
|
||||
patch: 0,
|
||||
is_nightly: false,
|
||||
year: 2022,
|
||||
month: 11,
|
||||
day: 15
|
||||
},
|
||||
version_1_66_0
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user