Skip to content

Commit

Permalink
Get rid of lazy_static.
Browse files Browse the repository at this point in the history
Now that our MSRV is 1.70, we can use std::sync::OnceCell.
  • Loading branch information
brotskydotcom committed Jul 6, 2024
1 parent 7e7f223 commit 281dd46
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async-io = ["zbus?/async-io"]
vendored = ["dbus-secret-service?/vendored", "openssl?/vendored"]

[dependencies]
lazy_static = "1"
openssl = { version = "0.10.55", optional = true }

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ then retrieving that password will return a [BadEncoding](Error::BadEncoding) er
The returned error will have the raw bytes attached,
so you can access them.
While this crate's code is thread-safe, not all of the underlying credential
stores handle access from different threads reliably.
While this crate's code is thread-safe, the underlying credential
stores may not handle access from different threads reliably.
In particular, accessing the same credential
from multiple threads at the same time can fail, especially on
Windows and Linux, because the accesses may not be serialized in the same order
Expand Down Expand Up @@ -270,16 +270,14 @@ pub fn set_default_credential_builder(new: Box<CredentialBuilder>) {
}

fn build_default_credential(target: Option<&str>, service: &str, user: &str) -> Result<Entry> {
lazy_static::lazy_static! {
static ref DEFAULT: Box<CredentialBuilder> = default::default_credential_builder();
}
static DEFAULT: std::sync::OnceLock<Box<CredentialBuilder>> = std::sync::OnceLock::new();
let guard = DEFAULT_BUILDER
.read()
.expect("Poisoned RwLock in keyring-rs: please report a bug!");
let builder = match guard.inner.as_ref() {
Some(builder) => builder,
None => &DEFAULT,
};
let builder = guard
.inner
.as_ref()
.unwrap_or_else(|| DEFAULT.get_or_init(|| default::default_credential_builder()));
let credential = builder.build(target, service, user)?;
Ok(Entry { inner: credential })
}
Expand Down
4 changes: 2 additions & 2 deletions tests/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn test_simultaneous_independent_create_set() {
fn test_multiple_create_delete_single_thread() {
let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let repeats = 10_000;
let repeats = 10;
for _i in 0..repeats {
entry.set_password(&name).expect("Can't set ascii password");
let stored_password = entry.get_password().expect("Can't get ascii password");
Expand All @@ -182,7 +182,7 @@ fn test_simultaneous_multiple_create_delete_single_thread() {
let test = move || {
let name = format!("{name}-{t}");
let entry = Entry::new(&name, &name).expect("Can't create entry");
let repeats = 10_000;
let repeats = 10;
for _i in 0..repeats {
entry.set_password(&name).expect("Can't set ascii password");
let stored_password = entry.get_password().expect("Can't get ascii password");
Expand Down

0 comments on commit 281dd46

Please sign in to comment.