Skip to content

Commit 1dfcc0c

Browse files
committed
Bus::get_pollfd generate doc for both unix & windows
There are different implementations and signatures for `get_pollfd` depending on whether the target platform is unix or windows. When generating the doc, we need both implementations to appear regardless of the target platform. This commit is inspired by the way Rust `std` library deals with `process::Command` OS dependent variants (https://doc.rust-lang.org/std/process/struct.Command.html#impl-CommandExt). Documentation can't be accurate though as we can't use the`std::os::windows` on `unix` and vice versa. As a workaround a fake fd class matching the other platform is declared. This could be further enhanced once `#[doc(cfg(...))]` is stabilized (rust-lang/rust#43781) by declaring `#[doc(cfg(unix))]` or `#[doc(cfg(windows))]` instead of the hard coded comments `This is supported on **Windows/Unix** only`. Unfortunately, these comments disappear when generating will `--all-features` because they are not part of the documentation in the gir file.
1 parent 8694714 commit 1dfcc0c

File tree

5 files changed

+139
-28
lines changed

5 files changed

+139
-28
lines changed

gstreamer/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ build = "build.rs"
1414

1515
[dependencies]
1616
bitflags = "1.0"
17+
cfg-if = "0.1"
1718
libc = "0.2"
1819
glib-sys = { git = "https://github.com/gtk-rs/sys" }
1920
gobject-sys = { git = "https://github.com/gtk-rs/sys" }

gstreamer/src/bus.rs

-28
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ use glib::source::{CallbackGuard, Continue, Priority, SourceId};
1515
use glib_ffi;
1616
use glib_ffi::{gboolean, gpointer};
1717
use std::ptr;
18-
#[cfg(any(feature = "v1_14", feature = "dox"))]
19-
use std::mem;
20-
21-
#[cfg(all(unix, any(feature = "v1_14", feature = "dox")))]
22-
use std::os::unix;
23-
24-
#[cfg(all(not(unix), any(feature = "v1_14", feature = "dox")))]
25-
use std::os::windows;
2618

2719
use Bus;
2820
use BusSyncReply;
@@ -146,26 +138,6 @@ impl Bus {
146138
pub fn unset_sync_handler(&self) {
147139
unsafe { ffi::gst_bus_set_sync_handler(self.to_glib_none().0, None, ptr::null_mut(), None) }
148140
}
149-
150-
#[cfg(all(unix, any(feature = "v1_14", feature = "dox")))]
151-
pub fn get_pollfd(&self) -> unix::io::RawFd {
152-
unsafe {
153-
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
154-
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);
155-
156-
pollfd.fd
157-
}
158-
}
159-
160-
#[cfg(all(not(unix), any(feature = "v1_14", feature = "dox")))]
161-
pub fn get_pollfd(&self) -> windows::io::RawHandle {
162-
unsafe {
163-
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
164-
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);
165-
166-
pollfd.fd as *mut _
167-
}
168-
}
169141
}
170142

171143
#[cfg(any(feature = "futures", feature = "dox"))]

gstreamer/src/bus_unix.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) 2016-2018 Sebastian Dröge <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
#[macro_use]
10+
cfg_if! {
11+
if #[cfg(unix)] {
12+
use ffi;
13+
use glib_ffi;
14+
use glib::translate::ToGlibPtr;
15+
16+
use std::mem;
17+
use std::os::unix;
18+
} else if #[cfg(feature = "dox")] {
19+
// Declare a fake RawFd for doc generation on windows
20+
pub mod unix {
21+
pub mod io {
22+
pub struct RawFd{}
23+
}
24+
}
25+
}
26+
}
27+
28+
use super::Bus;
29+
30+
pub trait BusExtManual {
31+
fn get_pollfd(&self) -> unix::io::RawFd;
32+
}
33+
34+
impl BusExtManual for Bus {
35+
/// This is supported on **Unix** only.
36+
fn get_pollfd(&self) -> unix::io::RawFd {
37+
#[cfg(unix)]
38+
unsafe {
39+
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
40+
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);
41+
42+
pollfd.fd
43+
}
44+
45+
#[cfg(all(not(unix), feature = "dox"))]
46+
unix::io::RawFd {}
47+
}
48+
}

gstreamer/src/bus_windows.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) 2016-2018 Sebastian Dröge <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
#[macro_use]
10+
cfg_if! {
11+
if #[cfg(windows)] {
12+
use ffi;
13+
use glib_ffi;
14+
use glib::translate::ToGlibPtr;
15+
16+
use std::mem;
17+
use std::os::windows;
18+
} else if #[cfg(feature = "dox")] {
19+
// Declare a fake RawHandle for doc generation on unix
20+
pub mod windows {
21+
pub mod io {
22+
pub struct RawHandle{}
23+
}
24+
}
25+
}
26+
}
27+
28+
use super::Bus;
29+
30+
pub trait BusExtManual {
31+
fn get_pollfd(&self) -> windows::io::RawHandle;
32+
}
33+
34+
impl BusExtManual for Bus {
35+
/// This is supported on **Windows** only.
36+
fn get_pollfd(&self) -> windows::io::RawHandle {
37+
#[cfg(windows)]
38+
unsafe {
39+
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
40+
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);
41+
42+
pollfd.fd as *mut _
43+
}
44+
45+
#[cfg(all(not(windows), feature = "dox"))]
46+
windows::io::RawHandle {}
47+
}
48+
}

gstreamer/src/lib.rs

+42
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#![recursion_limit = "256"]
1010
#[macro_use]
1111
extern crate bitflags;
12+
#[cfg(any(feature = "v1_14", feature = "dox"))]
13+
#[macro_use]
14+
extern crate cfg_if;
1215
#[macro_use]
1316
extern crate lazy_static;
1417
extern crate libc;
@@ -102,6 +105,15 @@ pub use promise::*;
102105
mod element;
103106
mod bin;
104107
mod bus;
108+
// OS dependent Bus extensions
109+
#[cfg(any(feature = "v1_14", feature = "dox"))]
110+
cfg_if! {
111+
if #[cfg(unix)] {
112+
mod bus_unix;
113+
} else {
114+
mod bus_windows;
115+
}
116+
}
105117
mod pad;
106118
mod object;
107119
mod gobject;
@@ -120,6 +132,28 @@ pub use element::{ElementExtManual, ElementMessageType, NotifyWatchId};
120132
pub use element::{ELEMENT_METADATA_AUTHOR, ELEMENT_METADATA_DESCRIPTION, ELEMENT_METADATA_DOC_URI,
121133
ELEMENT_METADATA_ICON_NAME, ELEMENT_METADATA_KLASS, ELEMENT_METADATA_LONGNAME};
122134
pub use bin::BinExtManual;
135+
136+
// OS dependent Bus extensions
137+
#[cfg(any(feature = "v1_14", feature = "dox"))]
138+
cfg_if! {
139+
if #[cfg(unix)] {
140+
pub use bus_unix::BusExtManual;
141+
} else {
142+
pub use bus_windows::BusExtManual;
143+
}
144+
}
145+
// also import the other plateform impl for doc
146+
#[cfg(feature = "dox")]
147+
cfg_if! {
148+
if #[cfg(unix)] {
149+
mod bus_windows;
150+
pub use bus_windows::BusExtManual as WindowsBusExtManual;
151+
} else {
152+
mod bus_unix;
153+
pub use bus_unix::BusExtManual as UnixBusExtManual;
154+
}
155+
}
156+
123157
pub use pad::{PadExtManual, PadProbeData, PadProbeId, PadProbeInfo};
124158
pub use gobject::GObjectExtManualGst;
125159
pub use child_proxy::ChildProxyExtManual;
@@ -205,6 +239,14 @@ pub mod prelude {
205239

206240
pub use element::ElementExtManual;
207241
pub use bin::BinExtManual;
242+
#[cfg(any(feature = "v1_14", feature = "dox"))]
243+
cfg_if! {
244+
if #[cfg(unix)] {
245+
pub use bus_unix::BusExtManual;
246+
} else {
247+
pub use bus_windows::BusExtManual;
248+
}
249+
}
208250
pub use pad::PadExtManual;
209251
pub use object::GstObjectExtManual;
210252
pub use gobject::GObjectExtManualGst;

0 commit comments

Comments
 (0)