Skip to content

Commit efcda52

Browse files
committed
dist: move Notifier into DownloadCfg
1 parent 9182627 commit efcda52

File tree

13 files changed

+117
-118
lines changed

13 files changed

+117
-118
lines changed

src/cli/common.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt::Display;
44
use std::fs;
55
use std::io::{BufRead, Write};
66
use std::path::{Path, PathBuf};
7-
use std::sync::{Arc, LazyLock};
7+
use std::sync::LazyLock;
88
use std::{cmp, env};
99

1010
use anyhow::{Context, Result, anyhow};
@@ -13,7 +13,6 @@ use termcolor::Color;
1313
use tracing::{error, info, warn};
1414
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
1515

16-
use crate::dist::download::Notifier;
1716
use crate::{
1817
config::Cfg,
1918
dist::{TargetTriple, ToolchainDesc},
@@ -122,8 +121,7 @@ pub(crate) fn read_line(process: &Process) -> Result<String> {
122121

123122
#[tracing::instrument(level = "trace", skip(process))]
124123
pub(crate) fn set_globals(current_dir: PathBuf, quiet: bool, process: &Process) -> Result<Cfg<'_>> {
125-
let notifier = Notifier::new(quiet, process);
126-
Cfg::from_env(current_dir, Arc::new(move |n| notifier.handle(n)), process)
124+
Cfg::from_env(current_dir, quiet, process)
127125
}
128126

129127
pub(crate) fn show_channel_update(

src/cli/rustup_mode.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ use crate::{
3333
command, component_for_bin,
3434
config::{ActiveReason, Cfg},
3535
dist::{
36-
AutoInstallMode, PartialToolchainDesc, Profile, TargetTriple,
37-
manifest::{Component, ComponentStatus},
36+
AutoInstallMode, PartialToolchainDesc, Profile, TargetTriple, download::DownloadCfg, manifest::{Component, ComponentStatus}
3837
},
3938
errors::RustupError,
4039
install::{InstallMethod, UpdateStatus},
@@ -909,7 +908,7 @@ async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<utils::ExitCode
909908
&& self_update_mode == SelfUpdateMode::Enable
910909
&& !opts.no_self_update;
911910

912-
if self_update && check_rustup_update(cfg.process).await? {
911+
if self_update && check_rustup_update(&DownloadCfg::new(cfg)).await? {
913912
update_available = true;
914913
}
915914

@@ -937,10 +936,13 @@ async fn update(
937936
if let Some(p) = opts.profile {
938937
cfg.set_profile_override(p);
939938
}
939+
940940
let cfg = &cfg;
941941
if cfg.get_profile()? == Profile::Complete {
942942
warn!("{}", common::WARN_COMPLETE_PROFILE);
943943
}
944+
945+
let dl_cfg = DownloadCfg::new(cfg);
944946
let names = opts.toolchain;
945947
if !names.is_empty() {
946948
for name in names {
@@ -994,7 +996,7 @@ async fn update(
994996
}
995997
}
996998
if self_update {
997-
exit_code &= self_update::self_update(cfg.process).await?;
999+
exit_code &= self_update::self_update(&dl_cfg).await?;
9981000
}
9991001
} else if ensure_active_toolchain {
10001002
let (toolchain, reason) = cfg.ensure_active_toolchain(force_non_host, true).await?;
@@ -1003,7 +1005,7 @@ async fn update(
10031005
} else {
10041006
exit_code &= common::update_all_channels(cfg, opts.force).await?;
10051007
if self_update {
1006-
exit_code &= self_update::self_update(cfg.process).await?;
1008+
exit_code &= self_update::self_update(&dl_cfg).await?;
10071009
}
10081010

10091011
info!("cleaning up downloads & tmp directories");
@@ -1012,7 +1014,7 @@ async fn update(
10121014
}
10131015

10141016
if !cfg!(feature = "no-self-update") && self_update_mode == SelfUpdateMode::CheckOnly {
1015-
check_rustup_update(cfg.process).await?;
1017+
check_rustup_update(&dl_cfg).await?;
10161018
}
10171019

10181020
if cfg!(feature = "no-self-update") {

src/cli/self_update.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use serde::{Deserialize, Serialize};
5050
use termcolor::Color;
5151
use tracing::{error, info, trace, warn};
5252

53+
use crate::dist::download::DownloadCfg;
5354
use crate::{
5455
DUP_TOOLS, TOOLS,
5556
cli::{
@@ -1112,7 +1113,7 @@ pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermissi
11121113
}
11131114

11141115
/// Performs all of a self-update: check policy, download, apply and exit.
1115-
pub(crate) async fn self_update(process: &Process) -> Result<utils::ExitCode> {
1116+
pub(crate) async fn self_update(dl_cfg: &DownloadCfg<'_>) -> Result<utils::ExitCode> {
11161117
match self_update_permitted(false)? {
11171118
SelfUpdatePermission::HardFail => {
11181119
error!("Unable to self-update. STOP");
@@ -1123,13 +1124,13 @@ pub(crate) async fn self_update(process: &Process) -> Result<utils::ExitCode> {
11231124
SelfUpdatePermission::Permit => {}
11241125
}
11251126

1126-
let setup_path = prepare_update(process).await?;
1127+
let setup_path = prepare_update(dl_cfg).await?;
11271128

11281129
if let Some(setup_path) = &setup_path {
11291130
return run_update(setup_path);
11301131
} else {
11311132
// Try again in case we emitted "tool `{}` is already installed" last time.
1132-
install_proxies(process)?;
1133+
install_proxies(dl_cfg.process)?;
11331134
}
11341135

11351136
Ok(utils::ExitCode(0))
@@ -1174,7 +1175,7 @@ pub(crate) async fn update(cfg: &Cfg<'_>) -> Result<utils::ExitCode> {
11741175
Permit => {}
11751176
}
11761177

1177-
match prepare_update(cfg.process).await? {
1178+
match prepare_update(&DownloadCfg::new(cfg)).await? {
11781179
Some(setup_path) => {
11791180
let Some(version) = get_and_parse_new_rustup_version(&setup_path) else {
11801181
error!("failed to get rustup version");
@@ -1227,8 +1228,8 @@ fn parse_new_rustup_version(version: String) -> String {
12271228
String::from(matched_version)
12281229
}
12291230

1230-
pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>> {
1231-
let cargo_home = process.cargo_home()?;
1231+
pub(crate) async fn prepare_update(dl_cfg: &DownloadCfg<'_>) -> Result<Option<PathBuf>> {
1232+
let cargo_home = dl_cfg.process.cargo_home()?;
12321233
let rustup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup{EXE_SUFFIX}"));
12331234
let setup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup-init{EXE_SUFFIX}"));
12341235

@@ -1253,19 +1254,19 @@ pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>>
12531254
let triple = dist::TargetTriple::from_host(process).unwrap_or(triple);
12541255

12551256
// Get update root.
1256-
let update_root = update_root(process);
1257+
let update_root = update_root(dl_cfg.process);
12571258

12581259
// Get current version
12591260
let current_version = env!("CARGO_PKG_VERSION");
12601261

12611262
// Get available version
12621263
info!("checking for self-update (current version: {current_version})");
1263-
let available_version = match process.var_opt("RUSTUP_VERSION")? {
1264+
let available_version = match dl_cfg.process.var_opt("RUSTUP_VERSION")? {
12641265
Some(ver) => {
12651266
info!("`RUSTUP_VERSION` has been set to `{ver}`");
12661267
ver
12671268
}
1268-
None => get_available_rustup_version(process).await?,
1269+
None => get_available_rustup_version(dl_cfg).await?,
12691270
};
12701271

12711272
// If up-to-date
@@ -1281,16 +1282,23 @@ pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>>
12811282

12821283
// Download new version
12831284
info!("downloading self-update (new version: {available_version})");
1284-
download_file(&download_url, &setup_path, None, &|_| (), process).await?;
1285+
download_file(
1286+
&download_url,
1287+
&setup_path,
1288+
None,
1289+
&dl_cfg.notifier,
1290+
dl_cfg.process,
1291+
)
1292+
.await?;
12851293

12861294
// Mark as executable
12871295
utils::make_executable(&setup_path)?;
12881296

12891297
Ok(Some(setup_path))
12901298
}
12911299

1292-
async fn get_available_rustup_version(process: &Process) -> Result<String> {
1293-
let update_root = update_root(process);
1300+
async fn get_available_rustup_version(dl_cfg: &DownloadCfg<'_>) -> Result<String> {
1301+
let update_root = update_root(dl_cfg.process);
12941302
let tempdir = tempfile::Builder::new()
12951303
.prefix("rustup-update")
12961304
.tempdir()
@@ -1300,7 +1308,14 @@ async fn get_available_rustup_version(process: &Process) -> Result<String> {
13001308
let release_file_url = format!("{update_root}/release-stable.toml");
13011309
let release_file_url = utils::parse_url(&release_file_url)?;
13021310
let release_file = tempdir.path().join("release-stable.toml");
1303-
download_file(&release_file_url, &release_file, None, &|_| (), process).await?;
1311+
download_file(
1312+
&release_file_url,
1313+
&release_file,
1314+
None,
1315+
&dl_cfg.notifier,
1316+
dl_cfg.process,
1317+
)
1318+
.await?;
13041319
let release_toml_str = utils::read_file("rustup release", &release_file)?;
13051320
let release_toml = toml::from_str::<RustupManifest>(&release_toml_str)
13061321
.context("unable to parse rustup release file")?;
@@ -1348,13 +1363,13 @@ impl fmt::Display for SchemaVersion {
13481363
}
13491364

13501365
/// Returns whether an update was available
1351-
pub(crate) async fn check_rustup_update(process: &Process) -> anyhow::Result<bool> {
1352-
let mut t = process.stdout();
1366+
pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg<'_>) -> anyhow::Result<bool> {
1367+
let mut t = dl_cfg.process.stdout();
13531368
// Get current rustup version
13541369
let current_version = env!("CARGO_PKG_VERSION");
13551370

13561371
// Get available rustup version
1357-
let available_version = get_available_rustup_version(process).await?;
1372+
let available_version = get_available_rustup_version(dl_cfg).await?;
13581373

13591374
let _ = t.attr(Attr::Bold);
13601375
write!(t.lock(), "rustup - ")?;

src/config.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::fmt::{self, Debug, Display};
22
use std::io;
33
use std::path::{Path, PathBuf};
44
use std::str::FromStr;
5-
use std::sync::Arc;
65

76
use anyhow::{Context, Result, anyhow, bail};
87
use serde::Deserialize;
@@ -17,7 +16,6 @@ use crate::{
1716
errors::RustupError,
1817
fallback_settings::FallbackSettings,
1918
install::UpdateStatus,
20-
notifications::*,
2119
process::Process,
2220
settings::{MetadataVersion, Settings, SettingsFile},
2321
toolchain::{
@@ -230,15 +228,15 @@ pub(crate) struct Cfg<'a> {
230228
pub toolchain_override: Option<ResolvableToolchainName>,
231229
pub env_override: Option<LocalToolchainName>,
232230
pub dist_root_url: String,
233-
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
231+
pub quiet: bool,
234232
pub current_dir: PathBuf,
235233
pub process: &'a Process,
236234
}
237235

238236
impl<'a> Cfg<'a> {
239237
pub(crate) fn from_env(
240238
current_dir: PathBuf,
241-
notify_handler: Arc<dyn Fn(Notification<'_>)>,
239+
quiet: bool,
242240
process: &'a Process,
243241
) -> Result<Self> {
244242
// Set up the rustup home directory
@@ -299,10 +297,10 @@ impl<'a> Cfg<'a> {
299297
update_hash_dir,
300298
download_dir,
301299
tmp_cx,
302-
notify_handler,
303300
toolchain_override: None,
304301
env_override,
305302
dist_root_url: dist_root,
303+
quiet,
306304
current_dir,
307305
process,
308306
};
@@ -997,7 +995,7 @@ impl Debug for Cfg<'_> {
997995
toolchain_override,
998996
env_override,
999997
dist_root_url,
1000-
notify_handler: _,
998+
quiet,
1001999
current_dir,
10021000
process: _,
10031001
} = self;
@@ -1014,6 +1012,7 @@ impl Debug for Cfg<'_> {
10141012
.field("toolchain_override", toolchain_override)
10151013
.field("env_override", env_override)
10161014
.field("dist_root_url", dist_root_url)
1015+
.field("quiet", quiet)
10171016
.field("current_dir", current_dir)
10181017
.finish()
10191018
}

src/diskio/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub(crate) mod immediate;
5555
#[cfg(test)]
5656
mod test;
5757
pub(crate) mod threaded;
58+
use threaded::PoolReference;
5859

5960
use std::io::{self, Write};
6061
use std::ops::{Deref, DerefMut};
@@ -65,9 +66,8 @@ use std::{fmt::Debug, fs::OpenOptions};
6566

6667
use anyhow::Result;
6768

68-
use crate::notifications::Notification;
69+
use crate::dist::download::Notifier;
6970
use crate::process::Process;
70-
use threaded::PoolReference;
7171

7272
/// Carries the implementation specific data for complete file transfers into the executor.
7373
#[derive(Debug)]
@@ -443,13 +443,13 @@ pub(crate) fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
443443

444444
/// Get the executor for disk IO.
445445
pub(crate) fn get_executor<'a>(
446-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
446+
notifier: Option<&'a Notifier>,
447447
ram_budget: usize,
448448
process: &Process,
449449
) -> anyhow::Result<Box<dyn Executor + 'a>> {
450450
// If this gets lots of use, consider exposing via the config file.
451451
Ok(match process.io_thread_count()? {
452452
0 | 1 => Box::new(immediate::ImmediateUnpacker::new()),
453-
n => Box::new(threaded::Threaded::new(notify_handler, n, ram_budget)),
453+
n => Box::new(threaded::Threaded::new(notifier, n, ram_budget)),
454454
})
455455
}

src/diskio/threaded.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use sharded_slab::pool::{OwnedRef, OwnedRefMut};
1515
use tracing::debug;
1616

1717
use super::{CompletedIo, Executor, Item, perform};
18+
use crate::dist::download::Notifier;
1819
use crate::notifications::Notification;
1920

2021
#[derive(Copy, Clone, Debug, Enum)]
@@ -99,7 +100,7 @@ impl fmt::Debug for Pool {
99100
pub(crate) struct Threaded<'a> {
100101
n_files: Arc<AtomicUsize>,
101102
pool: threadpool::ThreadPool,
102-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
103+
notifier: Option<&'a Notifier>,
103104
rx: Receiver<Task>,
104105
tx: Sender<Task>,
105106
vec_pools: EnumMap<Bucket, Pool>,
@@ -109,7 +110,7 @@ pub(crate) struct Threaded<'a> {
109110
impl<'a> Threaded<'a> {
110111
/// Construct a new Threaded executor.
111112
pub(crate) fn new(
112-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
113+
notify_handler: Option<&'a Notifier>,
113114
thread_count: usize,
114115
ram_budget: usize,
115116
) -> Self {
@@ -168,7 +169,7 @@ impl<'a> Threaded<'a> {
168169
Self {
169170
n_files: Arc::new(AtomicUsize::new(0)),
170171
pool,
171-
notify_handler,
172+
notifier: notify_handler,
172173
rx,
173174
tx,
174175
vec_pools,
@@ -261,9 +262,9 @@ impl Executor for Threaded<'_> {
261262
// actual handling of data today, we synthesis a data buffer and
262263
// pretend to have bytes to deliver.
263264
let mut prev_files = self.n_files.load(Ordering::Relaxed);
264-
if let Some(handler) = self.notify_handler {
265-
handler(Notification::DownloadFinished(None));
266-
handler(Notification::DownloadContentLengthReceived(
265+
if let Some(notifier) = self.notifier {
266+
notifier.handle(Notification::DownloadFinished(None));
267+
notifier.handle(Notification::DownloadContentLengthReceived(
267268
prev_files as u64,
268269
None,
269270
));
@@ -282,16 +283,16 @@ impl Executor for Threaded<'_> {
282283
prev_files = current_files;
283284
current_files = self.n_files.load(Ordering::Relaxed);
284285
let step_count = prev_files - current_files;
285-
if let Some(handler) = self.notify_handler {
286-
handler(Notification::DownloadDataReceived(
286+
if let Some(notifier) = self.notifier {
287+
notifier.handle(Notification::DownloadDataReceived(
287288
&buf[0..step_count],
288289
None,
289290
));
290291
}
291292
}
292293
self.pool.join();
293-
if let Some(handler) = self.notify_handler {
294-
handler(Notification::DownloadFinished(None));
294+
if let Some(notifier) = self.notifier {
295+
notifier.handle(Notification::DownloadFinished(None));
295296
}
296297
// close the feedback channel so that blocking reads on it can
297298
// complete. send is atomic, and we know the threads completed from the

0 commit comments

Comments
 (0)