Skip to content

Commit

Permalink
Bus::get_pollfd generate doc for both unix & windows
Browse files Browse the repository at this point in the history
There are different implementations and signatures for `get_pollfd` depending
on whether the target plateform 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.
  • Loading branch information
fengalin committed Mar 17, 2018
1 parent 8694714 commit 89293e3
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 28 deletions.
1 change: 1 addition & 0 deletions gstreamer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build = "build.rs"

[dependencies]
bitflags = "1.0"
cfg-if = "0.1"
libc = "0.2"
glib-sys = { git = "https://github.com/gtk-rs/sys" }
gobject-sys = { git = "https://github.com/gtk-rs/sys" }
Expand Down
28 changes: 0 additions & 28 deletions gstreamer/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ use glib::source::{CallbackGuard, Continue, Priority, SourceId};
use glib_ffi;
use glib_ffi::{gboolean, gpointer};
use std::ptr;
#[cfg(any(feature = "v1_14", feature = "dox"))]
use std::mem;

#[cfg(all(unix, any(feature = "v1_14", feature = "dox")))]
use std::os::unix;

#[cfg(all(not(unix), any(feature = "v1_14", feature = "dox")))]
use std::os::windows;

use Bus;
use BusSyncReply;
Expand Down Expand Up @@ -146,26 +138,6 @@ impl Bus {
pub fn unset_sync_handler(&self) {
unsafe { ffi::gst_bus_set_sync_handler(self.to_glib_none().0, None, ptr::null_mut(), None) }
}

#[cfg(all(unix, any(feature = "v1_14", feature = "dox")))]
pub fn get_pollfd(&self) -> unix::io::RawFd {
unsafe {
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);

pollfd.fd
}
}

#[cfg(all(not(unix), any(feature = "v1_14", feature = "dox")))]
pub fn get_pollfd(&self) -> windows::io::RawHandle {
unsafe {
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);

pollfd.fd as *mut _
}
}
}

#[cfg(any(feature = "futures", feature = "dox"))]
Expand Down
48 changes: 48 additions & 0 deletions gstreamer/src/bus_unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (C) 2016-2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use]
cfg_if! {
if #[cfg(unix)] {
use ffi;
use glib_ffi;
use glib::translate::ToGlibPtr;

use std::mem;
use std::os::unix;
} else if #[cfg(feature = "dox")] {
// Declare a fake RawFd for doc generation on windows
pub mod unix {
pub mod io {
pub struct RawFd{}
}
}
}
}

use super::Bus;

pub trait BusExtManual {
fn get_pollfd(&self) -> unix::io::RawFd;
}

impl BusExtManual for Bus {
/// This is supported on **Unix** only.
fn get_pollfd(&self) -> unix::io::RawFd {
#[cfg(unix)]
unsafe {
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);

pollfd.fd
}

#[cfg(all(not(unix), feature = "dox"))]
unix::io::RawFd {}
}
}
48 changes: 48 additions & 0 deletions gstreamer/src/bus_windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (C) 2016-2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use]
cfg_if! {
if #[cfg(windows)] {
use ffi;
use glib_ffi;
use glib::translate::ToGlibPtr;

use std::mem;
use std::os::windows;
} else if #[cfg(feature = "dox")] {
// Declare a fake RawHandle for doc generation on unix
pub mod windows {
pub mod io {
pub struct RawHandle{}
}
}
}
}

use super::Bus;

pub trait BusExtManual {
fn get_pollfd(&self) -> windows::io::RawHandle;
}

impl BusExtManual for Bus {
/// This is supported on **Windows** only.
fn get_pollfd(&self) -> windows::io::RawHandle {
#[cfg(windows)]
unsafe {
let mut pollfd: glib_ffi::GPollFD = mem::zeroed();
ffi::gst_bus_get_pollfd(self.to_glib_none().0, &mut pollfd);

pollfd.fd as *mut _
}

#[cfg(all(not(windows), feature = "dox"))]
windows::io::RawHandle {}
}
}
42 changes: 42 additions & 0 deletions gstreamer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#![recursion_limit = "256"]
#[macro_use]
extern crate bitflags;
#[cfg(any(feature = "v1_14", feature = "dox"))]
#[macro_use]
extern crate cfg_if;
#[macro_use]
extern crate lazy_static;
extern crate libc;
Expand Down Expand Up @@ -102,6 +105,15 @@ pub use promise::*;
mod element;
mod bin;
mod bus;
// OS dependent Bus extensions
#[cfg(any(feature = "v1_14", feature = "dox"))]
cfg_if! {
if #[cfg(unix)] {
mod bus_unix;
} else {
mod bus_windows;
}
}
mod pad;
mod object;
mod gobject;
Expand All @@ -120,6 +132,28 @@ pub use element::{ElementExtManual, ElementMessageType, NotifyWatchId};
pub use element::{ELEMENT_METADATA_AUTHOR, ELEMENT_METADATA_DESCRIPTION, ELEMENT_METADATA_DOC_URI,
ELEMENT_METADATA_ICON_NAME, ELEMENT_METADATA_KLASS, ELEMENT_METADATA_LONGNAME};
pub use bin::BinExtManual;

// OS dependent Bus extensions
#[cfg(any(feature = "v1_14", feature = "dox"))]
cfg_if! {
if #[cfg(unix)] {
pub use bus_unix::BusExtManual;
} else {
pub use bus_windows::BusExtManual;
}
}
// also import the other plateform impl for doc
#[cfg(feature = "dox")]
cfg_if! {
if #[cfg(unix)] {
mod bus_windows;
pub use bus_windows::BusExtManual as WindowsBusExtManual;
} else {
mod bus_unix;
pub use bus_unix::BusExtManual as UnixBusExtManual;
}
}

pub use pad::{PadExtManual, PadProbeData, PadProbeId, PadProbeInfo};
pub use gobject::GObjectExtManualGst;
pub use child_proxy::ChildProxyExtManual;
Expand Down Expand Up @@ -205,6 +239,14 @@ pub mod prelude {

pub use element::ElementExtManual;
pub use bin::BinExtManual;
#[cfg(any(feature = "v1_14", feature = "dox"))]
cfg_if! {
if #[cfg(unix)] {
pub use bus_unix::BusExtManual;
} else {
pub use bus_windows::BusExtManual;
}
}
pub use pad::PadExtManual;
pub use object::GstObjectExtManual;
pub use gobject::GObjectExtManualGst;
Expand Down

0 comments on commit 89293e3

Please sign in to comment.