Skip to content

Commit

Permalink
bb8: Make parking_lot an optional feature enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
r-ml authored and djc committed Mar 13, 2023
1 parent 92983bb commit 03f95c9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
8 changes: 6 additions & 2 deletions bb8/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ rust-version = "1.60"
async-trait = "0.1"
futures-channel = "0.3.2"
futures-util = { version = "0.3.2", default-features = false, features = ["channel"] }
parking_lot = "0.12"
tokio = { version = "1.0", features = ["rt", "time", "parking_lot"] }
parking_lot = { version = "0.12", optional = true }
tokio = { version = "1.0", features = ["rt", "time"] }

[dev-dependencies]
tokio = { version = "1.0", features = ["macros"] }

[features]
parking_lot = ["dep:parking_lot", "tokio/parking_lot"]
default = ["parking_lot"]
2 changes: 1 addition & 1 deletion bb8/src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::cmp::min;
use std::sync::Arc;
use std::time::Instant;

use crate::lock::Mutex;
use futures_channel::oneshot;
use parking_lot::Mutex;

use crate::api::{Builder, ManageConnection};
use std::collections::VecDeque;
Expand Down
30 changes: 30 additions & 0 deletions bb8/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,33 @@ pub use api::{

mod inner;
mod internals;
mod lock {
#[cfg(feature = "parking_lot")]
use parking_lot::Mutex as MutexImpl;
#[cfg(feature = "parking_lot")]
use parking_lot::MutexGuard;

#[cfg(not(feature = "parking_lot"))]
use std::sync::Mutex as MutexImpl;
#[cfg(not(feature = "parking_lot"))]
use std::sync::MutexGuard;

pub(crate) struct Mutex<T>(MutexImpl<T>);

impl<T> Mutex<T> {
pub(crate) fn new(val: T) -> Self {
Self(MutexImpl::new(val))
}

pub(crate) fn lock(&self) -> MutexGuard<'_, T> {
#[cfg(feature = "parking_lot")]
{
self.0.lock()
}
#[cfg(not(feature = "parking_lot"))]
{
self.0.lock().unwrap()
}
}
}
}

0 comments on commit 03f95c9

Please sign in to comment.