11//! Installation from a Rust distribution server
22
33use std:: {
4- collections:: HashSet , env, fmt, io:: Write , ops:: Deref , path:: Path , str:: FromStr , sync:: LazyLock ,
4+ collections:: HashSet , env, fmt, io:: Write , ops:: Deref , path:: PathBuf , str:: FromStr ,
5+ sync:: LazyLock ,
56} ;
67
78use anyhow:: { Context , Result , anyhow, bail} ;
@@ -871,26 +872,49 @@ impl fmt::Display for Profile {
871872}
872873
873874pub ( crate ) struct DistOptions < ' a > {
874- pub ( crate ) cfg : & ' a Cfg < ' a > ,
875- pub ( crate ) toolchain : & ' a ToolchainDesc ,
876- pub ( crate ) profile : Profile ,
877- pub ( crate ) update_hash : & ' a Path ,
878- pub ( crate ) dl_cfg : DownloadCfg < ' a > ,
875+ pub ( super ) cfg : & ' a Cfg < ' a > ,
876+ pub ( super ) toolchain : & ' a ToolchainDesc ,
877+ profile : Profile ,
878+ pub ( super ) update_hash : PathBuf ,
879+ dl_cfg : DownloadCfg < ' a > ,
879880 /// --force bool is whether to force an update/install
880- pub ( crate ) force : bool ,
881+ force : bool ,
881882 /// --allow-downgrade
882- pub ( crate ) allow_downgrade : bool ,
883+ pub ( super ) allow_downgrade : bool ,
883884 /// toolchain already exists
884- pub ( crate ) exists : bool ,
885+ pub ( super ) exists : bool ,
885886 /// currently installed date and version
886- pub ( crate ) old_date_version : Option < ( String , String ) > ,
887+ pub ( super ) old_date_version : Option < ( String , String ) > ,
887888 /// Extra components to install from dist
888- pub ( crate ) components : & ' a [ & ' a str ] ,
889+ components : & ' a [ & ' a str ] ,
889890 /// Extra targets to install from dist
890- pub ( crate ) targets : & ' a [ & ' a str ] ,
891+ targets : & ' a [ & ' a str ] ,
891892}
892893
893894impl < ' a > DistOptions < ' a > {
895+ pub ( super ) fn new (
896+ components : & ' a [ & ' a str ] ,
897+ targets : & ' a [ & ' a str ] ,
898+ toolchain : & ' a ToolchainDesc ,
899+ profile : Profile ,
900+ force : bool ,
901+ cfg : & ' a Cfg < ' _ > ,
902+ ) -> Result < Self > {
903+ Ok ( Self {
904+ cfg,
905+ toolchain,
906+ profile,
907+ update_hash : cfg. get_hash_file ( toolchain, true ) ?,
908+ dl_cfg : DownloadCfg :: new ( cfg) ,
909+ force,
910+ allow_downgrade : false ,
911+ exists : false ,
912+ old_date_version : None ,
913+ components,
914+ targets,
915+ } )
916+ }
917+
894918 // Installs or updates a toolchain from a dist server. If an initial
895919 // install then it will be installed with the default components. If
896920 // an upgrade then all the existing components will be upgraded.
@@ -900,12 +924,12 @@ impl<'a> DistOptions<'a> {
900924 pub ( crate ) async fn install_into ( & self , prefix : & InstallPrefix ) -> Result < Option < String > > {
901925 let fresh_install = !prefix. path ( ) . exists ( ) ;
902926 // fresh_install means the toolchain isn't present, but hash_exists means there is a stray hash file
903- if fresh_install && Path :: exists ( self . update_hash ) {
927+ if fresh_install && self . update_hash . exists ( ) {
904928 warn ! (
905929 "removing stray hash file in order to continue: {}" ,
906930 self . update_hash. display( )
907931 ) ;
908- std:: fs:: remove_file ( self . update_hash ) ?;
932+ std:: fs:: remove_file ( & self . update_hash ) ?;
909933 }
910934
911935 let mut fetched = String :: new ( ) ;
@@ -1067,7 +1091,7 @@ impl<'a> DistOptions<'a> {
10671091 // So if components or targets is not empty, we skip passing `update_hash` so that
10681092 // we essentially degenerate to `rustup component add` / `rustup target add`
10691093 if self . components . is_empty ( ) && self . targets . is_empty ( ) {
1070- Some ( self . update_hash )
1094+ Some ( & self . update_hash )
10711095 } else {
10721096 None
10731097 } ,
0 commit comments