Skip to content

Commit ac8ad88

Browse files
committed
dist: discard unnecessary abstraction layer
1 parent c6c258f commit ac8ad88

File tree

3 files changed

+32
-132
lines changed

3 files changed

+32
-132
lines changed

src/dist/component/package.rs

Lines changed: 28 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! prefix, represented by a `Components` instance.
44
55
use std::collections::{HashMap, HashSet};
6-
use std::fmt;
76
use std::io::{self, ErrorKind as IOErrorKind, Read};
87
use std::mem;
8+
use std::ops::Deref;
99
use std::path::{Path, PathBuf};
1010
use std::sync::OnceLock;
1111

@@ -17,6 +17,7 @@ use crate::diskio::{CompletedIo, Executor, FileBuffer, IO_CHUNK_SIZE, Item, Kind
1717
use crate::dist::component::components::{ComponentPart, ComponentPartKind, Components};
1818
use crate::dist::component::transaction::Transaction;
1919
use crate::dist::download::DownloadCfg;
20+
use crate::dist::manifest::CompressionKind;
2021
use crate::dist::temp;
2122
use crate::errors::RustupError;
2223
use crate::utils;
@@ -26,18 +27,6 @@ use crate::utils::units::Size;
2627
pub(crate) const INSTALLER_VERSION: &str = "3";
2728
pub(crate) const VERSION_FILE: &str = "rust-installer-version";
2829

29-
pub trait Package: fmt::Debug {
30-
fn contains(&self, component: &str, short_name: Option<&str>) -> bool;
31-
fn install<'a>(
32-
&self,
33-
target: &Components,
34-
component: &str,
35-
short_name: Option<&str>,
36-
tx: Transaction<'a>,
37-
) -> Result<Transaction<'a>>;
38-
fn components(&self) -> Vec<String>;
39-
}
40-
4130
#[derive(Debug)]
4231
pub struct DirectoryPackage {
4332
path: PathBuf,
@@ -69,16 +58,17 @@ fn validate_installer_version(path: &Path) -> Result<()> {
6958
}
7059
}
7160

72-
impl Package for DirectoryPackage {
73-
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
61+
impl DirectoryPackage {
62+
pub fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
7463
self.components.contains(component)
7564
|| if let Some(n) = short_name {
7665
self.components.contains(n)
7766
} else {
7867
false
7968
}
8069
}
81-
fn install<'a>(
70+
71+
pub fn install<'a>(
8272
&self,
8373
target: &Components,
8474
name: &str,
@@ -129,7 +119,7 @@ impl Package for DirectoryPackage {
129119
Ok(tx)
130120
}
131121

132-
fn components(&self) -> Vec<String> {
122+
pub(crate) fn components(&self) -> Vec<String> {
133123
self.components.iter().cloned().collect()
134124
}
135125
}
@@ -139,7 +129,19 @@ impl Package for DirectoryPackage {
139129
pub(crate) struct TarPackage(DirectoryPackage, temp::Dir);
140130

141131
impl TarPackage {
142-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
132+
pub(crate) fn compressed<R: Read>(
133+
stream: R,
134+
kind: CompressionKind,
135+
dl_cfg: &DownloadCfg<'_>,
136+
) -> Result<Self> {
137+
match kind {
138+
CompressionKind::GZip => Self::new(flate2::read::GzDecoder::new(stream), dl_cfg),
139+
CompressionKind::ZStd => Self::new(zstd::stream::read::Decoder::new(stream)?, dl_cfg),
140+
CompressionKind::XZ => Self::new(xz2::read::XzDecoder::new(stream), dl_cfg),
141+
}
142+
}
143+
144+
fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
143145
let temp_dir = dl_cfg.tmp_cx.new_directory()?;
144146
let mut archive = tar::Archive::new(stream);
145147
// The rust-installer packages unpack to a directory called
@@ -155,6 +157,14 @@ impl TarPackage {
155157
}
156158
}
157159

160+
impl Deref for TarPackage {
161+
type Target = DirectoryPackage;
162+
163+
fn deref(&self) -> &Self::Target {
164+
&self.0
165+
}
166+
}
167+
158168
// Probably this should live in diskio but ¯\_(ツ)_/¯
159169
fn unpack_ram(
160170
io_chunk_size: usize,
@@ -525,105 +535,3 @@ fn unpack_without_first_dir<R: Read>(
525535

526536
Ok(())
527537
}
528-
529-
impl Package for TarPackage {
530-
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
531-
self.0.contains(component, short_name)
532-
}
533-
fn install<'b>(
534-
&self,
535-
target: &Components,
536-
component: &str,
537-
short_name: Option<&str>,
538-
tx: Transaction<'b>,
539-
) -> Result<Transaction<'b>> {
540-
self.0.install(target, component, short_name, tx)
541-
}
542-
fn components(&self) -> Vec<String> {
543-
self.0.components()
544-
}
545-
}
546-
547-
#[derive(Debug)]
548-
pub(crate) struct TarGzPackage(TarPackage);
549-
550-
impl TarGzPackage {
551-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
552-
let stream = flate2::read::GzDecoder::new(stream);
553-
Ok(TarGzPackage(TarPackage::new(stream, dl_cfg)?))
554-
}
555-
}
556-
557-
impl Package for TarGzPackage {
558-
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
559-
self.0.contains(component, short_name)
560-
}
561-
fn install<'b>(
562-
&self,
563-
target: &Components,
564-
component: &str,
565-
short_name: Option<&str>,
566-
tx: Transaction<'b>,
567-
) -> Result<Transaction<'b>> {
568-
self.0.install(target, component, short_name, tx)
569-
}
570-
fn components(&self) -> Vec<String> {
571-
self.0.components()
572-
}
573-
}
574-
575-
#[derive(Debug)]
576-
pub(crate) struct TarXzPackage(TarPackage);
577-
578-
impl TarXzPackage {
579-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
580-
let stream = xz2::read::XzDecoder::new(stream);
581-
Ok(TarXzPackage(TarPackage::new(stream, dl_cfg)?))
582-
}
583-
}
584-
585-
impl Package for TarXzPackage {
586-
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
587-
self.0.contains(component, short_name)
588-
}
589-
fn install<'b>(
590-
&self,
591-
target: &Components,
592-
component: &str,
593-
short_name: Option<&str>,
594-
tx: Transaction<'b>,
595-
) -> Result<Transaction<'b>> {
596-
self.0.install(target, component, short_name, tx)
597-
}
598-
fn components(&self) -> Vec<String> {
599-
self.0.components()
600-
}
601-
}
602-
603-
#[derive(Debug)]
604-
pub(crate) struct TarZStdPackage(TarPackage);
605-
606-
impl TarZStdPackage {
607-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
608-
let stream = zstd::stream::read::Decoder::new(stream)?;
609-
Ok(TarZStdPackage(TarPackage::new(stream, dl_cfg)?))
610-
}
611-
}
612-
613-
impl Package for TarZStdPackage {
614-
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
615-
self.0.contains(component, short_name)
616-
}
617-
fn install<'b>(
618-
&self,
619-
target: &Components,
620-
component: &str,
621-
short_name: Option<&str>,
622-
tx: Transaction<'b>,
623-
) -> Result<Transaction<'b>> {
624-
self.0.install(target, component, short_name, tx)
625-
}
626-
fn components(&self) -> Vec<String> {
627-
self.0.components()
628-
}
629-
}

src/dist/manifestation.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ use tokio::sync::Semaphore;
1313
use tracing::{info, warn};
1414
use url::Url;
1515

16-
use crate::dist::component::{
17-
Components, Package, TarGzPackage, TarXzPackage, TarZStdPackage, Transaction,
18-
};
16+
use crate::dist::component::{Components, TarPackage, Transaction};
1917
use crate::dist::config::Config;
2018
use crate::dist::download::{DownloadCfg, DownloadStatus, File};
2119
use crate::dist::manifest::{Component, CompressionKind, HashedBinary, Manifest, TargetedPackage};
@@ -432,7 +430,7 @@ impl Manifestation {
432430

433431
// Install all the components in the installer
434432
let reader = utils::FileReaderWithProgress::new_file(&installer_file)?;
435-
let package: &dyn Package = &TarGzPackage::new(reader, dl_cfg)?;
433+
let package = TarPackage::compressed(reader, CompressionKind::GZip, dl_cfg)?;
436434
for component in package.components() {
437435
tx = package.install(&self.installation, &component, None, tx)?;
438436
}
@@ -747,11 +745,7 @@ impl<'a> ComponentBinary<'a> {
747745
self.status.installing();
748746

749747
let reader = utils::FileReaderWithProgress::new_file(&installer_file)?;
750-
let package = match self.binary.compression {
751-
CompressionKind::GZip => &TarGzPackage::new(reader, download_cfg)? as &dyn Package,
752-
CompressionKind::XZ => &TarXzPackage::new(reader, download_cfg)?,
753-
CompressionKind::ZStd => &TarZStdPackage::new(reader, download_cfg)?,
754-
};
748+
let package = TarPackage::compressed(reader, self.binary.compression, download_cfg)?;
755749

756750
// If the package doesn't contain the component that the
757751
// manifest says it does then somebody must be playing a joke on us.

tests/suite/dist_install.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::fs::File;
22
use std::io::Write;
33

4-
use rustup::dist::component::Components;
5-
use rustup::dist::component::Transaction;
6-
use rustup::dist::component::{DirectoryPackage, Package};
4+
use rustup::dist::component::{Components, DirectoryPackage, Transaction};
75
use rustup::dist::prefix::InstallPrefix;
86
use rustup::test::{DistContext, MockComponentBuilder, MockFile, MockInstallerBuilder};
97
use rustup::utils;

0 commit comments

Comments
 (0)