Skip to content

Commit da0113e

Browse files
refactor(downloads): remove some lifetime parameters to allow multi-thread installations
Co-authored-by: rami3l <[email protected]>
1 parent e37e800 commit da0113e

File tree

14 files changed

+614
-372
lines changed

14 files changed

+614
-372
lines changed

src/config.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,19 @@ pub(crate) struct Cfg<'a> {
229229
pub toolchains_dir: PathBuf,
230230
pub update_hash_dir: PathBuf,
231231
pub download_dir: PathBuf,
232-
pub tmp_cx: temp::Context,
232+
pub tmp_cx: Arc<temp::Context>,
233233
pub toolchain_override: Option<ResolvableToolchainName>,
234234
pub env_override: Option<LocalToolchainName>,
235235
pub dist_root_url: String,
236-
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
236+
pub notify_handler: Arc<NotifyHandler>,
237237
pub current_dir: PathBuf,
238238
pub process: &'a Process,
239239
}
240240

241241
impl<'a> Cfg<'a> {
242242
pub(crate) fn from_env(
243243
current_dir: PathBuf,
244-
notify_handler: Arc<dyn Fn(Notification<'_>)>,
244+
notify_handler: Arc<NotifyHandler>,
245245
process: &'a Process,
246246
) -> Result<Self> {
247247
// Set up the rustup home directory
@@ -292,11 +292,11 @@ impl<'a> Cfg<'a> {
292292
let dist_root_server = dist_root_server(process)?;
293293

294294
let notify_clone = notify_handler.clone();
295-
let tmp_cx = temp::Context::new(
295+
let tmp_cx = Arc::new(temp::Context::new(
296296
rustup_dir.join("tmp"),
297297
dist_root_server.as_str(),
298-
Box::new(move |n| (notify_clone)(n.into())),
299-
);
298+
Arc::new(move |n| (notify_clone)(n.into())),
299+
));
300300
let dist_root = dist_root_server + "/dist";
301301

302302
let cfg = Self {
@@ -329,15 +329,15 @@ impl<'a> Cfg<'a> {
329329

330330
/// construct a download configuration
331331
pub(crate) fn download_cfg(
332-
&'a self,
333-
notify_handler: &'a dyn Fn(crate::dist::Notification<'_>),
334-
) -> DownloadCfg<'a> {
332+
&self,
333+
notify_handler: Arc<dist::notifications::NotifyHandler>,
334+
) -> DownloadCfg {
335335
DownloadCfg {
336-
dist_root: &self.dist_root_url,
337-
tmp_cx: &self.tmp_cx,
338-
download_dir: &self.download_dir,
336+
dist_root: Arc::from(self.dist_root_url.clone()),
337+
tmp_cx: Arc::clone(&self.tmp_cx),
338+
download_dir: Arc::new(self.download_dir.clone()),
339339
notify_handler,
340-
process: self.process,
340+
process: Arc::new(self.process.clone()),
341341
}
342342
}
343343

src/dist/component/components.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Components {
5555
Ok(None)
5656
}
5757
}
58-
fn write_version(&self, tx: &mut Transaction<'_>) -> Result<()> {
58+
fn write_version(&self, tx: &mut Transaction) -> Result<()> {
5959
tx.modify_file(self.prefix.rel_manifest_file(VERSION_FILE))?;
6060
utils::write_file(
6161
VERSION_FILE,
@@ -79,7 +79,7 @@ impl Components {
7979
})
8080
.collect())
8181
}
82-
pub(crate) fn add<'a>(&self, name: &str, tx: Transaction<'a>) -> ComponentBuilder<'a> {
82+
pub(crate) fn add(&self, name: &str, tx: Transaction) -> ComponentBuilder {
8383
ComponentBuilder {
8484
components: self.clone(),
8585
name: name.to_owned(),
@@ -96,14 +96,14 @@ impl Components {
9696
}
9797
}
9898

99-
pub(crate) struct ComponentBuilder<'a> {
99+
pub(crate) struct ComponentBuilder {
100100
components: Components,
101101
name: String,
102102
parts: Vec<ComponentPart>,
103-
tx: Transaction<'a>,
103+
tx: Transaction,
104104
}
105105

106-
impl<'a> ComponentBuilder<'a> {
106+
impl ComponentBuilder {
107107
pub(crate) fn copy_file(&mut self, path: PathBuf, src: &Path) -> Result<()> {
108108
self.parts.push(ComponentPart {
109109
kind: ComponentPartKind::File,
@@ -132,7 +132,7 @@ impl<'a> ComponentBuilder<'a> {
132132
});
133133
self.tx.move_dir(&self.name, path, src)
134134
}
135-
pub(crate) fn finish(mut self) -> Result<Transaction<'a>> {
135+
pub(crate) fn finish(mut self) -> Result<Transaction> {
136136
// Write component manifest
137137
let path = self.components.rel_component_manifest(&self.name);
138138
let abs_path = self.components.prefix.abs_path(&path);
@@ -255,18 +255,20 @@ impl Component {
255255
}
256256
Ok(result)
257257
}
258-
pub fn uninstall<'a>(
259-
&self,
260-
mut tx: Transaction<'a>,
261-
process: &Process,
262-
) -> Result<Transaction<'a>> {
258+
pub fn uninstall(&self, mut tx: Transaction, process: &Process) -> Result<Transaction> {
263259
// Update components file
264260
let path = self.components.rel_components_file();
265261
let abs_path = self.components.prefix.abs_path(&path);
266262
let temp = tx.temp().new_file()?;
267263
utils::filter_file("components", &abs_path, &temp, |l| l != self.name)?;
268264
tx.modify_file(path)?;
269-
utils::rename("components", &temp, &abs_path, tx.notify_handler(), process)?;
265+
utils::rename(
266+
"components",
267+
&temp,
268+
&abs_path,
269+
&*tx.notify_handler(),
270+
process,
271+
)?;
270272

271273
// TODO: If this is the last component remove the components file
272274
// and the version file.

src/dist/component/package.rs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt;
77
use std::io::{self, ErrorKind as IOErrorKind, Read};
88
use std::mem;
99
use std::path::{Path, PathBuf};
10+
use std::sync::Arc;
1011

1112
use anyhow::{Context, Result, anyhow, bail};
1213
use tar::EntryType;
@@ -26,13 +27,13 @@ pub(crate) const VERSION_FILE: &str = "rust-installer-version";
2627

2728
pub trait Package: fmt::Debug {
2829
fn contains(&self, component: &str, short_name: Option<&str>) -> bool;
29-
fn install<'a>(
30+
fn install(
3031
&self,
3132
target: &Components,
3233
component: &str,
3334
short_name: Option<&str>,
34-
tx: Transaction<'a>,
35-
) -> Result<Transaction<'a>>;
35+
tx: Transaction,
36+
) -> Result<Transaction>;
3637
fn components(&self) -> Vec<String>;
3738
}
3839

@@ -79,13 +80,13 @@ impl Package for DirectoryPackage {
7980
false
8081
}
8182
}
82-
fn install<'a>(
83+
fn install(
8384
&self,
8485
target: &Components,
8586
name: &str,
8687
short_name: Option<&str>,
87-
tx: Transaction<'a>,
88-
) -> Result<Transaction<'a>> {
88+
tx: Transaction,
89+
) -> Result<Transaction> {
8990
let actual_name = if self.components.contains(name) {
9091
name
9192
} else if let Some(n) = short_name {
@@ -137,13 +138,13 @@ impl Package for DirectoryPackage {
137138

138139
#[derive(Debug)]
139140
#[allow(dead_code)] // temp::Dir is held for drop.
140-
pub(crate) struct TarPackage<'a>(DirectoryPackage, temp::Dir<'a>);
141+
pub(crate) struct TarPackage(DirectoryPackage, temp::Dir);
141142

142-
impl<'a> TarPackage<'a> {
143+
impl TarPackage {
143144
pub(crate) fn new<R: Read>(
144145
stream: R,
145-
tmp_cx: &'a temp::Context,
146-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
146+
tmp_cx: Arc<temp::Context>,
147+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
147148
process: &Process,
148149
) -> Result<Self> {
149150
let temp_dir = tmp_cx.new_directory()?;
@@ -532,17 +533,17 @@ fn unpack_without_first_dir<R: Read>(
532533
Ok(())
533534
}
534535

535-
impl Package for TarPackage<'_> {
536+
impl Package for TarPackage {
536537
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
537538
self.0.contains(component, short_name)
538539
}
539-
fn install<'b>(
540+
fn install(
540541
&self,
541542
target: &Components,
542543
component: &str,
543544
short_name: Option<&str>,
544-
tx: Transaction<'b>,
545-
) -> Result<Transaction<'b>> {
545+
tx: Transaction,
546+
) -> Result<Transaction> {
546547
self.0.install(target, component, short_name, tx)
547548
}
548549
fn components(&self) -> Vec<String> {
@@ -551,13 +552,13 @@ impl Package for TarPackage<'_> {
551552
}
552553

553554
#[derive(Debug)]
554-
pub(crate) struct TarGzPackage<'a>(TarPackage<'a>);
555+
pub(crate) struct TarGzPackage(TarPackage);
555556

556-
impl<'a> TarGzPackage<'a> {
557+
impl TarGzPackage {
557558
pub(crate) fn new<R: Read>(
558559
stream: R,
559-
tmp_cx: &'a temp::Context,
560-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
560+
tmp_cx: Arc<temp::Context>,
561+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
561562
process: &Process,
562563
) -> Result<Self> {
563564
let stream = flate2::read::GzDecoder::new(stream);
@@ -570,17 +571,17 @@ impl<'a> TarGzPackage<'a> {
570571
}
571572
}
572573

573-
impl Package for TarGzPackage<'_> {
574+
impl Package for TarGzPackage {
574575
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
575576
self.0.contains(component, short_name)
576577
}
577-
fn install<'b>(
578+
fn install(
578579
&self,
579580
target: &Components,
580581
component: &str,
581582
short_name: Option<&str>,
582-
tx: Transaction<'b>,
583-
) -> Result<Transaction<'b>> {
583+
tx: Transaction,
584+
) -> Result<Transaction> {
584585
self.0.install(target, component, short_name, tx)
585586
}
586587
fn components(&self) -> Vec<String> {
@@ -589,13 +590,13 @@ impl Package for TarGzPackage<'_> {
589590
}
590591

591592
#[derive(Debug)]
592-
pub(crate) struct TarXzPackage<'a>(TarPackage<'a>);
593+
pub(crate) struct TarXzPackage(TarPackage);
593594

594-
impl<'a> TarXzPackage<'a> {
595+
impl TarXzPackage {
595596
pub(crate) fn new<R: Read>(
596597
stream: R,
597-
tmp_cx: &'a temp::Context,
598-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
598+
tmp_cx: Arc<temp::Context>,
599+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
599600
process: &Process,
600601
) -> Result<Self> {
601602
let stream = xz2::read::XzDecoder::new(stream);
@@ -608,17 +609,17 @@ impl<'a> TarXzPackage<'a> {
608609
}
609610
}
610611

611-
impl Package for TarXzPackage<'_> {
612+
impl Package for TarXzPackage {
612613
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
613614
self.0.contains(component, short_name)
614615
}
615-
fn install<'b>(
616+
fn install(
616617
&self,
617618
target: &Components,
618619
component: &str,
619620
short_name: Option<&str>,
620-
tx: Transaction<'b>,
621-
) -> Result<Transaction<'b>> {
621+
tx: Transaction,
622+
) -> Result<Transaction> {
622623
self.0.install(target, component, short_name, tx)
623624
}
624625
fn components(&self) -> Vec<String> {
@@ -627,13 +628,13 @@ impl Package for TarXzPackage<'_> {
627628
}
628629

629630
#[derive(Debug)]
630-
pub(crate) struct TarZStdPackage<'a>(TarPackage<'a>);
631+
pub(crate) struct TarZStdPackage(TarPackage);
631632

632-
impl<'a> TarZStdPackage<'a> {
633+
impl TarZStdPackage {
633634
pub(crate) fn new<R: Read>(
634635
stream: R,
635-
tmp_cx: &'a temp::Context,
636-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
636+
tmp_cx: Arc<temp::Context>,
637+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
637638
process: &Process,
638639
) -> Result<Self> {
639640
let stream = zstd::stream::read::Decoder::new(stream)?;
@@ -646,17 +647,17 @@ impl<'a> TarZStdPackage<'a> {
646647
}
647648
}
648649

649-
impl Package for TarZStdPackage<'_> {
650+
impl Package for TarZStdPackage {
650651
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
651652
self.0.contains(component, short_name)
652653
}
653-
fn install<'b>(
654+
fn install(
654655
&self,
655656
target: &Components,
656657
component: &str,
657658
short_name: Option<&str>,
658-
tx: Transaction<'b>,
659-
) -> Result<Transaction<'b>> {
659+
tx: Transaction,
660+
) -> Result<Transaction> {
660661
self.0.install(target, component, short_name, tx)
661662
}
662663
fn components(&self) -> Vec<String> {

0 commit comments

Comments
 (0)