Skip to content

Commit

Permalink
Replace lazy_static with once_cell (#627)
Browse files Browse the repository at this point in the history
* Replace `lazy_static` with `once_cell`

* Don't expose `Lazy` in public API

* Add changelog entry
  • Loading branch information
daxpedda authored Jun 5, 2023
1 parent 9233b1e commit 394231b
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 164 deletions.
91 changes: 47 additions & 44 deletions wayland-backend/src/sys/client_impl/mod.rs

Large diffs are not rendered by default.

137 changes: 70 additions & 67 deletions wayland-backend/src/sys/server_impl/mod.rs

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions wayland-egl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl WlEglSurface {
if width <= 0 || height <= 0 {
return Err(Error::InvalidSize);
}
let ptr = ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_create, surface, width, height);
let ptr = ffi_dispatch!(wayland_egl_handle(), wl_egl_window_create, surface, width, height);
if ptr.is_null() {
panic!("egl window allocation failed");
}
Expand All @@ -84,7 +84,7 @@ impl WlEglSurface {
let mut h = 0i32;
unsafe {
ffi_dispatch!(
WAYLAND_EGL_HANDLE,
wayland_egl_handle(),
wl_egl_window_get_attached_size,
self.ptr,
&mut w as *mut i32,
Expand All @@ -102,7 +102,15 @@ impl WlEglSurface {
/// direction of the resizing if necessary.
pub fn resize(&self, width: i32, height: i32, dx: i32, dy: i32) {
unsafe {
ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_resize, self.ptr, width, height, dx, dy)
ffi_dispatch!(
wayland_egl_handle(),
wl_egl_window_resize,
self.ptr,
width,
height,
dx,
dy
)
}
}

Expand All @@ -122,7 +130,7 @@ unsafe impl Send for WlEglSurface {}
impl Drop for WlEglSurface {
fn drop(&mut self) {
unsafe {
ffi_dispatch!(WAYLAND_EGL_HANDLE, wl_egl_window_destroy, self.ptr);
ffi_dispatch!(wayland_egl_handle(), wl_egl_window_destroy, self.ptr);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions wayland-sys/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

#### Breaking changes

- `WAYLAND_*_OPTION` and `WAYLAND_*_HANDLE` are now functions with the same name:
`wayland_*_option()` and `wayland_*_handle()`.

## 0.30.1

#### Bugfixes
Expand Down
6 changes: 3 additions & 3 deletions wayland-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ rust-version = "1.59"
readme = "README.md"

[dependencies]
dlib = { version = "0.5" }
dlib = { version = "0.5.1" }
libc = { version = "0.2", optional = true }
lazy_static = { version = "1.4", optional = true }
once_cell = { version = "1.0", optional = true }
memoffset = { version = "0.9", optional = true }
log = "0.4"

[build-dependencies]
pkg-config = "0.3.7"

[features]
dlopen = ["lazy_static"]
dlopen = ["once_cell"]
client = []
cursor = ["client"]
egl = ["client"]
Expand Down
26 changes: 17 additions & 9 deletions wayland-sys/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Bindings to the client library `libwayland-client.so`
//!
//! The generated handle is named `WAYLAND_CLIENT_HANDLE`
//! The generated handle is named `wayland_client_handle()`
#![cfg_attr(rustfmt, rustfmt_skip)]

#[cfg(all(feature = "client", feature = "dlopen"))]
use once_cell::sync::Lazy;
#[cfg(feature = "client")]
use super::common::*;
#[cfg(feature = "client")]
Expand Down Expand Up @@ -84,8 +86,8 @@ external_library!(WaylandClient, "wayland-client",
);

#[cfg(all(feature = "client", feature = "dlopen"))]
lazy_static::lazy_static!(
pub static ref WAYLAND_CLIENT_OPTION: Option<WaylandClient> = {
pub fn wayland_client_option() -> Option<&'static WaylandClient> {
static WAYLAND_CLIENT_OPTION: Lazy<Option<WaylandClient>> = Lazy::new(||{
// This is a workaround for Ubuntu 17.04, which doesn't have a bare symlink
// for libwayland-client.so but does have it with the version numbers for
// whatever reason.
Expand All @@ -105,17 +107,23 @@ lazy_static::lazy_static!(
}
}
None
};
pub static ref WAYLAND_CLIENT_HANDLE: &'static WaylandClient = {
WAYLAND_CLIENT_OPTION.as_ref().expect("Library libwayland-client.so could not be loaded.")
};
);
});

WAYLAND_CLIENT_OPTION.as_ref()
}

#[cfg(all(feature = "client", feature = "dlopen"))]
pub fn wayland_client_handle() -> &'static WaylandClient {
static WAYLAND_CLIENT_HANDLE: Lazy<&'static WaylandClient> = Lazy::new(|| wayland_client_option().expect("Library libwayland-client.so could not be loaded."));

&WAYLAND_CLIENT_HANDLE
}

#[cfg(all(feature = "client", not(feature = "dlopen")))]
pub fn is_lib_available() -> bool {
true
}
#[cfg(all(feature = "client", feature = "dlopen"))]
pub fn is_lib_available() -> bool {
WAYLAND_CLIENT_OPTION.is_some()
wayland_client_option().is_some()
}
31 changes: 20 additions & 11 deletions wayland-sys/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Bindings to the `wayland-cursor.so` library
//!
//! The created handle is named `WAYLAND_CURSOR_HANDLE`.
//! The created handle is named `wayland_cursor_handle()`.
use crate::client::wl_proxy;
#[cfg(feature = "dlopen")]
use once_cell::sync::Lazy;
use std::os::raw::{c_char, c_int, c_uint};

pub enum wl_cursor_theme {}
Expand Down Expand Up @@ -39,16 +41,15 @@ external_library!(WaylandCursor, "wayland-cursor",
);

#[cfg(feature = "dlopen")]
lazy_static::lazy_static!(
pub static ref WAYLAND_CURSOR_OPTION: Option<WaylandCursor> = {
pub fn wayland_cursor_option() -> Option<&'static WaylandCursor> {
static WAYLAND_CURSOR_OPTION: Lazy<Option<WaylandCursor>> = Lazy::new(|| {
// This is a workaround for Ubuntu 17.04, which doesn't have a bare symlink
// for libwayland-client.so but does have it with the version numbers for
// whatever reason.
//
// We could do some trickery with str slices but that is more trouble
// than its worth
let versions = ["libwayland-cursor.so",
"libwayland-cursor.so.0"];
let versions = ["libwayland-cursor.so", "libwayland-cursor.so.0"];

for ver in &versions {
match unsafe { WaylandCursor::open(ver) } {
Expand All @@ -61,17 +62,25 @@ lazy_static::lazy_static!(
}
}
None
};
pub static ref WAYLAND_CURSOR_HANDLE: &'static WaylandCursor = {
WAYLAND_CURSOR_OPTION.as_ref().expect("Library libwayland-cursor.so could not be loaded.")
};
);
});

WAYLAND_CURSOR_OPTION.as_ref()
}

#[cfg(feature = "dlopen")]
pub fn wayland_cursor_handle() -> &'static WaylandCursor {
static WAYLAND_CURSOR_HANDLE: Lazy<&'static WaylandCursor> = Lazy::new(|| {
wayland_cursor_option().expect("Library libwayland-cursor.so could not be loaded.")
});

&WAYLAND_CURSOR_HANDLE
}

#[cfg(not(feature = "dlopen"))]
pub fn is_lib_available() -> bool {
true
}
#[cfg(feature = "dlopen")]
pub fn is_lib_available() -> bool {
WAYLAND_CURSOR_OPTION.is_some()
wayland_cursor_option().is_some()
}
30 changes: 19 additions & 11 deletions wayland-sys/src/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
//!
//! This lib allows to create EGL surfaces out of wayland surfaces.
//!
//! The created handle is named `WAYLAND_EGl_HANDLE`.
//! The created handle is named `wayland_egl_handle()`.
use crate::client::wl_proxy;
#[cfg(feature = "dlopen")]
use once_cell::sync::Lazy;
use std::os::raw::c_int;

pub enum wl_egl_window {}
Expand All @@ -18,16 +20,15 @@ external_library!(WaylandEgl, "wayland-egl",
);

#[cfg(feature = "dlopen")]
lazy_static::lazy_static!(
pub static ref WAYLAND_EGL_OPTION: Option<WaylandEgl> = {
pub fn wayland_egl_option() -> Option<&'static WaylandEgl> {
static WAYLAND_EGL_OPTION: Lazy<Option<WaylandEgl>> = Lazy::new(|| {
// This is a workaround for Ubuntu 17.04, which doesn't have a bare symlink
// for libwayland-client.so but does have it with the version numbers for
// whatever reason.
//
// We could do some trickery with str slices but that is more trouble
// than its worth
let versions = ["libwayland-egl.so",
"libwayland-egl.so.1"];
let versions = ["libwayland-egl.so", "libwayland-egl.so.1"];

for ver in &versions {
match unsafe { WaylandEgl::open(ver) } {
Expand All @@ -40,17 +41,24 @@ lazy_static::lazy_static!(
}
}
None
};
pub static ref WAYLAND_EGL_HANDLE: &'static WaylandEgl = {
WAYLAND_EGL_OPTION.as_ref().expect("Library libwayland-egl.so could not be loaded.")
};
);
});

WAYLAND_EGL_OPTION.as_ref()
}

#[cfg(feature = "dlopen")]
pub fn wayland_egl_handle() -> &'static WaylandEgl {
static WAYLAND_EGL_HANDLE: Lazy<&'static WaylandEgl> =
Lazy::new(|| wayland_egl_option().expect("Library libwayland-egl.so could not be loaded."));

&WAYLAND_EGL_HANDLE
}

#[cfg(not(feature = "dlopen"))]
pub fn is_lib_available() -> bool {
true
}
#[cfg(feature = "dlopen")]
pub fn is_lib_available() -> bool {
WAYLAND_EGL_OPTION.is_some()
wayland_egl_option().is_some()
}
6 changes: 3 additions & 3 deletions wayland-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! use wayland_sys::client::*;
//!
//! let display_ptr = unsafe {
//! ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_display_connect, ::std::ptr::null())
//! ffi_dispatch!(wayland_client_handle(), wl_display_connect, ::std::ptr::null())
//! };
//! ```
//!
Expand Down Expand Up @@ -61,15 +61,15 @@ pub use libc::{gid_t, pid_t, uid_t};
#[cfg(feature = "dlopen")]
#[macro_export]
macro_rules! ffi_dispatch(
($handle: ident, $func: ident $(, $arg: expr)* $(,)?) => (
($handle: expr, $func: ident $(, $arg: expr)* $(,)?) => (
($handle.$func)($($arg),*)
)
);

#[cfg(not(feature = "dlopen"))]
#[macro_export]
macro_rules! ffi_dispatch(
($handle: ident, $func: ident $(, $arg: expr)* $(,)?) => (
($handle: expr, $func: ident $(, $arg: expr)* $(,)?) => (
$func($($arg),*)
)
);
32 changes: 20 additions & 12 deletions wayland-sys/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Bindings to the client library `libwayland-server.so`
//!
//! The generated handle is named `WAYLAND_SERVER_HANDLE`
//! The generated handle is named `wayland_server_handle()`
#![cfg_attr(rustfmt, rustfmt_skip)]

Expand All @@ -10,6 +10,8 @@ use libc::{gid_t, pid_t, uid_t};
#[cfg(feature = "server")]
use std::os::raw::c_char;
use std::os::raw::{c_int, c_void};
#[cfg(all(feature = "server", feature = "dlopen"))]
use once_cell::sync::Lazy;

pub enum wl_client {}
pub enum wl_display {}
Expand Down Expand Up @@ -152,8 +154,8 @@ external_library!(WaylandServer, "wayland-server",
);

#[cfg(all(feature = "server", feature = "dlopen"))]
lazy_static::lazy_static!(
pub static ref WAYLAND_SERVER_OPTION: Option<WaylandServer> = {
pub fn wayland_server_option() -> Option<&'static WaylandServer> {
static WAYLAND_SERVER_OPTION: Lazy<Option<WaylandServer>> = Lazy::new(||{
// This is a workaround for Ubuntu 17.04, which doesn't have a bare symlink
// for libwayland-server.so but does have it with the version numbers for
// whatever reason.
Expand All @@ -173,26 +175,32 @@ lazy_static::lazy_static!(
}
}
None
};
pub static ref WAYLAND_SERVER_HANDLE: &'static WaylandServer = {
WAYLAND_SERVER_OPTION.as_ref().expect("Library libwayland-server.so could not be loaded.")
};
);
});

WAYLAND_SERVER_OPTION.as_ref()
}

#[cfg(all(feature = "server", feature = "dlopen"))]
pub fn wayland_server_handle() -> &'static WaylandServer {
static WAYLAND_SERVER_HANDLE: Lazy<&'static WaylandServer> = Lazy::new(|| wayland_server_option().expect("Library libwayland-server.so could not be loaded."));

&WAYLAND_SERVER_HANDLE
}

#[cfg(all(feature = "server", not(feature = "dlopen")))]
pub fn is_lib_available() -> bool {
true
}
#[cfg(all(feature = "server", feature = "dlopen"))]
pub fn is_lib_available() -> bool {
WAYLAND_SERVER_OPTION.is_some()
wayland_server_option().is_some()
}

#[cfg(feature = "server")]
pub mod signal {
#![allow(clippy::cast_ptr_alignment, clippy::missing_safety_doc)]
#[cfg(feature = "dlopen")]
use super::WAYLAND_SERVER_HANDLE as WSH;
use super::wayland_server_handle as wsh;
#[cfg(not(feature = "dlopen"))]
use super::{wl_list_init, wl_list_insert};
use super::{wl_listener, wl_notify_func_t, wl_signal};
Expand Down Expand Up @@ -230,13 +238,13 @@ pub mod signal {

pub unsafe fn wl_signal_init(signal: *mut wl_signal) {
// Safety: signal is a valid initialized wl_signal
ffi_dispatch!(WSH, wl_list_init, unsafe { &mut (*signal).listener_list });
ffi_dispatch!(wsh(), wl_list_init, unsafe { &mut (*signal).listener_list });
}

pub unsafe fn wl_signal_add(signal: *mut wl_signal, listener: *mut wl_listener) {
// Safety: signal and listener are valid pointers
ffi_dispatch!(
WSH,
wsh(),
wl_list_insert,
unsafe { (*signal).listener_list.prev },
unsafe { &mut (*listener).link }
Expand Down

0 comments on commit 394231b

Please sign in to comment.