Skip to content

Commit

Permalink
ndk-glue: Replace ancient lazy_static crate with once_cell
Browse files Browse the repository at this point in the history
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]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[`const fn new()` on `RwLock`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLock.html#method.new
  • Loading branch information
MarijnS95 committed Jun 10, 2022
1 parent 8e1148f commit dc11885
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ndk-glue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
24 changes: 10 additions & 14 deletions ndk-glue/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -46,12 +46,10 @@ pub fn android_log(level: Level, tag: &CStr, msg: &CStr) {
}
}

lazy_static! {
static ref NATIVE_WINDOW: RwLock<Option<NativeWindow>> = Default::default();
static ref INPUT_QUEUE: RwLock<Option<InputQueue>> = Default::default();
static ref CONTENT_RECT: RwLock<Rect> = Default::default();
static ref LOOPER: Mutex<Option<ForeignLooper>> = Default::default();
}
static NATIVE_WINDOW: Lazy<RwLock<Option<NativeWindow>>> = Lazy::new(Default::default);
static INPUT_QUEUE: Lazy<RwLock<Option<InputQueue>>> = Lazy::new(Default::default);
static CONTENT_RECT: Lazy<RwLock<Rect>> = Lazy::new(Default::default);
static LOOPER: Lazy<Mutex<Option<ForeignLooper>>> = Lazy::new(Default::default);

static mut NATIVE_ACTIVITY: Option<NativeActivity> = None;

Expand All @@ -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<Event> {
unsafe {
Expand Down

0 comments on commit dc11885

Please sign in to comment.