1
1
use std:: fmt:: { self , Display } ;
2
+ use std:: sync:: OnceLock ;
2
3
3
4
use rustc_macros:: {
4
5
Decodable , Encodable , HashStable_Generic , PrintAttribute , current_rustc_version,
@@ -16,8 +17,29 @@ pub struct RustcVersion {
16
17
17
18
impl RustcVersion {
18
19
pub const CURRENT : Self = current_rustc_version ! ( ) ;
20
+ pub fn current_configurable ( ) -> Self {
21
+ * CURRENT_CONFIGURABLE . get_or_init ( || {
22
+ if let Ok ( override_var) = std:: env:: var ( "RUSTC_OVERRIDE_VERSION_STRING" )
23
+ && let Some ( override_) = Self :: parse_str ( & override_var)
24
+ {
25
+ override_
26
+ } else {
27
+ Self :: CURRENT
28
+ }
29
+ } )
30
+ }
31
+ fn parse_str ( value : & str ) -> Option < Self > {
32
+ // Ignore any suffixes such as "-dev" or "-nightly".
33
+ let mut components = value. split ( '-' ) . next ( ) . unwrap ( ) . splitn ( 3 , '.' ) ;
34
+ let major = components. next ( ) ?. parse ( ) . ok ( ) ?;
35
+ let minor = components. next ( ) ?. parse ( ) . ok ( ) ?;
36
+ let patch = components. next ( ) . unwrap_or ( "0" ) . parse ( ) . ok ( ) ?;
37
+ Some ( RustcVersion { major, minor, patch } )
38
+ }
19
39
}
20
40
41
+ static CURRENT_CONFIGURABLE : OnceLock < RustcVersion > = OnceLock :: new ( ) ;
42
+
21
43
impl Display for RustcVersion {
22
44
fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
23
45
write ! ( formatter, "{}.{}.{}" , self . major, self . minor, self . patch)
0 commit comments