Skip to content

Commit a90c7bc

Browse files
committed
Use cfg_aliases crate to make Wayland/X #[cfg(..)] less redundant
1 parent 4f6542c commit a90c7bc

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
@@ -23,7 +23,7 @@ thiserror = "1.0.30"
2323
raw-window-handle = "0.5.0"
2424
log = "0.4.17"
2525

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

35-
[target.'cfg(any(target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
35+
[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox", target_os = "linux", target_os = "freebsd"))))'.dependencies]
3636
fastrand = { version = "1.8.0", optional = true }
3737

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

58+
[build-dependencies]
59+
cfg_aliases = "0.1.1"
60+
5861
[dev-dependencies]
5962
instant = "0.1.12"
6063
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", target_os = "redox"))) },
4+
x11_platform: { all(feature = "x11", free_unix, not(target_arch = "wasm32")) },
5+
wayland_platform: { all(feature = "wayland", free_unix, not(target_arch = "wasm32")) },
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, SoftBufferError> {
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)