@@ -11,6 +11,7 @@ use dialoguer::Select;
1111use failure:: { Fail , Error } ;
1212use flate2:: read:: GzDecoder ;
1313use log:: debug;
14+ use once_cell:: sync:: OnceCell ;
1415use pbr:: { ProgressBar , Units } ;
1516use regex:: Regex ;
1617use reqwest:: blocking:: { Client , Response } ;
@@ -88,25 +89,30 @@ impl Toolchain {
8889 /// This returns the date of the default toolchain, if it is a nightly toolchain.
8990 /// Returns `None` if the installed toolchain is not a nightly toolchain.
9091 pub ( crate ) fn default_nightly ( ) -> Option < GitDate > {
91- let version_meta = rustc_version:: version_meta ( ) . unwrap ( ) ;
92-
93- if let Channel :: Nightly = version_meta. channel {
94- if let Some ( str_date) = version_meta. commit_date {
95- let regex = Regex :: new ( r"(?m)^(\d{4})-(\d{2})-(\d{2})$" ) . unwrap ( ) ;
96- if let Some ( cap) = regex. captures ( & str_date) {
97- let year = cap. get ( 1 ) ?. as_str ( ) . parse :: < i32 > ( ) . ok ( ) ?;
98- let month = cap. get ( 2 ) ?. as_str ( ) . parse :: < u32 > ( ) . ok ( ) ?;
99- let day = cap. get ( 3 ) ?. as_str ( ) . parse :: < u32 > ( ) . ok ( ) ?;
100-
101- return Some ( Date :: from_utc (
102- naive:: NaiveDate :: from_ymd ( year, month, day) ,
103- Utc ,
104- ) ) ;
92+ fn inner ( ) -> Option < GitDate > {
93+ let version_meta = rustc_version:: version_meta ( ) . unwrap ( ) ;
94+
95+ if let Channel :: Nightly = version_meta. channel {
96+ if let Some ( str_date) = version_meta. commit_date {
97+ let regex = Regex :: new ( r"(?m)^(\d{4})-(\d{2})-(\d{2})$" ) . unwrap ( ) ;
98+ if let Some ( cap) = regex. captures ( & str_date) {
99+ let year = cap. get ( 1 ) ?. as_str ( ) . parse :: < i32 > ( ) . ok ( ) ?;
100+ let month = cap. get ( 2 ) ?. as_str ( ) . parse :: < u32 > ( ) . ok ( ) ?;
101+ let day = cap. get ( 3 ) ?. as_str ( ) . parse :: < u32 > ( ) . ok ( ) ?;
102+
103+ return Some ( Date :: from_utc (
104+ naive:: NaiveDate :: from_ymd ( year, month, day) ,
105+ Utc ,
106+ ) ) ;
107+ }
105108 }
106109 }
110+
111+ None
107112 }
108113
109- None
114+ static DATE : OnceCell < Option < GitDate > > = OnceCell :: new ( ) ;
115+ * ( DATE . get_or_init ( || inner ( ) ) )
110116 }
111117
112118 pub ( crate ) fn is_current_nightly ( & self ) -> bool {
0 commit comments