Skip to content

Commit 3d0d90f

Browse files
committed
Auto merge of #5032 - matklad:lazycell, r=alexcrichton
Switch to lazycell from crate.io This switches from a home-grown implementation of `lazycell` to the one from crates.io. There are no particularly large improvements here, but our own lazy cell is definitely unsafe in theory, because of potential reentrancy in `get_or_try_init`, and the one from crates.io does not have at least this hole :-) Note that `rustc` already has `lazycell` in its Cargo.lock (because of clippy I guess?), albeit with a lower version, 0.5.
2 parents 40486ce + 7f3e86e commit 3d0d90f

File tree

8 files changed

+18
-87
lines changed

8 files changed

+18
-87
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ hex = "0.3"
3535
home = "0.3"
3636
ignore = "0.3"
3737
jobserver = "0.1.9"
38+
lazycell = "0.6"
3839
libc = "0.2"
3940
libgit2-sys = "0.6"
4041
log = "0.4"

src/cargo/core/package.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ use std::path::{Path, PathBuf};
77
use semver::Version;
88
use serde::ser;
99
use toml;
10+
use lazycell::LazyCell;
1011

1112
use core::{Dependency, Manifest, PackageId, SourceId, Target};
1213
use core::{Summary, SourceMap};
1314
use ops;
14-
use util::{Config, LazyCell, internal, lev_distance};
15+
use util::{Config, internal, lev_distance};
1516
use util::errors::{CargoResult, CargoResultExt};
1617

1718
/// Information about a package that is available somewhere in the file system.

src/cargo/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate hex;
2121
extern crate home;
2222
extern crate ignore;
2323
extern crate jobserver;
24+
extern crate lazycell;
2425
extern crate libc;
2526
extern crate libgit2_sys;
2627
extern crate num_cpus;

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::collections::{HashMap, HashSet, BTreeSet};
22
use std::ffi::OsStr;
33
use std::path::PathBuf;
4+
45
use semver::Version;
6+
use lazycell::LazyCell;
57

68
use core::{PackageId, Package, Target, TargetKind};
7-
use util::{self, CargoResult, Config, LazyCell, ProcessBuilder, process, join_paths};
9+
use util::{self, CargoResult, Config, ProcessBuilder, process, join_paths};
810

911
/// A structure returning the result of a compilation.
1012
pub struct Compilation<'cfg> {
@@ -101,7 +103,7 @@ impl<'cfg> Compilation<'cfg> {
101103
}
102104

103105
fn target_runner(&self) -> CargoResult<&Option<(PathBuf, Vec<String>)>> {
104-
self.target_runner.get_or_try_init(|| {
106+
self.target_runner.try_borrow_with(|| {
105107
let key = format!("target.{}.runner", self.target);
106108
Ok(self.config.get_path_and_args(&key)?.map(|v| v.val))
107109
})

src/cargo/sources/registry/remote.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use std::str;
99
use git2;
1010
use hex;
1111
use serde_json;
12+
use lazycell::LazyCell;
1213

1314
use core::{PackageId, SourceId};
1415
use sources::git;
1516
use sources::registry::{RegistryData, RegistryConfig, INDEX_LOCK, CRATE_TEMPLATE, VERSION_TEMPLATE};
1617
use util::network;
17-
use util::{FileLock, Filesystem, LazyCell};
18+
use util::{FileLock, Filesystem};
1819
use util::{Config, Sha256, ToUrl, Progress};
1920
use util::errors::{CargoResult, CargoResultExt, HttpNot200};
2021

@@ -43,7 +44,7 @@ impl<'cfg> RemoteRegistry<'cfg> {
4344
}
4445

4546
fn repo(&self) -> CargoResult<&git2::Repository> {
46-
self.repo.get_or_try_init(|| {
47+
self.repo.try_borrow_with(|| {
4748
let path = self.index_path.clone().into_path_unlocked();
4849

4950
// Fast path without a lock

src/cargo/util/config.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use curl::easy::Easy;
1616
use jobserver;
1717
use serde::{Serialize, Serializer};
1818
use toml;
19+
use lazycell::LazyCell;
1920

2021
use core::shell::Verbosity;
2122
use core::{Shell, CliUnstable};
@@ -26,7 +27,7 @@ use util::Rustc;
2627
use util::errors::{CargoResult, CargoResultExt, CargoError, internal};
2728
use util::paths;
2829
use util::toml as cargo_toml;
29-
use util::{Filesystem, LazyCell};
30+
use util::Filesystem;
3031

3132
use self::ConfigValue as CV;
3233

@@ -144,18 +145,18 @@ impl Config {
144145

145146
/// Get the path to the `rustdoc` executable
146147
pub fn rustdoc(&self) -> CargoResult<&Path> {
147-
self.rustdoc.get_or_try_init(|| self.get_tool("rustdoc")).map(AsRef::as_ref)
148+
self.rustdoc.try_borrow_with(|| self.get_tool("rustdoc")).map(AsRef::as_ref)
148149
}
149150

150151
/// Get the path to the `rustc` executable
151152
pub fn rustc(&self) -> CargoResult<&Rustc> {
152-
self.rustc.get_or_try_init(|| Rustc::new(self.get_tool("rustc")?,
153+
self.rustc.try_borrow_with(|| Rustc::new(self.get_tool("rustc")?,
153154
self.maybe_get_tool("rustc_wrapper")?))
154155
}
155156

156157
/// Get the path to the `cargo` executable
157158
pub fn cargo_exe(&self) -> CargoResult<&Path> {
158-
self.cargo_exe.get_or_try_init(|| {
159+
self.cargo_exe.try_borrow_with(|| {
159160
fn from_current_exe() -> CargoResult<PathBuf> {
160161
// Try fetching the path to `cargo` using env::current_exe().
161162
// The method varies per operating system and might fail; in particular,
@@ -207,7 +208,7 @@ impl Config {
207208
}
208209

209210
pub fn values(&self) -> CargoResult<&HashMap<String, ConfigValue>> {
210-
self.values.get_or_try_init(|| self.load_values())
211+
self.values.try_borrow_with(|| self.load_values())
211212
}
212213

213214
pub fn set_values(&self, values: HashMap<String, ConfigValue>) -> CargoResult<()> {
@@ -648,7 +649,7 @@ impl Config {
648649
}
649650

650651
pub fn http(&self) -> CargoResult<&RefCell<Easy>> {
651-
let http = self.easy.get_or_try_init(|| {
652+
let http = self.easy.try_borrow_with(|| {
652653
ops::http_handle(self).map(RefCell::new)
653654
})?;
654655
{

src/cargo/util/lazy_cell.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/cargo/util/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub use self::errors::{process_error, internal};
77
pub use self::flock::{FileLock, Filesystem};
88
pub use self::graph::Graph;
99
pub use self::hex::{to_hex, short_hash, hash_u64};
10-
pub use self::lazy_cell::LazyCell;
1110
pub use self::lev_distance::{lev_distance};
1211
pub use self::paths::{join_paths, path2bytes, bytes2path, dylib_path};
1312
pub use self::paths::{normalize_path, dylib_path_envvar, without_prefix};
@@ -40,7 +39,6 @@ mod dependency_queue;
4039
mod rustc;
4140
mod sha256;
4241
mod vcs;
43-
mod lazy_cell;
4442
mod flock;
4543
mod read2;
4644
mod progress;

0 commit comments

Comments
 (0)