Skip to content

Commit

Permalink
Get rid of unused-doctest-main lint
Browse files Browse the repository at this point in the history
For this, we need to get rid of the lazy_static thing. Removing
everywhere in favor of once_cell.
  • Loading branch information
vorner committed Nov 16, 2019
1 parent 9d237ba commit c82bd2d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 118 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ members = ["benchmarks"]
[dev-dependencies]
crossbeam-utils = "~0.6"
itertools = "~0.8"
lazy_static = "~1"
model = "~0.1"
num_cpus = "~1"
version-sync = "~0.8"
once_cell = "~1"
proptest = "~0.8"
version-sync = "~0.8"

This comment has been minimized.

Copy link
@matklad

matklad Nov 17, 2019

I think all tildas are no-op here: ~major is like major, ~0.minor is like 0.minor, because Cargo interprets 0.x version in this way.

This comment has been minimized.

Copy link
@vorner

vorner Nov 17, 2019

Author Owner

I know tilda is a no-op. I still prefer it because it is more readable to many people. Without any sign, people tend to sometime assume it is exact-version. With a tilda they kind of intuitively get it's some kind of "approximate" matching.

This comment has been minimized.

Copy link
@matklad

matklad Nov 17, 2019

Yeah, that's reasonable! I guess it backfired in my case, because I know exactly how "x.y.z" works, but had to look up tilda requirements.


[profile.bench]
debug = true
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ publish = false
arc-swap = { path = ".." }
crossbeam = "~0.5" # Outdated on purpose, benchmarking something that no longer exists
crossbeam-utils = "~0.6"
lazy_static = "~1"
once_cell = "~1"
parking_lot = "~0.9"

[[bench]]
Expand Down
50 changes: 17 additions & 33 deletions benchmarks/benches/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
extern crate arc_swap;
extern crate crossbeam;
extern crate crossbeam_utils;
#[macro_use]
extern crate lazy_static;
extern crate once_cell;
extern crate parking_lot;
extern crate test;

Expand All @@ -16,6 +15,7 @@ use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use arc_swap::{ArcSwap, ArcSwapOption, Cache};
use crossbeam_utils::thread;
use once_cell::sync::Lazy;
use test::Bencher;

const ITERS: usize = 100_000;
Expand Down Expand Up @@ -72,17 +72,17 @@ macro_rules! method {
macro_rules! noise {
() => {
use super::{
test, thread, Arc, AtomicBool, Bencher, Mutex, MutexGuard, Ordering, PoisonError, ITERS,
test, thread, Arc, AtomicBool, Bencher, Lazy, Mutex, MutexGuard, Ordering, PoisonError,
ITERS,
};

lazy_static! {
static ref LOCK: Mutex<()> = Mutex::new(());
}
static LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

/// We want to prevent these tests from running concurrently, because they run multi-threaded.
/// We want to prevent these tests from running concurrently, because they run
/// multi-threaded.
///
/// If it is run as benchmark, it is OK. But if it is run as a test, they run in multiple threads
/// and some of them fight (especially the rwlock ones run for a really long time).
/// If it is run as benchmark, it is OK. But if it is run as a test, they run in multiple
/// threads and some of them fight (especially the rwlock ones run for a really long time).
fn lock() -> MutexGuard<'static, ()> {
LOCK.lock().unwrap_or_else(PoisonError::into_inner)
}
Expand Down Expand Up @@ -123,9 +123,7 @@ macro_rules! noise {
mod arc_swap_b {
use super::ArcSwap;

lazy_static! {
static ref A: ArcSwap<usize> = ArcSwap::from(Arc::new(0));
}
static A: Lazy<ArcSwap<usize>> = Lazy::new(|| ArcSwap::from_pointee(0));

fn lease() {
for _ in 0..ITERS {
Expand Down Expand Up @@ -167,9 +165,7 @@ mod arc_swap_b {
mod arc_swap_option {
use super::ArcSwapOption;

lazy_static! {
static ref A: ArcSwapOption<usize> = ArcSwapOption::from(None);
}
static A: Lazy<ArcSwapOption<usize>> = Lazy::new(|| ArcSwapOption::from(None));

fn lease() {
for _ in 0..ITERS {
Expand Down Expand Up @@ -199,9 +195,7 @@ mod arc_swap_option {
mod arc_swap_cached {
use super::{ArcSwap, Cache};

lazy_static! {
static ref A: ArcSwap<usize> = ArcSwap::from_pointee(0);
}
static A: Lazy<ArcSwap<usize>> = Lazy::new(|| ArcSwap::from_pointee(0));

fn read() {
let mut cache = Cache::from(&A as &ArcSwap<usize>);
Expand Down Expand Up @@ -229,9 +223,7 @@ mod arc_swap_cached {
}

mod mutex {
lazy_static! {
static ref M: Mutex<Arc<usize>> = Mutex::new(Arc::new(0));
}
static M: Lazy<Mutex<Arc<usize>>> = Lazy::new(|| Mutex::new(Arc::new(0)));

fn lease() {
for _ in 0..ITERS {
Expand Down Expand Up @@ -260,9 +252,7 @@ mod mutex {
mod parking_mutex {
use parking_lot::Mutex as ParkingMutex;

lazy_static! {
static ref M: ParkingMutex<Arc<usize>> = ParkingMutex::new(Arc::new(0));
}
static M: Lazy<ParkingMutex<Arc<usize>>> = Lazy::new(|| ParkingMutex::new(Arc::new(0)));

fn lease() {
for _ in 0..ITERS {
Expand Down Expand Up @@ -291,9 +281,7 @@ mod parking_mutex {
mod rwlock {
use std::sync::RwLock;

lazy_static! {
static ref L: RwLock<Arc<usize>> = RwLock::new(Arc::new(0));
}
static L: Lazy<RwLock<Arc<usize>>> = Lazy::new(|| RwLock::new(Arc::new(0)));

fn lease() {
for _ in 0..ITERS {
Expand Down Expand Up @@ -322,9 +310,7 @@ mod rwlock {
mod parking_rwlock {
use parking_lot::RwLock;

lazy_static! {
static ref L: RwLock<Arc<usize>> = RwLock::new(Arc::new(0));
}
static L: Lazy<RwLock<Arc<usize>>> = Lazy::new(|| RwLock::new(Arc::new(0)));

fn lease() {
for _ in 0..ITERS {
Expand Down Expand Up @@ -353,9 +339,7 @@ mod parking_rwlock {
mod arc_cell {
use crossbeam::atomic::ArcCell;

lazy_static! {
static ref A: ArcCell<usize> = ArcCell::new(Arc::new(0));
}
static A: Lazy<ArcCell<usize>> = Lazy::new(|| ArcCell::new(Arc::new(0)));

fn lease() {
for _ in 0..ITERS {
Expand Down
130 changes: 58 additions & 72 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
)]
#![deny(missing_docs, warnings)]
// We aim at older rust too, one without dyn
#![allow(
unknown_lints,
bare_trait_objects,
renamed_and_removed_lints,
clippy::needless_doctest_main
)]
#![allow(unknown_lints, bare_trait_objects, renamed_and_removed_lints)]
#![cfg_attr(feature = "unstable-weak", feature(weak_into_raw))]

//! Making [`Arc`][Arc] itself atomic
Expand Down Expand Up @@ -40,12 +35,13 @@
//! blocked for arbitrary long time by a steady inflow of readers.
//!
//! ```rust
//! # #[macro_use] extern crate lazy_static;
//! # extern crate once_cell;
//! # use std::sync::{Arc, RwLock};
//! # use once_cell::sync::Lazy;
//! # struct RoutingTable; struct Packet; impl RoutingTable { fn route(&self, _: Packet) {} }
//! lazy_static! {
//! static ref ROUTING_TABLE: RwLock<Arc<RoutingTable>> = RwLock::new(Arc::new(RoutingTable));
//! }
//! static ROUTING_TABLE: Lazy<RwLock<Arc<RoutingTable>>> = Lazy::new(|| {
//! RwLock::new(Arc::new(RoutingTable))
//! });
//!
//! fn process_packet(packet: Packet) {
//! let table = Arc::clone(&ROUTING_TABLE.read().unwrap());
Expand All @@ -59,12 +55,13 @@
//!
//! ```rust
//! # extern crate arc_swap;
//! # #[macro_use] extern crate lazy_static;
//! # extern crate once_cell;
//! # use arc_swap::ArcSwap;
//! # use once_cell::sync::Lazy;
//! # struct RoutingTable; struct Packet; impl RoutingTable { fn route(&self, _: Packet) {} }
//! lazy_static! {
//! static ref ROUTING_TABLE: ArcSwap<RoutingTable> = ArcSwap::from_pointee(RoutingTable);
//! }
//! static ROUTING_TABLE: Lazy<ArcSwap<RoutingTable>> = Lazy::new(|| {
//! ArcSwap::from_pointee(RoutingTable)
//! });
//!
//! fn process_packet(packet: Packet) {
//! let table = ROUTING_TABLE.load();
Expand Down Expand Up @@ -1026,77 +1023,68 @@ impl<T: RefCnt, S: LockStorage> ArcSwapAny<T, S> {
/// might have updated the value.
///
/// ```rust
/// extern crate arc_swap;
/// extern crate crossbeam_utils;
///
/// use std::sync::Arc;
///
/// use arc_swap::ArcSwap;
/// use crossbeam_utils::thread;
///
/// fn main() {
/// let cnt = ArcSwap::from(Arc::new(0));
/// thread::scope(|scope| {
/// for _ in 0..10 {
/// scope.spawn(|_| {
/// let inner = cnt.load_full();
/// // Another thread might have stored some other number than what we have
/// // between the load and store.
/// cnt.store(Arc::new(*inner + 1));
/// });
/// }
/// }).unwrap();
/// // This will likely fail:
/// // assert_eq!(10, *cnt.load_full());
/// }
/// # extern crate arc_swap;
/// # extern crate crossbeam_utils;
/// #
/// # use std::sync::Arc;
/// #
/// # use arc_swap::ArcSwap;
/// # use crossbeam_utils::thread;
/// #
/// let cnt = ArcSwap::from_pointee(0);
/// thread::scope(|scope| {
/// for _ in 0..10 {
/// scope.spawn(|_| {
/// let inner = cnt.load_full();
/// // Another thread might have stored some other number than what we have
/// // between the load and store.
/// cnt.store(Arc::new(*inner + 1));
/// });
/// }
/// }).unwrap();
/// // This will likely fail:
/// // assert_eq!(10, *cnt.load_full());
/// ```
///
/// This will, but it can call the closure multiple times to retry:
///
/// ```rust
/// extern crate arc_swap;
/// extern crate crossbeam_utils;
///
/// use std::sync::Arc;
///
/// use arc_swap::ArcSwap;
/// use crossbeam_utils::thread;
///
/// fn main() {
/// let cnt = ArcSwap::from(Arc::new(0));
/// thread::scope(|scope| {
/// for _ in 0..10 {
/// scope.spawn(|_| cnt.rcu(|inner| **inner + 1));
/// }
/// }).unwrap();
/// assert_eq!(10, *cnt.load_full());
/// }
/// # extern crate arc_swap;
/// # extern crate crossbeam_utils;
/// #
/// # use arc_swap::ArcSwap;
/// # use crossbeam_utils::thread;
/// #
/// let cnt = ArcSwap::from_pointee(0);
/// thread::scope(|scope| {
/// for _ in 0..10 {
/// scope.spawn(|_| cnt.rcu(|inner| **inner + 1));
/// }
/// }).unwrap();
/// assert_eq!(10, *cnt.load_full());
/// ```
///
/// Due to the retries, you might want to perform all the expensive operations *before* the
/// rcu. As an example, if there's a cache of some computations as a map, and the map is cheap
/// to clone but the computations are not, you could do something like this:
///
/// ```rust
/// extern crate arc_swap;
/// extern crate crossbeam_utils;
/// #[macro_use]
/// extern crate lazy_static;
///
/// use std::collections::HashMap;
/// use std::sync::Arc;
///
/// use arc_swap::ArcSwap;
///
/// # extern crate arc_swap;
/// # extern crate crossbeam_utils;
/// # extern crate once_cell;
/// #
/// # use std::collections::HashMap;
/// #
/// # use arc_swap::ArcSwap;
/// # use once_cell::sync::Lazy;
/// #
/// fn expensive_computation(x: usize) -> usize {
/// x * 2 // Let's pretend multiplication is really expensive
/// x * 2 // Let's pretend multiplication is *really expensive expensive*
/// }
///
/// type Cache = HashMap<usize, usize>;
///
/// lazy_static! {
/// static ref CACHE: ArcSwap<Cache> = ArcSwap::from(Arc::new(HashMap::new()));
/// }
/// static CACHE: Lazy<ArcSwap<Cache>> = Lazy::new(|| ArcSwap::default());
///
/// fn cached_computation(x: usize) -> usize {
/// let cache = CACHE.load();
Expand All @@ -1115,10 +1103,8 @@ impl<T: RefCnt, S: LockStorage> ArcSwapAny<T, S> {
/// result
/// }
///
/// fn main() {
/// assert_eq!(42, cached_computation(21));
/// assert_eq!(42, cached_computation(21));
/// }
/// assert_eq!(42, cached_computation(21));
/// assert_eq!(42, cached_computation(21));
/// ```
///
/// # The cost of cloning
Expand Down
8 changes: 3 additions & 5 deletions tests/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
extern crate arc_swap;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate model;
extern crate once_cell;
#[macro_use]
extern crate proptest;

use std::mem;
use std::sync::Arc;

use arc_swap::ArcSwap;
use once_cell::sync::Lazy;

#[test]
fn ops() {
Expand Down Expand Up @@ -42,9 +42,7 @@ fn ops() {

const LIMIT: usize = 5;

lazy_static! {
static ref ARCS: Vec<Arc<usize>> = (0..LIMIT).map(Arc::new).collect();
}
static ARCS: Lazy<Vec<Arc<usize>>> = Lazy::new(|| (0..LIMIT).map(Arc::new).collect());

#[test]
fn selection() {
Expand Down
8 changes: 3 additions & 5 deletions tests/stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
extern crate arc_swap;
extern crate crossbeam_utils;
extern crate itertools;
#[macro_use]
extern crate lazy_static;
extern crate num_cpus;
extern crate once_cell;

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Barrier, Mutex, MutexGuard, PoisonError};
Expand All @@ -17,10 +16,9 @@ use arc_swap::gen_lock::{Global, LockStorage, PrivateSharded, PrivateUnsharded,
use arc_swap::{ArcSwapAny, ArcSwapOption};
use crossbeam_utils::thread;
use itertools::Itertools;
use once_cell::sync::Lazy;

lazy_static! {
static ref LOCK: Mutex<()> = Mutex::new(());
}
static LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

/// We want to prevent these tests from running concurrently, because they run multi-threaded.
fn lock() -> MutexGuard<'static, ()> {
Expand Down

0 comments on commit c82bd2d

Please sign in to comment.