Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
cac1768
First cut of `std::lazy` module
matklad Jan 12, 2020
237a977
integrate Lazy into std layout
KodrAus Jan 14, 2020
d1263f5
use set() in SyncOnceCell::from
KodrAus Jun 30, 2020
d101794
remove inlined lazy::Waiter in favor of sync::Once
KodrAus Jun 30, 2020
1f1cda6
appease tidy
KodrAus Jun 30, 2020
ab23a2a
ci: Set `shell: bash` as a default, remove duplicates
rye Jul 16, 2020
b26ecd2
Test codegen of compare_exchange operations
tmiasko Jul 17, 2020
48844fe
include changes to Cargo.lock
KodrAus Jul 17, 2020
d866160
bootstrap.py: guard against GC in NixOS patching support.
eddyb Jul 17, 2020
b5076fb
bootstrap.py: patch RPATH on NixOS to handle the new zlib dependency.
eddyb Jul 17, 2020
49f5078
ci: Stop setting CI_OVERRIDE_SHELL environment variable
rye Jul 17, 2020
586629c
ci: Replace exec-with-shell wrapper with "plain bash"
rye Jul 17, 2020
f7979d3
Add regression test for #69414
Alexendoo Jul 17, 2020
b0a7fbd
[experiment] ty/layout: compute both niche-filling and tagged layouts…
eddyb Apr 11, 2020
2e431c6
compare tagged/niche-filling layout and pick the best one
erikdesjardins Jul 5, 2020
3924672
document test changes
erikdesjardins Jul 16, 2020
95df802
improper_ctypes_definitions: allow `Box`
davidtwco Jul 17, 2020
4127ed1
Fix `Safety` docs for `from_raw_parts_mut`
aticu Jul 17, 2020
4adb13c
rustbuild: drop tool::should_install
Keruspe Jul 17, 2020
91314e2
Use intra-doc links in BTreeSet docs
Manishearth Jul 17, 2020
748634e
Use intra doc links in std::str
Manishearth Jul 17, 2020
4b6a027
fixes #67108 by using the external crate
Jul 17, 2020
5702ce8
Allows pathdiff package
Jul 17, 2020
fe63905
link once_cell feature to #74465
KodrAus Jul 18, 2020
c41db4c
Run fmt
Manishearth Jul 18, 2020
01418bd
Rollup merge of #72414 - KodrAus:feat/stdlazy, r=Mark-Simulacrum
Manishearth Jul 18, 2020
e775b4d
Rollup merge of #74069 - erikdesjardins:bad-niche, r=nikomatsakis
Manishearth Jul 18, 2020
7b66c66
Rollup merge of #74418 - rye:gha-dedup-shell-setting, r=pietroalbini
Manishearth Jul 18, 2020
8d1bb0e
Rollup merge of #74441 - eddyb:zlib-on-nixos, r=nagisa
Manishearth Jul 18, 2020
378f46d
Rollup merge of #74444 - Alexendoo:test-69414, r=nikomatsakis
Manishearth Jul 18, 2020
f276dd4
Rollup merge of #74448 - davidtwco:improper-ctypes-definitions-boxes,…
Manishearth Jul 18, 2020
18e8089
Rollup merge of #74449 - tmiasko:cmpxchg-test, r=nikomatsakis
Manishearth Jul 18, 2020
9597744
Rollup merge of #74450 - aticu:master, r=jonas-schievink
Manishearth Jul 18, 2020
0d669a9
Rollup merge of #74453 - Manishearth:intra-doc-std, r=jyn514
Manishearth Jul 18, 2020
5f76240
Rollup merge of #74457 - Keruspe:install, r=Mark-Simulacrum
Manishearth Jul 18, 2020
cae9c50
Rollup merge of #74464 - FedericoPonzi:fix-#67108, r=Manishearth
Manishearth Jul 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
integrate Lazy into std layout
This commit refactors the initial implementation to fit into std and
makes some other changes:

- use MaybeUninit internally in SyncOnceCell
- correctly impl Drop for lazy::Once
- port Lazy::take from once_cell from: matklad/once_cell#100

Co-Authored-By: Paul Dicker <[email protected]>
  • Loading branch information
KodrAus and pitdicker committed Jul 16, 2020
commit 237a97760ad79a21ce0655b9f5adc0cc5b5cbc79
2 changes: 1 addition & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1372,8 +1372,8 @@ checksum = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d"
name = "installer"
version = "0.0.0"
dependencies = [
"anyhow",
"clap",
"failure",
"flate2",
"lazy_static",
"num_cpus",
Expand Down
379 changes: 379 additions & 0 deletions src/libcore/lazy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,379 @@
//! Lazy values and one-time initialization of static data.

use crate::cell::{Cell, UnsafeCell};
use crate::fmt;
use crate::mem;
use crate::ops::Deref;

/// A cell which can be written to only once.
///
/// Unlike `RefCell`, a `OnceCell` only provides shared `&T` references to its value.
/// Unlike `Cell`, a `OnceCell` doesn't require copying or replacing the value to access it.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::OnceCell;
///
/// let cell = OnceCell::new();
/// assert!(cell.get().is_none());
///
/// let value: &String = cell.get_or_init(|| {
/// "Hello, World!".to_string()
/// });
/// assert_eq!(value, "Hello, World!");
/// assert!(cell.get().is_some());
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub struct OnceCell<T> {
// Invariant: written to at most once.
inner: UnsafeCell<Option<T>>,
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T> Default for OnceCell<T> {
fn default() -> Self {
Self::new()
}
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.get() {
Some(v) => f.debug_tuple("OnceCell").field(v).finish(),
None => f.write_str("OnceCell(Uninit)"),
}
}
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T: Clone> Clone for OnceCell<T> {
fn clone(&self) -> OnceCell<T> {
let res = OnceCell::new();
if let Some(value) = self.get() {
match res.set(value.clone()) {
Ok(()) => (),
Err(_) => unreachable!(),
}
}
res
}
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T: PartialEq> PartialEq for OnceCell<T> {
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
}
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T: Eq> Eq for OnceCell<T> {}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T> From<T> for OnceCell<T> {
fn from(value: T) -> Self {
OnceCell { inner: UnsafeCell::new(Some(value)) }
}
}

impl<T> OnceCell<T> {
/// Creates a new empty cell.
#[unstable(feature = "once_cell", issue = "68198")]
pub const fn new() -> OnceCell<T> {
OnceCell { inner: UnsafeCell::new(None) }
}

/// Gets the reference to the underlying value.
///
/// Returns `None` if the cell is empty.
#[unstable(feature = "once_cell", issue = "68198")]
pub fn get(&self) -> Option<&T> {
// Safety: Safe due to `inner`'s invariant
unsafe { &*self.inner.get() }.as_ref()
}

/// Gets the mutable reference to the underlying value.
///
/// Returns `None` if the cell is empty.
#[unstable(feature = "once_cell", issue = "68198")]
pub fn get_mut(&mut self) -> Option<&mut T> {
// Safety: Safe because we have unique access
unsafe { &mut *self.inner.get() }.as_mut()
}

/// Sets the contents of the cell to `value`.
///
/// # Errors
///
/// This method returns `Ok(())` if the cell was empty and `Err(value)` if
/// it was full.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::OnceCell;
///
/// let cell = OnceCell::new();
/// assert!(cell.get().is_none());
///
/// assert_eq!(cell.set(92), Ok(()));
/// assert_eq!(cell.set(62), Err(62));
///
/// assert!(cell.get().is_some());
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub fn set(&self, value: T) -> Result<(), T> {
// Safety: Safe because we cannot have overlapping mutable borrows
let slot = unsafe { &*self.inner.get() };
if slot.is_some() {
return Err(value);
}

// Safety: This is the only place where we set the slot, no races
// due to reentrancy/concurrency are possible, and we've
// checked that slot is currently `None`, so this write
// maintains the `inner`'s invariant.
let slot = unsafe { &mut *self.inner.get() };
*slot = Some(value);
Ok(())
}

/// Gets the contents of the cell, initializing it with `f`
/// if the cell was empty.
///
/// # Panics
///
/// If `f` panics, the panic is propagated to the caller, and the cell
/// remains uninitialized.
///
/// It is an error to reentrantly initialize the cell from `f`. Doing
/// so results in a panic.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::OnceCell;
///
/// let cell = OnceCell::new();
/// let value = cell.get_or_init(|| 92);
/// assert_eq!(value, &92);
/// let value = cell.get_or_init(|| unreachable!());
/// assert_eq!(value, &92);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub fn get_or_init<F>(&self, f: F) -> &T
where
F: FnOnce() -> T,
{
match self.get_or_try_init(|| Ok::<T, !>(f())) {
Ok(val) => val,
}
}

/// Gets the contents of the cell, initializing it with `f` if
/// the cell was empty. If the cell was empty and `f` failed, an
/// error is returned.
///
/// # Panics
///
/// If `f` panics, the panic is propagated to the caller, and the cell
/// remains uninitialized.
///
/// It is an error to reentrantly initialize the cell from `f`. Doing
/// so results in a panic.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::OnceCell;
///
/// let cell = OnceCell::new();
/// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
/// assert!(cell.get().is_none());
/// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
/// Ok(92)
/// });
/// assert_eq!(value, Ok(&92));
/// assert_eq!(cell.get(), Some(&92))
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
where
F: FnOnce() -> Result<T, E>,
{
if let Some(val) = self.get() {
return Ok(val);
}
let val = f()?;
// Note that *some* forms of reentrant initialization might lead to
// UB (see `reentrant_init` test). I believe that just removing this
// `assert`, while keeping `set/get` would be sound, but it seems
// better to panic, rather than to silently use an old value.
assert!(self.set(val).is_ok(), "reentrant init");
Ok(self.get().unwrap())
}

/// Consumes the cell, returning the wrapped value.
///
/// Returns `None` if the cell was empty.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::OnceCell;
///
/// let cell: OnceCell<String> = OnceCell::new();
/// assert_eq!(cell.into_inner(), None);
///
/// let cell = OnceCell::new();
/// cell.set("hello".to_string()).unwrap();
/// assert_eq!(cell.into_inner(), Some("hello".to_string()));
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub fn into_inner(self) -> Option<T> {
// Because `into_inner` takes `self` by value, the compiler statically verifies
// that it is not currently borrowed. So it is safe to move out `Option<T>`.
self.inner.into_inner()
}

/// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
///
/// Has no effect and returns `None` if the `OnceCell` hasn't been initialized.
///
/// Safety is guaranteed by requiring a mutable reference.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::OnceCell;
///
/// let mut cell: OnceCell<String> = OnceCell::new();
/// assert_eq!(cell.take(), None);
///
/// let mut cell = OnceCell::new();
/// cell.set("hello".to_string()).unwrap();
/// assert_eq!(cell.take(), Some("hello".to_string()));
/// assert_eq!(cell.get(), None);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub fn take(&mut self) -> Option<T> {
mem::take(self).into_inner()
}
}

/// A value which is initialized on the first access.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::Lazy;
///
/// let lazy: Lazy<i32> = Lazy::new(|| {
/// println!("initializing");
/// 92
/// });
/// println!("ready");
/// println!("{}", *lazy);
/// println!("{}", *lazy);
///
/// // Prints:
/// // ready
/// // initializing
/// // 92
/// // 92
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub struct Lazy<T, F = fn() -> T> {
cell: OnceCell<T>,
init: Cell<Option<F>>,
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T: fmt::Debug, F> fmt::Debug for Lazy<T, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
}
}

impl<T, F> Lazy<T, F> {
/// Creates a new lazy value with the given initializing function.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// # fn main() {
/// use std::lazy::Lazy;
///
/// let hello = "Hello, World!".to_string();
///
/// let lazy = Lazy::new(|| hello.to_uppercase());
///
/// assert_eq!(&*lazy, "HELLO, WORLD!");
/// # }
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub const fn new(init: F) -> Lazy<T, F> {
Lazy { cell: OnceCell::new(), init: Cell::new(Some(init)) }
}
}

impl<T, F: FnOnce() -> T> Lazy<T, F> {
/// Forces the evaluation of this lazy value and returns a reference to
/// the result.
///
/// This is equivalent to the `Deref` impl, but is explicit.
///
/// # Examples
///
/// ```
/// #![feature(once_cell)]
///
/// use std::lazy::Lazy;
///
/// let lazy = Lazy::new(|| 92);
///
/// assert_eq!(Lazy::force(&lazy), &92);
/// assert_eq!(&*lazy, &92);
/// ```
#[unstable(feature = "once_cell", issue = "68198")]
pub fn force(this: &Lazy<T, F>) -> &T {
this.cell.get_or_init(|| match this.init.take() {
Some(f) => f(),
None => panic!("`Lazy` instance has previously been poisoned"),
})
}
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
type Target = T;
fn deref(&self) -> &T {
Lazy::force(self)
}
}

#[unstable(feature = "once_cell", issue = "68198")]
impl<T: Default> Default for Lazy<T> {
/// Creates a new lazy value using `Default` as the initializing function.
fn default() -> Lazy<T> {
Lazy::new(T::default)
}
}
2 changes: 2 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ pub mod char;
pub mod ffi;
#[cfg(not(test))] // See #65860
pub mod iter;
#[unstable(feature = "once_cell", issue = "68198")]
pub mod lazy;
pub mod option;
pub mod panic;
pub mod panicking;
Expand Down
Loading