Skip to content

Commit

Permalink
Merge pull request #550 from Expyron/master
Browse files Browse the repository at this point in the history
Use `const Mutex::new()` whenever possible
  • Loading branch information
asomers authored Feb 8, 2024
2 parents 17942ef + 1f61dc0 commit cdc5794
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ common: &COMMON
task:
name: MSRV
container:
image: rust:1.64.0
image: rust:1.70.0
cargo_lock_script:
- cp Cargo.lock.msrv Cargo.lock
<< : *COMMON
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [ Unreleased ] - ReleaseDate

### Changed

- Raised MSRV to 1.70.0 to remove `lazy_static` dependency
([#550](https://github.com/asomers/mockall/pull/550))

## [ 0.12.1 ] - 2023-12-21

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ See the [API docs](https://docs.rs/mockall) for more information.

# Minimum Supported Rust Version (MSRV)

Mockall is supported on Rust 1.64.0 and higher. Mockall's MSRV will not be
Mockall is supported on Rust 1.70.0 and higher. Mockall's MSRV will not be
changed in the future without bumping the major or minor version.

# License
Expand Down
5 changes: 2 additions & 3 deletions mockall/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories = ["development-tools::testing"]
keywords = ["mock", "mocking", "testing"]
documentation = "https://docs.rs/mockall"
edition = "2021"
rust-version = "1.60"
rust-version = "1.70"
description = """
A powerful mock object library for Rust.
"""
Expand Down Expand Up @@ -41,7 +41,6 @@ nightly = [
cfg-if = "1.0"
downcast = "0.11"
fragile = "2.0"
lazy_static = "1.1"
predicates = { version = "3.0.0", default-features = false }
predicates-tree = "1.0"
mockall_derive = { version = "=0.12.1", path = "../mockall_derive" }
Expand All @@ -53,7 +52,7 @@ mockall_double = { version = "^0.3.1", path = "../mockall_double" }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tracing = "0.1.32"
tracing = "0.1.35"

[[example]]
name = "serde"
Expand Down
7 changes: 2 additions & 5 deletions mockall/examples/synchronization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! overwrites the mock that another test is depending on. The solution to this
//! is to add some form of synchronization so that tests that depend on a
//! specific mock will not run in parallel. This is easily achieved using a
//! Mutex and the lazy_static crate.
//! Mutex.
#![deny(warnings)]

use mockall_double::double;
Expand Down Expand Up @@ -34,12 +34,9 @@ fn main() {
#[cfg(test)]
mod test {
use crate::my_mock::MockThing;
use lazy_static::lazy_static;
use std::sync::Mutex;

lazy_static! {
static ref MTX: Mutex<()> = Mutex::new(());
}
static MTX: Mutex<()> = Mutex::new(());

#[test]
fn test_1() {
Expand Down
4 changes: 0 additions & 4 deletions mockall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,6 @@ pub use downcast::{Any, Downcast};
#[doc(hidden)]
pub use fragile::Fragile;

/// For mocking static methods
#[doc(hidden)]
pub use lazy_static::lazy_static;

pub use predicates::{
boolean::PredicateBooleanExt,
prelude::{
Expand Down
4 changes: 1 addition & 3 deletions mockall/tests/automock_foreign_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ mod ffi {
}
}

lazy_static! {
static ref FOO_MTX: Mutex<()> = Mutex::new(());
}
static FOO_MTX: Mutex<()> = Mutex::new(());

// Ensure we can still use the original mocked function
pub fn normal_usage() {
Expand Down
4 changes: 1 addition & 3 deletions mockall/tests/automock_generic_static_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use mockall::*;
use std::sync::Mutex;

lazy_static! {
static ref A_MTX: Mutex<()> = Mutex::new(());
}
static A_MTX: Mutex<()> = Mutex::new(());

#[automock]
trait A {
Expand Down
4 changes: 1 addition & 3 deletions mockall/tests/automock_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub mod m {
use mockall::*;
type T = u32;

lazy_static! {
static ref BAR_MTX: Mutex<()> = Mutex::new(());
}
static BAR_MTX: Mutex<()> = Mutex::new(());

#[automock]
pub mod foo {
Expand Down
8 changes: 3 additions & 5 deletions mockall/tests/automock_nonsend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ pub mod static_method {
use super::*;
use std::sync::Mutex;

lazy_static! {
static ref FOO_MTX: Mutex<()> = Mutex::new(());
static ref BAR_MTX: Mutex<()> = Mutex::new(());
static ref BAZ_MTX: Mutex<()> = Mutex::new(());
}
static FOO_MTX: Mutex<()> = Mutex::new(());
static BAR_MTX: Mutex<()> = Mutex::new(());
static BAZ_MTX: Mutex<()> = Mutex::new(());

#[automock]
trait Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ mock! {
}
}

lazy_static! {
static ref FOO_MTX: Mutex<()> = Mutex::new(());
}
static FOO_MTX: Mutex<()> = Mutex::new(());

// Checkpointing the mock object should not checkpoint static methods too
#[test]
Expand Down
4 changes: 1 addition & 3 deletions mockall/tests/mock_static_method_with_lifetime_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ mock!{
}
}

lazy_static! {
static ref FOO_MTX: Mutex<()> = Mutex::new(());
}
static FOO_MTX: Mutex<()> = Mutex::new(());

#[test]
fn return_const() {
Expand Down
4 changes: 1 addition & 3 deletions mockall/tests/mock_struct_with_static_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ mock!{
}
}

lazy_static! {
static ref BAR_MTX: Mutex<()> = Mutex::new(());
}
static BAR_MTX: Mutex<()> = Mutex::new(());

// Checkpointing the mock object should not checkpoint static methods
#[test]
Expand Down
33 changes: 18 additions & 15 deletions mockall_derive/src/mock_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl MockFunction {
use ::mockall::{ViaDebug, ViaNothing};
let no_match_msg = #no_match_msg;
#deref {
let __mockall_guard = #outer_mod_path::EXPECTATIONS
let __mockall_guard = #outer_mod_path::get_expectations()
.lock().unwrap();
/*
* TODO: catch panics, then gracefully release the mutex
Expand Down Expand Up @@ -558,7 +558,7 @@ impl MockFunction {
quote!(
#(#attrs)*
{
let __mockall_timeses = #inner_mod_ident::EXPECTATIONS.lock()
let __mockall_timeses = #inner_mod_ident::get_expectations().lock()
.unwrap()
.checkpoint()
.collect::<Vec<_>>();
Expand Down Expand Up @@ -1188,14 +1188,14 @@ impl<'a> ToTokens for CommonExpectationsMethods<'a> {
&mut self.0[__mockall_l - 1]
}

#v fn new() -> Self {
Self::default()
#v const fn new() -> Self {
Self(Vec::new())
}
}
impl #ig Default for Expectations #tg #wc
{
fn default() -> Self {
Expectations(Vec::new())
Expectations::new()
}
}
).to_tokens(tokens);
Expand Down Expand Up @@ -1397,12 +1397,14 @@ impl<'a> ToTokens for ConcreteExpectationGuard<'a> {
let (ei_ig, _, _) = e_generics.split_for_impl();
let v = &self.f.privmod_vis;
quote!(
::mockall::lazy_static! {
#[doc(hidden)]
#v static ref EXPECTATIONS:
#[doc(hidden)]
#v fn get_expectations() -> &'static ::std::sync::Mutex<Expectations> {
static EXPECTATIONS:
::std::sync::Mutex<Expectations #tg> =
::std::sync::Mutex::new(Expectations::new());
&EXPECTATIONS
}

/// Like an [`&Expectation`](struct.Expectation.html) but
/// protected by a Mutex guard. Useful for mocking static
/// methods. Forwards accesses to an `Expectation` object.
Expand Down Expand Up @@ -1466,11 +1468,12 @@ impl<'a> ToTokens for GenericExpectationGuard<'a> {
let tbf = tg.as_turbofish();
let v = &self.f.privmod_vis;
quote!(
::mockall::lazy_static! {
#v static ref EXPECTATIONS:
::std::sync::Mutex<GenericExpectations> =
::std::sync::Mutex::new(GenericExpectations::new());
#[doc(hidden)]
#v fn get_expectations() -> &'static ::std::sync::Mutex<GenericExpectations> {
static CELL: ::std::sync::OnceLock<::std::sync::Mutex<GenericExpectations>> = ::std::sync::OnceLock::new();
CELL.get_or_init(|| ::std::sync::Mutex::new(GenericExpectations::new()))
}

/// Like an [`&Expectation`](struct.Expectation.html) but
/// protected by a Mutex guard. Useful for mocking static
/// methods. Forwards accesses to an `Expectation` object.
Expand Down Expand Up @@ -1571,7 +1574,7 @@ impl<'a> ToTokens for Context<'a> {
}
#[doc(hidden)]
#v fn do_checkpoint() {
let __mockall_timeses = EXPECTATIONS
let __mockall_timeses = get_expectations()
.lock()
.unwrap()
.checkpoint()
Expand All @@ -1583,7 +1586,7 @@ impl<'a> ToTokens for Context<'a> {
#v fn expect #meth_ig ( &self,) -> ExpectationGuard #e_tg
#meth_wc
{
ExpectationGuard::new(EXPECTATIONS.lock().unwrap())
ExpectationGuard::new(get_expectations().lock().unwrap())
}
}
impl #ty_ig Default for Context #ty_tg #ty_wc {
Expand All @@ -1597,7 +1600,7 @@ impl<'a> ToTokens for Context<'a> {
// Drain all expectations so other tests can run with a
// blank slate. But ignore errors so we don't
// double-panic.
let _ = EXPECTATIONS
let _ = get_expectations()
.lock()
.map(|mut g| g.checkpoint().collect::<Vec<_>>());
} else {
Expand Down

0 comments on commit cdc5794

Please sign in to comment.