Switch to lazycell from crate.io

This commit is contained in:
Aleksey Kladov 2018-02-12 21:33:31 +03:00
parent cb30fba4a3
commit 7f3e86e069
8 changed files with 18 additions and 87 deletions

View File

@ -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"

View File

@ -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.

View File

@ -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;

View File

@ -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<String>)>> {
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))
})

View File

@ -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

View File

@ -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<PathBuf> {
// 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<String, ConfigValue>> {
self.values.get_or_try_init(|| self.load_values())
self.values.try_borrow_with(|| self.load_values())
}
pub fn set_values(&self, values: HashMap<String, ConfigValue>) -> CargoResult<()> {
@ -648,7 +649,7 @@ impl Config {
}
pub fn http(&self) -> CargoResult<&RefCell<Easy>> {
let http = self.easy.get_or_try_init(|| {
let http = self.easy.try_borrow_with(|| {
ops::http_handle(self).map(RefCell::new)
})?;
{

View File

@ -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<T> {
inner: UnsafeCell<Option<T>>,
}
impl<T> LazyCell<T> {
/// Creates a new empty lazy cell.
pub fn new() -> LazyCell<T> {
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<T> {
unsafe {
self.inner.into_inner()
}
}
/// Borrows the contents of this lazy cell, initializing it if necessary.
pub fn get_or_try_init<Error, F>(&self, init: F) -> Result<&T, Error>
where F: FnOnce() -> Result<T, Error>
{
if self.borrow().is_none() && self.fill(init()?).is_err() {
unreachable!();
}
Ok(self.borrow().unwrap())
}
}

View File

@ -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;