зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1716518 - Upgrade lazycell to v1.3.0. r=emilio
Differential Revision: https://phabricator.services.mozilla.com/D117808
This commit is contained in:
Родитель
c4483dcd6a
Коммит
b7bfebbee1
|
@ -2705,9 +2705,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|||
|
||||
[[package]]
|
||||
name = "lazycell"
|
||||
version = "1.2.1"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
|
||||
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
||||
|
||||
[[package]]
|
||||
name = "leb128"
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"files":{"CHANGELOG.md":"0aa4629e399d08ad3a446366d465449a5e92ba95aac708be76961100a9c7df48","Cargo.toml":"15c8e40c3c9ed8635ed338b633d343702b6f4a90dcd5aa759673d31af401dd0a","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"03f6ccb4e6040abccf12b31551bbbd1800a5069a17950bbd6db850d85744800f","README.md":"c0798b2c9b0d3b9c0da509fdd9001eb2d80e6ed0850d312e36136ac269dbe49f","src/lib.rs":"7ea882864c246d78be6ea3558b92a8a7dd5732f55556cd641efb7a8caf4e64e3"},"package":"b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"}
|
||||
{"files":{"CHANGELOG.md":"54036052985525a153f9e6c9dda6a143b7494a477908fb28f5705f92dd387059","Cargo.toml":"79f5da117603f75361b9a7309f102a9ab70b66d5c1c269a30f66fdea58ccc657","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"03f6ccb4e6040abccf12b31551bbbd1800a5069a17950bbd6db850d85744800f","README.md":"26e4440387d4fc898f377eb5394f3432f8625ed8aa46e02ffb61aac8b2032967","src/lib.rs":"3fcef569bd4feb760925e34aef0e66739a0827cbc1b26ae033f57e322c3a2515","src/serde_impl.rs":"4903fb722748e91bdc4b481c7f3309e79e962f75c9717e7e13edcccd0242a52d"},"package":"830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"}
|
|
@ -1,3 +1,22 @@
|
|||
<a name="v1.3.0"></a>
|
||||
## v1.3.0 (2020-08-12)
|
||||
|
||||
|
||||
#### Bug Fixes
|
||||
|
||||
* Add custom `impl Default` to support non-Default-able `<T>` types ([b49f4eab](https://github.com/indiv0/lazycell/commit/b49f4eabec49c0a5146ef01017c2506a3c357180))
|
||||
* **lazycell:** Fix unsound aliasing in `LazyCell::fill` ([e789ac1a](https://github.com/indiv0/lazycell/commit/e789ac1a99010ad79c2d09c761fec6d67053647d), closes [#98](https://github.com/indiv0/lazycell/issues/98))
|
||||
|
||||
#### Features
|
||||
|
||||
* Implement serde support ([e728a0b6](https://github.com/indiv0/lazycell/commit/e728a0b680e607b793a81b5af7bf7f1d2c0eb5e5))
|
||||
|
||||
#### Documentation
|
||||
|
||||
* fix typo ([5f5ba9d5](https://github.com/indiv0/lazycell/commit/5f5ba9d5ac3364f8376c0c872c2e5094974385ba))
|
||||
|
||||
|
||||
|
||||
<a name="v1.2.1"></a>
|
||||
## v1.2.1 (2018-12-03)
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g. crates.io) dependencies
|
||||
# to registry (e.g., crates.io) dependencies
|
||||
#
|
||||
# If you believe there's an error in this file please file an
|
||||
# issue against the rust-lang/cargo repository. If you're
|
||||
|
@ -12,7 +12,7 @@
|
|||
|
||||
[package]
|
||||
name = "lazycell"
|
||||
version = "1.2.1"
|
||||
version = "1.3.0"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>", "Nikita Pekin <contact@nikitapek.in>"]
|
||||
include = ["CHANGELOG.md", "Cargo.toml", "LICENSE-MIT", "LICENSE-APACHE", "README.md", "src/**/*.rs"]
|
||||
description = "A library providing a lazily filled Cell struct"
|
||||
|
@ -25,6 +25,10 @@ repository = "https://github.com/indiv0/lazycell"
|
|||
version = "0.0"
|
||||
optional = true
|
||||
|
||||
[dependencies.serde]
|
||||
version = "^1"
|
||||
optional = true
|
||||
|
||||
[features]
|
||||
nightly = []
|
||||
nightly-testing = ["clippy", "nightly"]
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
</tr>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<a href="https://crates.io/crates/lazycell" title="Crates.io downloads"><img src="https://img.shields.io/crates/d/lazycell.svg" alt="cargo-downloads-badge"></img></a>
|
||||
<a href="https://indiv0.github.io/lazycell/lazycell" title="API Docs"><img src="https://img.shields.io/badge/API-docs-blue.svg" alt="api-docs-badge"></img></a>
|
||||
<a href="https://crates.io/crates/lazycell" title="Crates.io"><img src="https://img.shields.io/crates/v/lazycell.svg" alt="crates-io"></img></a>
|
||||
<a href="#license" title="License: MIT/Apache-2.0"><img src="https://img.shields.io/crates/l/lazycell.svg" alt="license-badge"></img></a>
|
||||
|
@ -30,7 +31,7 @@ Add the following to your `Cargo.toml`:
|
|||
|
||||
```toml
|
||||
[dependencies]
|
||||
lazycell = "1.2"
|
||||
lazycell = "1.3"
|
||||
```
|
||||
|
||||
And in your `lib.rs` or `main.rs`:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Original work Copyright (c) 2014 The Rust Project Developers
|
||||
// Modified work Copyright (c) 2016-2018 Nikita Pekin and the lazycell contributors
|
||||
// Modified work Copyright (c) 2016-2020 Nikita Pekin and the lazycell contributors
|
||||
// See the README.md file at the top-level directory of this distribution.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
|
@ -48,6 +48,11 @@
|
|||
#[cfg(not(test))]
|
||||
#[macro_use]
|
||||
extern crate core as std;
|
||||
#[cfg(feature = "serde")]
|
||||
extern crate serde;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
mod serde_impl;
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
use std::mem;
|
||||
|
@ -58,7 +63,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
|
|||
/// A `LazyCell` is completely frozen once filled, **unless** you have `&mut`
|
||||
/// access to it, in which case `LazyCell::borrow_mut` may be used to mutate the
|
||||
/// contents.
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
pub struct LazyCell<T> {
|
||||
inner: UnsafeCell<Option<T>>,
|
||||
}
|
||||
|
@ -71,12 +76,13 @@ impl<T> LazyCell<T> {
|
|||
|
||||
/// Put a value into this cell.
|
||||
///
|
||||
/// This function will return `Err(value)` is the cell is already full.
|
||||
/// This function will return `Err(value)` if the cell is already full.
|
||||
pub fn fill(&self, value: T) -> Result<(), T> {
|
||||
let slot = unsafe { &mut *self.inner.get() };
|
||||
let slot = unsafe { &*self.inner.get() };
|
||||
if slot.is_some() {
|
||||
return Err(value);
|
||||
}
|
||||
let slot = unsafe { &mut *self.inner.get() };
|
||||
*slot = Some(value);
|
||||
|
||||
Ok(())
|
||||
|
@ -215,6 +221,12 @@ impl<T: Copy> LazyCell<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Default for LazyCell<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl <T: Clone> Clone for LazyCell<T> {
|
||||
/// Create a clone of this `LazyCell`
|
||||
///
|
||||
|
@ -232,7 +244,7 @@ const LOCK: usize = 1;
|
|||
const SOME: usize = 2;
|
||||
|
||||
/// A lazily filled and thread-safe `Cell`, with frozen contents.
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
pub struct AtomicLazyCell<T> {
|
||||
inner: UnsafeCell<Option<T>>,
|
||||
state: AtomicUsize,
|
||||
|
@ -252,7 +264,7 @@ impl<T> AtomicLazyCell<T> {
|
|||
|
||||
/// Put a value into this cell.
|
||||
///
|
||||
/// This function will return `Err(value)` is the cell is already full.
|
||||
/// This function will return `Err(value)` if the cell is already full.
|
||||
pub fn fill(&self, t: T) -> Result<(), T> {
|
||||
if NONE != self.state.compare_and_swap(NONE, LOCK, Ordering::Acquire) {
|
||||
return Err(t);
|
||||
|
@ -325,6 +337,12 @@ impl<T: Copy> AtomicLazyCell<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Default for AtomicLazyCell<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Clone for AtomicLazyCell<T> {
|
||||
/// Create a clone of this `AtomicLazyCell`
|
||||
///
|
||||
|
@ -646,4 +664,17 @@ mod tests {
|
|||
assert_eq!(clone2.borrow(), Some(&4));
|
||||
assert_eq!(cell.borrow(), Some(&2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default() {
|
||||
#[derive(Default)]
|
||||
struct Defaultable;
|
||||
struct NonDefaultable;
|
||||
|
||||
let _: LazyCell<Defaultable> = LazyCell::default();
|
||||
let _: LazyCell<NonDefaultable> = LazyCell::default();
|
||||
|
||||
let _: AtomicLazyCell<Defaultable> = AtomicLazyCell::default();
|
||||
let _: AtomicLazyCell<NonDefaultable> = AtomicLazyCell::default();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
// Copyright (c) 2020 Nikita Pekin and the lazycell contributors
|
||||
// See the README.md file at the top-level directory of this distribution.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.use serde::ser::{Serialize, Serializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use serde::de::{self, Deserialize, Deserializer, Visitor};
|
||||
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use super::{LazyCell, AtomicLazyCell};
|
||||
|
||||
impl<T: Serialize> Serialize for LazyCell<T> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
match self.borrow() {
|
||||
Some(val) => serializer.serialize_some(val),
|
||||
None => serializer.serialize_none()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T: Serialize> Serialize for AtomicLazyCell<T> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
match self.borrow() {
|
||||
Some(val) => serializer.serialize_some(val),
|
||||
None => serializer.serialize_none()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LazyCellVisitor<T>(PhantomData<*const T>);
|
||||
impl<'de, T: Deserialize<'de>> Visitor<'de> for LazyCellVisitor<T> {
|
||||
type Value = LazyCell<T>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a LazyCell")
|
||||
}
|
||||
|
||||
fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
|
||||
let mut cell = LazyCell::new();
|
||||
cell.replace(T::deserialize(deserializer)?);
|
||||
Ok(cell)
|
||||
}
|
||||
|
||||
fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
|
||||
Ok(LazyCell::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for LazyCell<T> {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
deserializer.deserialize_option(LazyCellVisitor(PhantomData))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct AtomicLazyCellVisitor<T>(PhantomData<*const T>);
|
||||
impl<'de, T: Deserialize<'de>> Visitor<'de> for AtomicLazyCellVisitor<T> {
|
||||
type Value = AtomicLazyCell<T>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("an AtomicLazyCell")
|
||||
}
|
||||
|
||||
fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
|
||||
let mut cell = AtomicLazyCell::new();
|
||||
cell.replace(T::deserialize(deserializer)?);
|
||||
Ok(cell)
|
||||
}
|
||||
|
||||
fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
|
||||
Ok(AtomicLazyCell::new())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for AtomicLazyCell<T> {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
deserializer.deserialize_option(AtomicLazyCellVisitor(PhantomData))
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче