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] [dependencies]
bitflags = "1.0" bitflags = "1.0"
libc = "0.2" libc = "0.2"
cfg-if = "1.0"
glib-sys = { git = "https://github.com/gtk-rs/gtk-rs" } glib-sys = { git = "https://github.com/gtk-rs/gtk-rs" }
gobject-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"] } gstreamer-sys = { path = "../gstreamer/sys", features = ["v1_8"] }

View file

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

View file

@ -379,7 +379,45 @@ impl<'a> VideoInfoBuilder<'a> {
unsafe { unsafe {
let mut info = mem::MaybeUninit::uninit(); let mut info = mem::MaybeUninit::uninit();
#[cfg(not(feature = "v1_12"))] 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,
));
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 {
let res: bool = { let res: bool = {
// The bool return value is new with 1.11.1, see // The bool return value is new with 1.11.1, see
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0 // https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0
@ -410,44 +448,9 @@ impl<'a> VideoInfoBuilder<'a> {
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 { if !res {
return Err(glib_bool_error!("Failed to build VideoInfo")); return Err(glib_bool_error!("Failed to build VideoInfo"));
} }
@ -624,28 +627,8 @@ impl VideoInfo {
pub fn builder<'a>(format: ::VideoFormat, width: u32, height: u32) -> VideoInfoBuilder<'a> { pub fn builder<'a>(format: ::VideoFormat, width: u32, height: u32) -> VideoInfoBuilder<'a> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
#[cfg(not(any(feature = "v1_12", feature = "dox")))] 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,
}
}
#[cfg(any(feature = "v1_12", feature = "dox"))]
{
VideoInfoBuilder { VideoInfoBuilder {
format, format,
width, width,
@ -664,6 +647,25 @@ impl VideoInfo {
multiview_flags: None, multiview_flags: None,
field_order: 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,7 +873,15 @@ impl VideoInfo {
} }
pub fn align(&mut self, align: &mut ::VideoAlignment) -> bool { pub fn align(&mut self, align: &mut ::VideoAlignment) -> bool {
#[cfg(not(feature = "v1_12"))] cfg_if! {
if #[cfg(feature = "v1_12")] {
unsafe {
from_glib(gst_video_sys::gst_video_info_align(
&mut self.0,
&mut align.0,
))
}
} else {
unsafe { unsafe {
// The bool return value is new with 1.11.1, see // The bool return value is new with 1.11.1, see
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0 // https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/commit/17cdd369e6f2f73329d27dfceb50011f40f1ceb0
@ -886,12 +896,7 @@ impl VideoInfo {
)) ))
} }
} }
#[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"] #![recursion_limit = "256"]
#[macro_use] #[macro_use]
extern crate bitflags; extern crate bitflags;
#[cfg(any(feature = "v1_14", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_14")))]
#[macro_use] #[macro_use]
extern crate cfg_if; extern crate cfg_if;
extern crate libc; extern crate libc;

View file

@ -1704,8 +1704,8 @@ impl<'a, T: MessageErrorDomain> ErrorBuilder<'a, T> {
} }
message_builder_generic_impl!(|s: &mut Self, src| { message_builder_generic_impl!(|s: &mut Self, src| {
#[cfg(any(feature = "v1_10", feature = "dox"))] cfg_if! {
{ if #[cfg(any(feature = "v1_10", feature = "dox"))] {
let details = match s.details.take() { let details = match s.details.take() {
None => ptr::null_mut(), None => ptr::null_mut(),
Some(details) => details.into_ptr(), Some(details) => details.into_ptr(),
@ -1719,9 +1719,7 @@ impl<'a, T: MessageErrorDomain> ErrorBuilder<'a, T> {
s.debug.to_glib_none().0, s.debug.to_glib_none().0,
details, details,
) )
} } else {
#[cfg(not(any(feature = "v1_10", feature = "dox")))]
{
let error = glib::Error::new(s.error, s.message); let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_error( gst_sys::gst_message_new_error(
@ -1730,6 +1728,7 @@ impl<'a, T: MessageErrorDomain> ErrorBuilder<'a, T> {
s.debug.to_glib_none().0, s.debug.to_glib_none().0,
) )
} }
}
}); });
} }
@ -1771,8 +1770,8 @@ impl<'a, T: MessageErrorDomain> WarningBuilder<'a, T> {
} }
message_builder_generic_impl!(|s: &mut Self, src| { message_builder_generic_impl!(|s: &mut Self, src| {
#[cfg(any(feature = "v1_10", feature = "dox"))] cfg_if! {
{ if #[cfg(any(feature = "v1_10", feature = "dox"))] {
let details = match s.details.take() { let details = match s.details.take() {
None => ptr::null_mut(), None => ptr::null_mut(),
Some(details) => details.into_ptr(), Some(details) => details.into_ptr(),
@ -1786,9 +1785,7 @@ impl<'a, T: MessageErrorDomain> WarningBuilder<'a, T> {
s.debug.to_glib_none().0, s.debug.to_glib_none().0,
details, details,
) )
} } else {
#[cfg(not(any(feature = "v1_10", feature = "dox")))]
{
let error = glib::Error::new(s.error, s.message); let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_warning( gst_sys::gst_message_new_warning(
@ -1797,6 +1794,7 @@ impl<'a, T: MessageErrorDomain> WarningBuilder<'a, T> {
s.debug.to_glib_none().0, s.debug.to_glib_none().0,
) )
} }
}
}); });
} }
@ -1838,8 +1836,8 @@ impl<'a, T: MessageErrorDomain> InfoBuilder<'a, T> {
} }
message_builder_generic_impl!(|s: &mut Self, src| { message_builder_generic_impl!(|s: &mut Self, src| {
#[cfg(any(feature = "v1_10", feature = "dox"))] cfg_if! {
{ if #[cfg(any(feature = "v1_10", feature = "dox"))] {
let details = match s.details.take() { let details = match s.details.take() {
None => ptr::null_mut(), None => ptr::null_mut(),
Some(details) => details.into_ptr(), Some(details) => details.into_ptr(),
@ -1853,9 +1851,7 @@ impl<'a, T: MessageErrorDomain> InfoBuilder<'a, T> {
s.debug.to_glib_none().0, s.debug.to_glib_none().0,
details, details,
) )
} } else {
#[cfg(not(any(feature = "v1_10", feature = "dox")))]
{
let error = glib::Error::new(s.error, s.message); let error = glib::Error::new(s.error, s.message);
gst_sys::gst_message_new_info( gst_sys::gst_message_new_info(
@ -1864,6 +1860,7 @@ impl<'a, T: MessageErrorDomain> InfoBuilder<'a, T> {
s.debug.to_glib_none().0, s.debug.to_glib_none().0,
) )
} }
}
}); });
} }

View file

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