From d7febd4f3ee449469f1b44fc9b8be3e95e1418b7 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 5 Feb 2023 17:53:48 -0600 Subject: [PATCH] Use once_cell instead of lazy_static lazy_static has better ergonomics at the call/access sites (it returns a reference to the type directly, whereas with once_cell we get a static Lazy that we must dereference instead) but the once_cell api is slated for integration into the standard library [0] and has been the "preferred" way to declare static global variables w/ deferred initialization. It's also less opaque and easier to comprehend how it works, I guess? (Both `once_cell` and `lazy_static` are already in our dependency tree, so this should have no detrimental effect on build times. It actually negligibly *improves* build times by not using macros, reducing the amount of expansion the compiler has to do by a miniscule amount.) [0]: https://github.com/rust-lang/rust/issues/74465 --- fish-rust/Cargo.lock | 1 + fish-rust/Cargo.toml | 1 + fish-rust/src/lib.rs | 3 --- fish-rust/src/wchar_ffi.rs | 9 ++++----- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/fish-rust/Cargo.lock b/fish-rust/Cargo.lock index 8fa57912a..ccd735abc 100644 --- a/fish-rust/Cargo.lock +++ b/fish-rust/Cargo.lock @@ -358,6 +358,7 @@ dependencies = [ "miette", "nix", "num-traits", + "once_cell", "unixstring", "widestring", "widestring-suffix", diff --git a/fish-rust/Cargo.toml b/fish-rust/Cargo.toml index d671ff5da..aa74d6b3a 100644 --- a/fish-rust/Cargo.toml +++ b/fish-rust/Cargo.toml @@ -15,6 +15,7 @@ lazy_static = "1.4.0" libc = "0.2.137" nix = { version = "0.25.0", default-features = false, features = [] } num-traits = "0.2.15" +once_cell = "1.17.0" unixstring = "0.2.7" widestring = "1.0.2" diff --git a/fish-rust/src/lib.rs b/fish-rust/src/lib.rs index e3645a42a..59005e146 100644 --- a/fish-rust/src/lib.rs +++ b/fish-rust/src/lib.rs @@ -4,9 +4,6 @@ #![allow(clippy::needless_return)] #![allow(clippy::manual_is_ascii_check)] -#[macro_use] -extern crate lazy_static; - mod fd_readable_set; mod fds; #[allow(rustdoc::broken_intra_doc_links)] diff --git a/fish-rust/src/wchar_ffi.rs b/fish-rust/src/wchar_ffi.rs index 32b75df67..0af4c27ad 100644 --- a/fish-rust/src/wchar_ffi.rs +++ b/fish-rust/src/wchar_ffi.rs @@ -9,6 +9,7 @@ use crate::ffi; pub use cxx::CxxWString; pub use ffi::{wchar_t, wcharz_t}; +use once_cell::sync::Lazy; pub use widestring::U32CString as W0String; pub use widestring::{u32cstr, utf32str}; pub use widestring::{Utf32Str as wstr, Utf32String as WString}; @@ -72,14 +73,12 @@ macro_rules! wcharz { pub(crate) use c_str; pub(crate) use wcharz; -lazy_static! { - /// A shared, empty CxxWString. - static ref EMPTY_WSTRING: cxx::UniquePtr = cxx::CxxWString::create(&[]); -} +static EMPTY_WSTRING: Lazy> = + Lazy::new(|| cxx::CxxWString::create(&[])); /// \return a reference to a shared empty wstring. pub fn empty_wstring() -> &'static cxx::CxxWString { - &EMPTY_WSTRING + &*EMPTY_WSTRING } /// Implement Debug for wcharz_t.