From 7f3e86e069191ab552122fdcf6eaba84843a51e8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 12 Feb 2018 21:33:31 +0300 Subject: [PATCH] Switch to lazycell from crate.io --- Cargo.toml | 1 + src/cargo/core/package.rs | 3 +- src/cargo/lib.rs | 1 + src/cargo/ops/cargo_rustc/compilation.rs | 6 +- src/cargo/sources/registry/remote.rs | 5 +- src/cargo/util/config.rs | 13 +++-- src/cargo/util/lazy_cell.rs | 74 ------------------------ src/cargo/util/mod.rs | 2 - 8 files changed, 18 insertions(+), 87 deletions(-) delete mode 100644 src/cargo/util/lazy_cell.rs diff --git a/Cargo.toml b/Cargo.toml index 265a0ad15..2cf17e31c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ hex = "0.3" home = "0.3" ignore = "0.3" jobserver = "0.1.9" +lazycell = "0.6" libc = "0.2" libgit2-sys = "0.6" log = "0.4" diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 72ac61625..e0c54c03a 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -7,11 +7,12 @@ use std::path::{Path, PathBuf}; use semver::Version; use serde::ser; use toml; +use lazycell::LazyCell; use core::{Dependency, Manifest, PackageId, SourceId, Target}; use core::{Summary, SourceMap}; use ops; -use util::{Config, LazyCell, internal, lev_distance}; +use util::{Config, internal, lev_distance}; use util::errors::{CargoResult, CargoResultExt}; /// Information about a package that is available somewhere in the file system. diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 6e8d77154..d60792c7a 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -21,6 +21,7 @@ extern crate hex; extern crate home; extern crate ignore; extern crate jobserver; +extern crate lazycell; extern crate libc; extern crate libgit2_sys; extern crate num_cpus; diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 3fe62f365..88f82516b 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -1,10 +1,12 @@ use std::collections::{HashMap, HashSet, BTreeSet}; use std::ffi::OsStr; use std::path::PathBuf; + use semver::Version; +use lazycell::LazyCell; use core::{PackageId, Package, Target, TargetKind}; -use util::{self, CargoResult, Config, LazyCell, ProcessBuilder, process, join_paths}; +use util::{self, CargoResult, Config, ProcessBuilder, process, join_paths}; /// A structure returning the result of a compilation. pub struct Compilation<'cfg> { @@ -101,7 +103,7 @@ impl<'cfg> Compilation<'cfg> { } fn target_runner(&self) -> CargoResult<&Option<(PathBuf, Vec)>> { - self.target_runner.get_or_try_init(|| { + self.target_runner.try_borrow_with(|| { let key = format!("target.{}.runner", self.target); Ok(self.config.get_path_and_args(&key)?.map(|v| v.val)) }) diff --git a/src/cargo/sources/registry/remote.rs b/src/cargo/sources/registry/remote.rs index 5ffabb9d5..331183e6d 100644 --- a/src/cargo/sources/registry/remote.rs +++ b/src/cargo/sources/registry/remote.rs @@ -9,12 +9,13 @@ use std::str; use git2; use hex; use serde_json; +use lazycell::LazyCell; use core::{PackageId, SourceId}; use sources::git; use sources::registry::{RegistryData, RegistryConfig, INDEX_LOCK, CRATE_TEMPLATE, VERSION_TEMPLATE}; use util::network; -use util::{FileLock, Filesystem, LazyCell}; +use util::{FileLock, Filesystem}; use util::{Config, Sha256, ToUrl, Progress}; use util::errors::{CargoResult, CargoResultExt, HttpNot200}; @@ -43,7 +44,7 @@ impl<'cfg> RemoteRegistry<'cfg> { } fn repo(&self) -> CargoResult<&git2::Repository> { - self.repo.get_or_try_init(|| { + self.repo.try_borrow_with(|| { let path = self.index_path.clone().into_path_unlocked(); // Fast path without a lock diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index bd6bea77c..8d1d75e86 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -16,6 +16,7 @@ use curl::easy::Easy; use jobserver; use serde::{Serialize, Serializer}; use toml; +use lazycell::LazyCell; use core::shell::Verbosity; use core::{Shell, CliUnstable}; @@ -26,7 +27,7 @@ use util::Rustc; use util::errors::{CargoResult, CargoResultExt, CargoError, internal}; use util::paths; use util::toml as cargo_toml; -use util::{Filesystem, LazyCell}; +use util::Filesystem; use self::ConfigValue as CV; @@ -144,18 +145,18 @@ impl Config { /// Get the path to the `rustdoc` executable pub fn rustdoc(&self) -> CargoResult<&Path> { - self.rustdoc.get_or_try_init(|| self.get_tool("rustdoc")).map(AsRef::as_ref) + self.rustdoc.try_borrow_with(|| self.get_tool("rustdoc")).map(AsRef::as_ref) } /// Get the path to the `rustc` executable pub fn rustc(&self) -> CargoResult<&Rustc> { - self.rustc.get_or_try_init(|| Rustc::new(self.get_tool("rustc")?, + self.rustc.try_borrow_with(|| Rustc::new(self.get_tool("rustc")?, self.maybe_get_tool("rustc_wrapper")?)) } /// Get the path to the `cargo` executable pub fn cargo_exe(&self) -> CargoResult<&Path> { - self.cargo_exe.get_or_try_init(|| { + self.cargo_exe.try_borrow_with(|| { fn from_current_exe() -> CargoResult { // Try fetching the path to `cargo` using env::current_exe(). // The method varies per operating system and might fail; in particular, @@ -207,7 +208,7 @@ impl Config { } pub fn values(&self) -> CargoResult<&HashMap> { - self.values.get_or_try_init(|| self.load_values()) + self.values.try_borrow_with(|| self.load_values()) } pub fn set_values(&self, values: HashMap) -> CargoResult<()> { @@ -648,7 +649,7 @@ impl Config { } pub fn http(&self) -> CargoResult<&RefCell> { - let http = self.easy.get_or_try_init(|| { + let http = self.easy.try_borrow_with(|| { ops::http_handle(self).map(RefCell::new) })?; { diff --git a/src/cargo/util/lazy_cell.rs b/src/cargo/util/lazy_cell.rs deleted file mode 100644 index a55254175..000000000 --- a/src/cargo/util/lazy_cell.rs +++ /dev/null @@ -1,74 +0,0 @@ -//! A lazily fill Cell, but with frozen contents. -//! -//! With a `RefCell`, the inner contents cannot be borrowed for the lifetime of -//! the entire object, but only of the borrows returned. A `LazyCell` is a -//! variation on `RefCell` which allows borrows tied to the lifetime of the -//! outer object. -//! -//! The limitation of a `LazyCell` is that after initialized, it can never be -//! modified unless you've otherwise got a `&mut` reference - -use std::cell::UnsafeCell; - -#[derive(Debug)] -pub struct LazyCell { - inner: UnsafeCell>, -} - -impl LazyCell { - /// Creates a new empty lazy cell. - pub fn new() -> LazyCell { - LazyCell { inner: UnsafeCell::new(None) } - } - - /// Put a value into this cell. - /// - /// This function will fail if the cell has already been filled. - pub fn fill(&self, t: T) -> Result<(), T> { - unsafe { - let slot = self.inner.get(); - if (*slot).is_none() { - *slot = Some(t); - Ok(()) - } else { - Err(t) - } - } - } - - /// Borrows the contents of this lazy cell for the duration of the cell - /// itself. - /// - /// This function will return `Some` if the cell has been previously - /// initialized, and `None` if it has not yet been initialized. - pub fn borrow(&self) -> Option<&T> { - unsafe { - (*self.inner.get()).as_ref() - } - } - - /// Same as `borrow`, but the mutable version - pub fn borrow_mut(&mut self) -> Option<&mut T> { - unsafe { - (*self.inner.get()).as_mut() - } - } - - /// Consumes this `LazyCell`, returning the underlying value. - #[allow(unused_unsafe)] - pub fn into_inner(self) -> Option { - unsafe { - self.inner.into_inner() - } - } - - /// Borrows the contents of this lazy cell, initializing it if necessary. - pub fn get_or_try_init(&self, init: F) -> Result<&T, Error> - where F: FnOnce() -> Result - { - if self.borrow().is_none() && self.fill(init()?).is_err() { - unreachable!(); - } - Ok(self.borrow().unwrap()) - } -} diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index 1e1b55e36..4b500cfb2 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -7,7 +7,6 @@ pub use self::errors::{process_error, internal}; pub use self::flock::{FileLock, Filesystem}; pub use self::graph::Graph; pub use self::hex::{to_hex, short_hash, hash_u64}; -pub use self::lazy_cell::LazyCell; pub use self::lev_distance::{lev_distance}; pub use self::paths::{join_paths, path2bytes, bytes2path, dylib_path}; pub use self::paths::{normalize_path, dylib_path_envvar, without_prefix}; @@ -40,7 +39,6 @@ mod dependency_queue; mod rustc; mod sha256; mod vcs; -mod lazy_cell; mod flock; mod read2; mod progress;