Skip to content

Commit

Permalink
Use cfg_aliases crate to make Wayland/X #[cfg(..)] less redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Dec 27, 2022
1 parent 3fcdf47 commit d3abd67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 54 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ thiserror = "1.0.30"
raw-window-handle = "0.5.0"
log = "0.4.17"

[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))))'.dependencies]
nix = { version = "0.26.1", optional = true }
wayland-backend = { version = "0.1.0-beta.14", features = ["client_system"], optional = true }
wayland-client = { version = "0.30.0-beta.14", optional = true }
Expand All @@ -31,7 +31,7 @@ bytemuck = { version = "1.12.3", optional = true }
x11-dl = { version = "2.19.1", optional = true }
x11rb = { version = "0.11.0", features = ["allow-unsafe-code", "dl-libxcb"], optional = true }

[target.'cfg(any(target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox", target_os = "linux", target_os = "freebsd"))))'.dependencies]
rand = { version = "0.8.5", optional = true }

[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
Expand All @@ -54,6 +54,9 @@ features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElemen
[target.'cfg(target_os = "redox")'.dependencies]
redox_syscall = "0.3"

[build-dependencies]
cfg_aliases = "0.1.1"

[dev-dependencies]
instant = "0.1.12"
winit = "0.27.2"
Expand Down
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
cfg_aliases::cfg_aliases! {
free_unix: { all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))) },
x11_platform: { all(feature = "x11", free_unix, not(wasm)) },
wayland_platform: { all(feature = "wayland", free_unix, not(wasm)) },
}
}
59 changes: 7 additions & 52 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,13 @@ extern crate core;
mod cg;
#[cfg(target_os = "redox")]
mod orbital;
#[cfg(all(
feature = "wayland",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
#[cfg(wayland_platform)]
mod wayland;
#[cfg(target_arch = "wasm32")]
mod web;
#[cfg(target_os = "windows")]
mod win32;
#[cfg(all(
feature = "x11",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
#[cfg(x11_platform)]
mod x11;

mod error;
Expand Down Expand Up @@ -84,9 +66,9 @@ macro_rules! make_dispatch {
}

make_dispatch! {
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd")))]
#[cfg(x11_platform)]
X11(x11::X11Impl),
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd")))]
#[cfg(wayland_platform)]
Wayland(wayland::WaylandImpl),
#[cfg(target_os = "windows")]
Win32(win32::Win32Impl),
Expand Down Expand Up @@ -123,48 +105,21 @@ impl GraphicsContext {
raw_display_handle: RawDisplayHandle,
) -> Result<Self, SwBufError> {
let imple: Dispatch = match (raw_window_handle, raw_display_handle) {
#[cfg(all(
feature = "x11",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
#[cfg(x11_platform)]
(
RawWindowHandle::Xlib(xlib_window_handle),
RawDisplayHandle::Xlib(xlib_display_handle),
) => Dispatch::X11(unsafe {
x11::X11Impl::from_xlib(xlib_window_handle, xlib_display_handle)?
}),
#[cfg(all(
feature = "x11",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
#[cfg(x11_platform)]
(
RawWindowHandle::Xcb(xcb_window_handle),
RawDisplayHandle::Xcb(xcb_display_handle),
) => Dispatch::X11(unsafe {
x11::X11Impl::from_xcb(xcb_window_handle, xcb_display_handle)?
}),
#[cfg(all(
feature = "wayland",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
#[cfg(wayland_platform)]
(
RawWindowHandle::Wayland(wayland_window_handle),
RawDisplayHandle::Wayland(wayland_display_handle),
Expand Down

0 comments on commit d3abd67

Please sign in to comment.