From dc1188511e4cf4433eb9c1ca221c8b5e384e10ef Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 10 Jun 2022 13:55:03 +0200 Subject: [PATCH] ndk-glue: Replace ancient `lazy_static` crate with `once_cell` Piggybacking on the [motivation in winit]: `lazy_static!` is a macro whereas `once_cell` achieves the same using generics. Its implementation has also been [proposed for inclusion in `std`], making it easier for us to switch to a standardized version if/when that happens. The author of that winit PR is making this change to many more crates, slowly turning the scales in favour of `once_cell` in our dependency tree too. Furthermore `lazy_static` hasn't published any updates for 3 years, and the new syntax is closer for dropping this wrapping completely when the necessary constructors become `const` (i.e. switching to `parking_lot` will give us a [`const fn new()` on `RwLock`]) or this feature lands in stable `std`. [motivation in winit]: https://github.com/rust-windowing/winit/pull/2313 [proposed for inclusion in `std`]: https://github.com/rust-lang/rust/issues/74465 [`const fn new()` on `RwLock`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLock.html#method.new --- ndk-glue/Cargo.toml | 2 +- ndk-glue/src/lib.rs | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/ndk-glue/Cargo.toml b/ndk-glue/Cargo.toml index 34f63a97..c37827ae 100644 --- a/ndk-glue/Cargo.toml +++ b/ndk-glue/Cargo.toml @@ -16,7 +16,7 @@ ndk = { path = "../ndk", version = "0.6.0" } ndk-context = { path = "../ndk-context", version = "0.1.1" } ndk-macro = { path = "../ndk-macro", version = "0.3.0" } ndk-sys = { path = "../ndk-sys", version = "0.3.0" } -lazy_static = "1.4.0" +once_cell = "1" libc = "0.2.84" log = "0.4.14" android_logger = { version = "0.10.1", optional = true } diff --git a/ndk-glue/src/lib.rs b/ndk-glue/src/lib.rs index ecbe6ae5..65678e9c 100644 --- a/ndk-glue/src/lib.rs +++ b/ndk-glue/src/lib.rs @@ -1,10 +1,10 @@ -use lazy_static::lazy_static; use log::Level; use ndk::input_queue::InputQueue; use ndk::looper::{FdEvent, ForeignLooper, ThreadLooper}; use ndk::native_activity::NativeActivity; use ndk::native_window::NativeWindow; use ndk_sys::{AInputQueue, ANativeActivity, ANativeWindow, ARect}; +use once_cell::sync::Lazy; use std::ffi::{CStr, CString}; use std::fs::File; use std::io::{BufRead, BufReader}; @@ -46,12 +46,10 @@ pub fn android_log(level: Level, tag: &CStr, msg: &CStr) { } } -lazy_static! { - static ref NATIVE_WINDOW: RwLock> = Default::default(); - static ref INPUT_QUEUE: RwLock> = Default::default(); - static ref CONTENT_RECT: RwLock = Default::default(); - static ref LOOPER: Mutex> = Default::default(); -} +static NATIVE_WINDOW: Lazy>> = Lazy::new(Default::default); +static INPUT_QUEUE: Lazy>> = Lazy::new(Default::default); +static CONTENT_RECT: Lazy> = Lazy::new(Default::default); +static LOOPER: Lazy>> = Lazy::new(Default::default); static mut NATIVE_ACTIVITY: Option = None; @@ -72,13 +70,11 @@ pub fn content_rect() -> Rect { CONTENT_RECT.read().unwrap().clone() } -lazy_static! { - static ref PIPE: [RawFd; 2] = { - let mut pipe: [RawFd; 2] = Default::default(); - unsafe { libc::pipe(pipe.as_mut_ptr()) }; - pipe - }; -} +static PIPE: Lazy<[RawFd; 2]> = Lazy::new(|| { + let mut pipe: [RawFd; 2] = Default::default(); + unsafe { libc::pipe(pipe.as_mut_ptr()) }; + pipe +}); pub fn poll_events() -> Option { unsafe {