Skip to content

Commit

Permalink
Compile on stable Rust
Browse files Browse the repository at this point in the history
This removes the usage of #[fundamental] and intrinsics::type_name in the
notifications module. The `Notifyable` trait and `Notifier` struct were also
removed in favor of just using `&Fn(Notification)` where necessary. This change
is less ergonomic when crossing crate boundaries where `Notifier` naturally
implemented `Notifyable` for multiple notification types before. Instead, now
closures must be written as:

    &|n| prev_handler(n.into())

Which is to say that when crossing crate boundaries you need to remap
notification closures and leverage a `From` implementation.

This crate does not currently compile on the stable *channel* due to the usage
of `panic::catch_unwind` in the curl crate for now, but that will get fixed with
the next stable release, this is just paving the way forward!

Closes rust-lang#58
  • Loading branch information
alexcrichton committed May 16, 2016
1 parent c6e430a commit 2f7f43d
Show file tree
Hide file tree
Showing 26 changed files with 513 additions and 511 deletions.
3 changes: 2 additions & 1 deletion src/rustup-cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::io::{Write, BufRead};
use std::process::Command;
use std::{cmp, iter};
use std::str::FromStr;
use std::sync::Arc;
use std;
use term2;

Expand Down Expand Up @@ -102,7 +103,7 @@ pub fn set_globals(verbose: bool) -> Result<Cfg> {

let download_tracker = RefCell::new(DownloadTracker::new());

Ok(try!(Cfg::from_env(shared_ntfy!(move |n: Notification| {
Ok(try!(Cfg::from_env(Arc::new(move |n: Notification| {
if download_tracker.borrow_mut().handle_notification(&n) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/rustup-cli/multirust_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use rustup_dist::dist::TargetTriple;
use self_update;
use std::env;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use job;

pub fn main() -> Result<()> {
try!(::self_update::cleanup_self_updater());

let need_metadata = try!(command_requires_metadata());
if need_metadata {
let cfg = try!(Cfg::from_env(shared_ntfy!(move |_: Notification| { })));
let cfg = try!(Cfg::from_env(Arc::new(move |_: Notification| { })));
try!(cfg.check_metadata_version());
}

Expand Down
18 changes: 8 additions & 10 deletions src/rustup-cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
//! and racy on Windows.
use common::{self, Confirm};
use rustup::{NotifyHandler};
use errors::*;
use rustup_dist::dist;
use rustup_utils::utils;
Expand Down Expand Up @@ -456,7 +455,7 @@ fn install_bins() -> Result<()> {
let ref this_exe_path = try!(utils::current_exe());
let ref multirust_path = bin_path.join(&format!("multirust{}", EXE_SUFFIX));

try!(utils::ensure_dir_exists("bin", bin_path, ntfy!(&NotifyHandler::none())));
try!(utils::ensure_dir_exists("bin", bin_path, &|_| {}));
// NB: Even on Linux we can't just copy the new binary over the (running)
// old binary; we must unlink it first.
if multirust_path.exists() {
Expand Down Expand Up @@ -519,7 +518,7 @@ pub fn uninstall(no_prompt: bool) -> Result<()> {
// Delete RUSTUP_HOME
let ref multirust_dir = try!(utils::multirust_home());
if multirust_dir.exists() {
try!(utils::remove_dir("multirust_home", multirust_dir, ntfy!(&NotifyHandler::none())));
try!(utils::remove_dir("multirust_home", multirust_dir, &|_| {}));
}

let read_dir_err = "failure reading directory";
Expand All @@ -537,7 +536,7 @@ pub fn uninstall(no_prompt: bool) -> Result<()> {
let dirent = try!(dirent.chain_err(|| read_dir_err));
if dirent.file_name().to_str() != Some("bin") {
if dirent.path().is_dir() {
try!(utils::remove_dir("cargo_home", &dirent.path(), ntfy!(&NotifyHandler::none())));
try!(utils::remove_dir("cargo_home", &dirent.path(), &|_| {}));
} else {
try!(utils::remove_file("cargo_home", &dirent.path()));
}
Expand All @@ -554,7 +553,7 @@ pub fn uninstall(no_prompt: bool) -> Result<()> {
let file_is_tool = name.to_str().map(|n| tools.iter().any(|t| *t == n));
if file_is_tool == Some(false) {
if dirent.path().is_dir() {
try!(utils::remove_dir("cargo_home", &dirent.path(), ntfy!(&NotifyHandler::none())));
try!(utils::remove_dir("cargo_home", &dirent.path(), &|_| {}));
} else {
try!(utils::remove_file("cargo_home", &dirent.path()));
}
Expand All @@ -576,7 +575,7 @@ pub fn uninstall(no_prompt: bool) -> Result<()> {
#[cfg(unix)]
fn delete_multirust_and_cargo_home() -> Result<()> {
let ref cargo_home = try!(utils::cargo_home());
try!(utils::remove_dir("cargo_home", cargo_home, ntfy!(&NotifyHandler::none())));
try!(utils::remove_dir("cargo_home", cargo_home, &|_| ()));

Ok(())
}
Expand Down Expand Up @@ -684,15 +683,14 @@ fn delete_multirust_and_cargo_home() -> Result<()> {
/// Run by multirust-gc-$num.exe to delete CARGO_HOME
#[cfg(windows)]
pub fn complete_windows_uninstall() -> Result<()> {
use rustup::NotifyHandler;
use std::ffi::OsStr;
use std::process::Stdio;

try!(wait_for_parent());

// Now that the parent has exited there are hopefully no more files open in CARGO_HOME
let ref cargo_home = try!(utils::cargo_home());
try!(utils::remove_dir("cargo_home", cargo_home, ntfy!(&NotifyHandler::none())));
try!(utils::remove_dir("cargo_home", cargo_home, &|_| ()));

// Now, run a *system* binary to inherit the DELETE_ON_CLOSE
// handle to *this* process, then exit. The OS will delete the gc
Expand Down Expand Up @@ -1099,7 +1097,7 @@ pub fn prepare_update() -> Result<Option<PathBuf>> {
info!("checking for self-updates");
let hash_url = try!(utils::parse_url(&(url.clone() + ".sha256")));
let hash_file = tempdir.path().join("hash");
try!(utils::download_file(&hash_url, &hash_file, None, ntfy!(&NotifyHandler::none())));
try!(utils::download_file(&hash_url, &hash_file, None, &|_| ()));
let mut latest_hash = try!(utils::read_file("hash", &hash_file));
latest_hash.truncate(64);

Expand All @@ -1118,7 +1116,7 @@ pub fn prepare_update() -> Result<Option<PathBuf>> {
try!(utils::download_file(&download_url,
&setup_path,
Some(&mut hasher),
ntfy!(&NotifyHandler::none())));
&|_| ()));
let download_hash = hasher.result_str();

// Check that hash is correct
Expand Down
1 change: 1 addition & 0 deletions src/rustup-dist/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ fn main() {
let target = env::var("TARGET").unwrap();

File::create(out_dir.join("target.txt")).unwrap().write_all(target.as_bytes()).unwrap();
println!("cargo:rerun-if-changed=build.rs");
}
1 change: 0 additions & 1 deletion src/rustup-dist/src/component/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ impl Components {
}
}

#[derive(Debug)]
pub struct ComponentBuilder<'a> {
components: Components,
name: String,
Expand Down
34 changes: 19 additions & 15 deletions src/rustup-dist/src/component/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! FIXME: This uses ensure_dir_exists in some places but rollback
//! does not remove any dirs created by it.
use rustup_utils::{self, utils};
use rustup_utils::utils;
use temp;
use prefix::InstallPrefix;
use errors::*;
Expand All @@ -31,19 +31,18 @@ use std::path::{Path, PathBuf};
///
/// All operations that create files will fail if the destination
/// already exists.
#[derive(Debug)]
pub struct Transaction<'a> {
prefix: InstallPrefix,
changes: Vec<ChangedItem<'a>>,
temp_cfg: &'a temp::Cfg,
notify_handler: NotifyHandler<'a>,
notify_handler: &'a Fn(Notification),
committed: bool,
}

impl<'a> Transaction<'a> {
pub fn new(prefix: InstallPrefix,
temp_cfg: &'a temp::Cfg,
notify_handler: NotifyHandler<'a>)
notify_handler: &'a Fn(Notification))
-> Self {
Transaction {
prefix: prefix,
Expand Down Expand Up @@ -131,7 +130,7 @@ impl<'a> Transaction<'a> {
pub fn temp(&self) -> &'a temp::Cfg {
self.temp_cfg
}
pub fn notify_handler(&self) -> NotifyHandler<'a> {
pub fn notify_handler(&self) -> &'a Fn(Notification) {
self.notify_handler
}
}
Expand All @@ -141,11 +140,16 @@ impl<'a> Transaction<'a> {
impl<'a> Drop for Transaction<'a> {
fn drop(&mut self) {
if !self.committed {
self.notify_handler.call(Notification::RollingBack);
(self.notify_handler)(Notification::RollingBack);
for item in self.changes.iter().rev() {
ok_ntfy!(self.notify_handler,
Notification::NonFatalError,
item.roll_back(&self.prefix));
// ok_ntfy!(self.notify_handler,
// Notification::NonFatalError,
match item.roll_back(&self.prefix) {
Ok(()) => {}
Err(e) => {
(self.notify_handler)(Notification::NonFatalError(&e));
}
}
}
}
}
Expand All @@ -172,7 +176,7 @@ impl<'a> ChangedItem<'a> {
AddedDir(ref path) => {
try!(utils::remove_dir("component",
&prefix.abs_path(path),
rustup_utils::NotifyHandler::none()))
&|_| ()))
}
RemovedFile(ref path, ref tmp) | ModifiedFile(ref path, Some(ref tmp)) => {
try!(utils::rename_file("component", &tmp, &prefix.abs_path(path)))
Expand All @@ -198,7 +202,7 @@ impl<'a> ChangedItem<'a> {
}.into())
} else {
if let Some(p) = abs_path.parent() {
try!(utils::ensure_dir_exists("component", p, rustup_utils::NotifyHandler::none()));
try!(utils::ensure_dir_exists("component", p, &|_| ()));
}
let file = try!(File::create(&abs_path)
.chain_err(|| format!("error creating file '{}'", abs_path.display())));
Expand All @@ -218,7 +222,7 @@ impl<'a> ChangedItem<'a> {
}.into())
} else {
if let Some(p) = abs_path.parent() {
try!(utils::ensure_dir_exists("component", p, rustup_utils::NotifyHandler::none()));
try!(utils::ensure_dir_exists("component", p, &|_| ()));
}
try!(utils::copy_file(src, &abs_path));
Ok(ChangedItem::AddedFile(relpath))
Expand All @@ -233,9 +237,9 @@ impl<'a> ChangedItem<'a> {
}.into())
} else {
if let Some(p) = abs_path.parent() {
try!(utils::ensure_dir_exists("component", p, rustup_utils::NotifyHandler::none()));
try!(utils::ensure_dir_exists("component", p, &|_| ()));
}
try!(utils::copy_dir(src, &abs_path, rustup_utils::NotifyHandler::none()));
try!(utils::copy_dir(src, &abs_path, &|_| ()));
Ok(ChangedItem::AddedDir(relpath))
}
}
Expand Down Expand Up @@ -274,7 +278,7 @@ impl<'a> ChangedItem<'a> {
Ok(ChangedItem::ModifiedFile(relpath, Some(backup)))
} else {
if let Some(p) = abs_path.parent() {
try!(utils::ensure_dir_exists("component", p, rustup_utils::NotifyHandler::none()));
try!(utils::ensure_dir_exists("component", p, &|_| {}));
}
Ok(ChangedItem::ModifiedFile(relpath, None))
}
Expand Down
20 changes: 11 additions & 9 deletions src/rustup-dist/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,19 @@ pub fn download_and_check<'a>(url_str: &str,
return Ok(None);
}
} else {
cfg.notify_handler.call(Notification::CantReadUpdateHash(hash_file));
(cfg.notify_handler)(Notification::CantReadUpdateHash(hash_file));
}
} else {
cfg.notify_handler.call(Notification::NoUpdateHash(hash_file));
(cfg.notify_handler)(Notification::NoUpdateHash(hash_file));
}
}

let url = try!(utils::parse_url(url_str));
let file = try!(cfg.temp_cfg.new_file_with_ext("", ext));

let mut hasher = Sha256::new();
try!(utils::download_file(&url, &file, Some(&mut hasher), ntfy!(&cfg.notify_handler)));
try!(utils::download_file(&url, &file, Some(&mut hasher),
&|n| (cfg.notify_handler)(n.into())));
let actual_hash = hasher.result_str();

if hash != actual_hash {
Expand All @@ -352,26 +353,27 @@ pub fn download_and_check<'a>(url_str: &str,
calculated: actual_hash,
}.into());
} else {
cfg.notify_handler.call(Notification::ChecksumValid(url_str));
(cfg.notify_handler)(Notification::ChecksumValid(url_str));
}

// TODO: Check the signature of the file

Ok(Some((file, partial_hash)))
}

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone)]
pub struct DownloadCfg<'a> {
pub dist_root: &'a str,
pub temp_cfg: &'a temp::Cfg,
pub notify_handler: NotifyHandler<'a>,
pub notify_handler: &'a Fn(Notification),
}

pub fn download_hash(url: &str, cfg: DownloadCfg) -> Result<String> {
let hash_url = try!(utils::parse_url(&(url.to_owned() + ".sha256")));
let hash_file = try!(cfg.temp_cfg.new_file());

try!(utils::download_file(&hash_url, &hash_file, None, ntfy!(&cfg.notify_handler)));
try!(utils::download_file(&hash_url, &hash_file, None,
&|n| (cfg.notify_handler)(n.into())));

Ok(try!(utils::read_file("hash", &hash_file).map(|s| s[0..64].to_owned())))
}
Expand All @@ -398,7 +400,7 @@ pub fn update_from_dist<'a>(download: DownloadCfg<'a>,
};

// TODO: Add a notification about which manifest version is going to be used
download.notify_handler.call(Notification::DownloadingManifest(&toolchain_str));
(download.notify_handler)(Notification::DownloadingManifest(&toolchain_str));
match dl_v2_manifest(download, update_hash, toolchain) {
Ok(Some((m, hash))) => {
return match try!(manifestation.update(&m, changes, &download.temp_cfg,
Expand All @@ -410,7 +412,7 @@ pub fn update_from_dist<'a>(download: DownloadCfg<'a>,
Ok(None) => return Ok(None),
Err(Error(ErrorKind::Utils(::rustup_utils::ErrorKind::Download404 { .. }), _)) => {
// Proceed to try v1 as a fallback
download.notify_handler.call(Notification::DownloadingLegacyManifest);
(download.notify_handler)(Notification::DownloadingLegacyManifest);
}
Err(e) => return Err(e)
}
Expand Down
15 changes: 8 additions & 7 deletions src/rustup-dist/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use sha2::{Sha256, Digest};
use std::path::Path;
use std::process::Command;

#[derive(Debug)]
pub struct DownloadCfg<'a> {
pub temp_cfg: &'a temp::Cfg,
pub notify_handler: NotifyHandler<'a>,
pub notify_handler: &'a Fn(Notification),
pub gpg_key: Option<&'a str>,
}

Expand All @@ -22,7 +21,8 @@ impl<'a> DownloadCfg<'a> {

let sig_url = try!(utils::parse_url(&format!("{}.asc", url)));
let sig_file = try!(self.temp_cfg.new_file());
try!(utils::download_file(&sig_url, &sig_file, None, ntfy!(&self.notify_handler)));
try!(utils::download_file(&sig_url, &sig_file, None,
&|n| (self.notify_handler)(n.into())));

let target_url = try!(utils::parse_url(url));
let target_file = try!(self.temp_cfg.new_file());
Expand All @@ -32,7 +32,7 @@ impl<'a> DownloadCfg<'a> {
try!(utils::download_file(&target_url,
&target_file,
None,
ntfy!(&self.notify_handler)));
&|n| (self.notify_handler)(n.into())));

let key_file = try!(self.temp_cfg.new_file());
let key_filename: &Path = &key_file;
Expand Down Expand Up @@ -62,7 +62,8 @@ impl<'a> DownloadCfg<'a> {

let hash_url = try!(utils::parse_url(&format!("{}.sha256", url)));
let hash_file = try!(self.temp_cfg.new_file());
try!(utils::download_file(&hash_url, &hash_file, None, ntfy!(&self.notify_handler)));
try!(utils::download_file(&hash_url, &hash_file, None,
&|n| (self.notify_handler)(n.into())));

let hash = try!(utils::read_file("hash", &hash_file).map(|s| s[0..64].to_owned()));
let mut hasher = Sha256::new();
Expand All @@ -72,7 +73,7 @@ impl<'a> DownloadCfg<'a> {
try!(utils::download_file(&target_url,
&target_file,
Some(&mut hasher),
ntfy!(&self.notify_handler)));
&|n| (self.notify_handler)(n.into())));

let actual_hash = hasher.result_str();

Expand All @@ -84,7 +85,7 @@ impl<'a> DownloadCfg<'a> {
calculated: actual_hash,
}.into());
} else {
self.notify_handler.call(Notification::ChecksumValid(url));
(self.notify_handler)(Notification::ChecksumValid(url));
}

Ok(target_file)
Expand Down
2 changes: 1 addition & 1 deletion src/rustup-dist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate error_chain;
extern crate sha2;

pub use errors::*;
pub use notifications::{Notification, NotifyHandler};
pub use notifications::{Notification};

pub mod temp;

Expand Down
Loading

0 comments on commit 2f7f43d

Please sign in to comment.