use cfg_if to refactor some #[cfg()] / #[cfg(not())] pairs

This commit is contained in:
Marijn Suijten 2020-11-19 11:26:34 +01:00
parent aeb1b70581
commit e88994a0b7
6 changed files with 202 additions and 199 deletions

View file

@ -15,6 +15,7 @@ build = "build.rs"
[dependencies]
bitflags = "1.0"
libc = "0.2"
cfg-if = "1.0"
glib-sys = { git = "https://github.com/gtk-rs/gtk-rs" }
gobject-sys = { git = "https://github.com/gtk-rs/gtk-rs" }
gstreamer-sys = { path = "../gstreamer/sys", features = ["v1_8"] }

View file

@ -13,6 +13,9 @@ extern crate bitflags;
extern crate libc;
extern crate once_cell;
#[macro_use]
extern crate cfg_if;
#[macro_use]
extern crate glib;
extern crate glib_sys;

View file

@ -379,74 +379,77 @@ impl<'a> VideoInfoBuilder<'a> {
unsafe {
let mut info = mem::MaybeUninit::uninit();
#[cfg(not(feature = "v1_12"))]
let res: bool = {
// The bool return value is new with 1.11.1, see
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0
let res = if gst::version() < (1, 11, 1, 0) {
gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
);
cfg_if! {
if #[cfg(feature = "v1_16")] {
let res: bool = {
from_glib(if let Some(interlace_mode) = self.interlace_mode {
gst_video_sys::gst_video_info_set_interlaced_format(
info.as_mut_ptr(),
self.format.to_glib(),
interlace_mode.to_glib(),
self.width,
self.height,
)
} else {
gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
)
})
};
} else if #[cfg(feature = "v1_12")] {
let res: bool = {
let res = from_glib(gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
));
true
if res {
if let Some(interlace_mode) = self.interlace_mode {
let info = info.as_mut_ptr();
(*info).interlace_mode = interlace_mode.to_glib();
}
}
res
};
} else {
from_glib(gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
))
};
let res: bool = {
// The bool return value is new with 1.11.1, see
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0
let res = if gst::version() < (1, 11, 1, 0) {
gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
);
if res {
if let Some(interlace_mode) = self.interlace_mode {
let info = info.as_mut_ptr();
(*info).interlace_mode = interlace_mode.to_glib();
}
true
} else {
from_glib(gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
))
};
if res {
if let Some(interlace_mode) = self.interlace_mode {
let info = info.as_mut_ptr();
(*info).interlace_mode = interlace_mode.to_glib();
}
}
res
};
}
res
};
#[cfg(all(feature = "v1_12", not(feature = "v1_16")))]
let res: bool = {
let res = from_glib(gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
));
if res {
if let Some(interlace_mode) = self.interlace_mode {
let info = info.as_mut_ptr();
(*info).interlace_mode = interlace_mode.to_glib();
}
}
res
};
#[cfg(feature = "v1_16")]
let res: bool = {
from_glib(if let Some(interlace_mode) = self.interlace_mode {
gst_video_sys::gst_video_info_set_interlaced_format(
info.as_mut_ptr(),
self.format.to_glib(),
interlace_mode.to_glib(),
self.width,
self.height,
)
} else {
gst_video_sys::gst_video_info_set_format(
info.as_mut_ptr(),
self.format.to_glib(),
self.width,
self.height,
)
})
};
}
if !res {
return Err(glib_bool_error!("Failed to build VideoInfo"));
@ -624,45 +627,44 @@ impl VideoInfo {
pub fn builder<'a>(format: ::VideoFormat, width: u32, height: u32) -> VideoInfoBuilder<'a> {
assert_initialized_main_thread!();
#[cfg(not(any(feature = "v1_12", feature = "dox")))]
{
VideoInfoBuilder {
format,
width,
height,
interlace_mode: None,
flags: None,
size: None,
views: None,
chroma_site: None,
colorimetry: None,
par: None,
fps: None,
offset: None,
stride: None,
multiview_mode: None,
multiview_flags: None,
}
}
#[cfg(any(feature = "v1_12", feature = "dox"))]
{
VideoInfoBuilder {
format,
width,
height,
interlace_mode: None,
flags: None,
size: None,
views: None,
chroma_site: None,
colorimetry: None,
par: None,
fps: None,
offset: None,
stride: None,
multiview_mode: None,
multiview_flags: None,
field_order: None,
cfg_if! {
if #[cfg(any(feature = "v1_12", feature = "dox"))] {
VideoInfoBuilder {
format,
width,
height,
interlace_mode: None,
flags: None,
size: None,
views: None,
chroma_site: None,
colorimetry: None,
par: None,
fps: None,
offset: None,
stride: None,
multiview_mode: None,
multiview_flags: None,
field_order: None,
}
} else {
VideoInfoBuilder {
format,
width,
height,
interlace_mode: None,
flags: None,
size: None,
views: None,
chroma_site: None,
colorimetry: None,
par: None,
fps: None,
offset: None,
stride: None,
multiview_mode: None,
multiview_flags: None,
}
}
}
}
@ -871,28 +873,31 @@ impl VideoInfo {
}
pub fn align(&mut self, align: &mut ::VideoAlignment) -> bool {
#[cfg(not(feature = "v1_12"))]
unsafe {
// The bool return value is new with 1.11.1, see
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0
if gst::version() < (1, 11, 1, 0) {
gst_video_sys::gst_video_info_align(&mut self.0, &mut align.0);
true
cfg_if! {
if #[cfg(feature = "v1_12")] {
unsafe {
from_glib(gst_video_sys::gst_video_info_align(
&mut self.0,
&mut align.0,
))
}
} else {
from_glib(gst_video_sys::gst_video_info_align(
&mut self.0,
&mut align.0,
))
unsafe {
// The bool return value is new with 1.11.1, see
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0
if gst::version() < (1, 11, 1, 0) {
gst_video_sys::gst_video_info_align(&mut self.0, &mut align.0);
true
} else {
from_glib(gst_video_sys::gst_video_info_align(
&mut self.0,
&mut align.0,
))
}
}
}
}
#[cfg(feature = "v1_12")]
unsafe {
from_glib(gst_video_sys::gst_video_info_align(
&mut self.0,
&mut align.0,
))
}
}
}

View file

@ -10,8 +10,6 @@
#![recursion_limit = "256"]
#[macro_use]
extern crate bitflags;
#[cfg(any(feature = "v1_14", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_14")))]
#[macro_use]
extern crate cfg_if;
extern crate libc;

View file

@ -1704,31 +1704,30 @@ impl<'a, T: MessageErrorDomain> ErrorBuilder<'a, T> {
}
message_builder_generic_impl!(|s: &mut Self, src| {
#[cfg(any(feature = "v1_10", feature = "dox"))]
{
let details = match s.details.take() {
None => ptr::null_mut(),
Some(details) => details.into_ptr(),
};
cfg_if! {
if #[cfg(any(feature = "v1_10", feature = "dox"))] {
let details = match s.details.take() {
None => ptr::null_mut(),
Some(details) => details.into_ptr(),
};
let error = glib::Error::new(s.error, s.message);
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_error_with_details(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
details,
)
}
#[cfg(not(any(feature = "v1_10", feature = "dox")))]
{
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_error_with_details(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
details,
)
} else {
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_error(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
)
gst_sys::gst_message_new_error(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
)
}
}
});
}
@ -1771,31 +1770,30 @@ impl<'a, T: MessageErrorDomain> WarningBuilder<'a, T> {
}
message_builder_generic_impl!(|s: &mut Self, src| {
#[cfg(any(feature = "v1_10", feature = "dox"))]
{
let details = match s.details.take() {
None => ptr::null_mut(),
Some(details) => details.into_ptr(),
};
cfg_if! {
if #[cfg(any(feature = "v1_10", feature = "dox"))] {
let details = match s.details.take() {
None => ptr::null_mut(),
Some(details) => details.into_ptr(),
};
let error = glib::Error::new(s.error, s.message);
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_warning_with_details(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
details,
)
}
#[cfg(not(any(feature = "v1_10", feature = "dox")))]
{
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_warning_with_details(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
details,
)
} else {
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_warning(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
)
gst_sys::gst_message_new_warning(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
)
}
}
});
}
@ -1838,31 +1836,30 @@ impl<'a, T: MessageErrorDomain> InfoBuilder<'a, T> {
}
message_builder_generic_impl!(|s: &mut Self, src| {
#[cfg(any(feature = "v1_10", feature = "dox"))]
{
let details = match s.details.take() {
None => ptr::null_mut(),
Some(details) => details.into_ptr(),
};
cfg_if! {
if #[cfg(any(feature = "v1_10", feature = "dox"))] {
let details = match s.details.take() {
None => ptr::null_mut(),
Some(details) => details.into_ptr(),
};
let error = glib::Error::new(s.error, s.message);
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_info_with_details(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
details,
)
}
#[cfg(not(any(feature = "v1_10", feature = "dox")))]
{
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_info_with_details(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
details,
)
} else {
let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_info(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
)
gst_sys::gst_message_new_info(
src,
mut_override(error.to_glib_none().0),
s.debug.to_glib_none().0,
)
}
}
});
}

View file

@ -211,15 +211,14 @@ impl<T: ClockImpl> ClockImplExt for T {
{
let clock = self.get_instance();
#[cfg(feature = "v1_16")]
{
assert!(id.uses_clock(&clock));
}
#[cfg(not(feature = "v1_16"))]
{
unsafe {
let ptr: *mut gst_sys::GstClockEntry = id.to_glib_none().0 as *mut _;
assert_eq!((*ptr).clock, clock.as_ref().to_glib_none().0);
cfg_if! {
if #[cfg(feature = "v1_16")] {
assert!(id.uses_clock(&clock));
} else {
unsafe {
let ptr: *mut gst_sys::GstClockEntry = id.to_glib_none().0 as *mut _;
assert_eq!((*ptr).clock, clock.as_ref().to_glib_none().0);
}
}
}