Skip to content

Commit f26d419

Browse files
committed
Use cfg_aliases crate to make Wayland/X #[cfg(..)] less redundant
1 parent 3fcdf47 commit f26d419

File tree

3 files changed

+19
-54
lines changed

3 files changed

+19
-54
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ thiserror = "1.0.30"
2222
raw-window-handle = "0.5.0"
2323
log = "0.4.17"
2424

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

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

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

57+
[build-dependencies]
58+
cfg_aliases = "0.1.1"
59+
5760
[dev-dependencies]
5861
instant = "0.1.12"
5962
winit = "0.27.2"

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
cfg_aliases::cfg_aliases! {
3+
free_unix: { all(unix, not(any(target_vendor = "apple", target_os = "android"))) },
4+
x11_platform: { all(feature = "x11", free_unix, not(wasm)) },
5+
wayland_platform: { all(feature = "wayland", free_unix, not(wasm)) },
6+
}
7+
}

src/lib.rs

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,13 @@ extern crate core;
1010
mod cg;
1111
#[cfg(target_os = "redox")]
1212
mod orbital;
13-
#[cfg(all(
14-
feature = "wayland",
15-
any(
16-
target_os = "linux",
17-
target_os = "freebsd",
18-
target_os = "dragonfly",
19-
target_os = "netbsd",
20-
target_os = "openbsd"
21-
)
22-
))]
13+
#[cfg(wayland_platform)]
2314
mod wayland;
2415
#[cfg(target_arch = "wasm32")]
2516
mod web;
2617
#[cfg(target_os = "windows")]
2718
mod win32;
28-
#[cfg(all(
29-
feature = "x11",
30-
any(
31-
target_os = "linux",
32-
target_os = "freebsd",
33-
target_os = "dragonfly",
34-
target_os = "netbsd",
35-
target_os = "openbsd"
36-
)
37-
))]
19+
#[cfg(x11_platform)]
3820
mod x11;
3921

4022
mod error;
@@ -84,9 +66,9 @@ macro_rules! make_dispatch {
8466
}
8567

8668
make_dispatch! {
87-
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd")))]
69+
#[cfg(x11_platform)]
8870
X11(x11::X11Impl),
89-
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd")))]
71+
#[cfg(wayland_platform)]
9072
Wayland(wayland::WaylandImpl),
9173
#[cfg(target_os = "windows")]
9274
Win32(win32::Win32Impl),
@@ -123,48 +105,21 @@ impl GraphicsContext {
123105
raw_display_handle: RawDisplayHandle,
124106
) -> Result<Self, SwBufError> {
125107
let imple: Dispatch = match (raw_window_handle, raw_display_handle) {
126-
#[cfg(all(
127-
feature = "x11",
128-
any(
129-
target_os = "linux",
130-
target_os = "freebsd",
131-
target_os = "dragonfly",
132-
target_os = "netbsd",
133-
target_os = "openbsd"
134-
)
135-
))]
108+
#[cfg(x11_platform)]
136109
(
137110
RawWindowHandle::Xlib(xlib_window_handle),
138111
RawDisplayHandle::Xlib(xlib_display_handle),
139112
) => Dispatch::X11(unsafe {
140113
x11::X11Impl::from_xlib(xlib_window_handle, xlib_display_handle)?
141114
}),
142-
#[cfg(all(
143-
feature = "x11",
144-
any(
145-
target_os = "linux",
146-
target_os = "freebsd",
147-
target_os = "dragonfly",
148-
target_os = "netbsd",
149-
target_os = "openbsd"
150-
)
151-
))]
115+
#[cfg(x11_platform)]
152116
(
153117
RawWindowHandle::Xcb(xcb_window_handle),
154118
RawDisplayHandle::Xcb(xcb_display_handle),
155119
) => Dispatch::X11(unsafe {
156120
x11::X11Impl::from_xcb(xcb_window_handle, xcb_display_handle)?
157121
}),
158-
#[cfg(all(
159-
feature = "wayland",
160-
any(
161-
target_os = "linux",
162-
target_os = "freebsd",
163-
target_os = "dragonfly",
164-
target_os = "netbsd",
165-
target_os = "openbsd"
166-
)
167-
))]
122+
#[cfg(wayland_platform)]
168123
(
169124
RawWindowHandle::Wayland(wayland_window_handle),
170125
RawDisplayHandle::Wayland(wayland_display_handle),

0 commit comments

Comments
 (0)