Switch to lazycell from crate.io
authorAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 12 Feb 2018 18:33:31 +0000 (21:33 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 12 Feb 2018 18:33:31 +0000 (21:33 +0300)
Cargo.toml
src/cargo/core/package.rs
src/cargo/lib.rs
src/cargo/ops/cargo_rustc/compilation.rs
src/cargo/sources/registry/remote.rs
src/cargo/util/config.rs
src/cargo/util/lazy_cell.rs [deleted file]
src/cargo/util/mod.rs

index 265a0ad1553199096b07647d8bb3dd302daf60c2..2cf17e31cf5a47bc6629a823877e32e66633a2d3 100644 (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"
index 72ac61625c4d0ce2ba2a16c5a5e456d2f008d5a2..e0c54c03af63fe1f5c9ad59e06ecd3687e4bd918 100644 (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.
index 6e8d771541c33e415dabe620e1d0a1f4f4dfe197..d60792c7a74645f5bd278b72f3052e95d9b27730 100644 (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;
index 3fe62f365afde17be8e01ce9bd07b35d57d3871d..88f82516bd4ddfa28913abf120e63c3f36d09fb1 100644 (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))
         })
index 5ffabb9d5b52e848e48645db58c46cfad3c1ea7d..331183e6d1d4a268b2f29076bcf499c436716764 100644 (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
index bd6bea77cc0bcd5cac915a5410214f24b3deef27..8d1d75e8613e5e1ae7b9a878ed67765223d3b898 100644 (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)
         })?;
         {
diff --git a/src/cargo/util/lazy_cell.rs b/src/cargo/util/lazy_cell.rs
deleted file mode 100644 (file)
index a552541..0000000
+++ /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<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())
-    }
-}
index 1e1b55e361508fb7199e57fc5a746e8ff09d5077..4b500cfb2be6f4170470251691cacdba8c01c68e 100644 (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;