From e88994a0b753d2dc192c19d2504c24f3880c4386 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Thu, 19 Nov 2020 11:26:34 +0100 Subject: [PATCH] use cfg_if to refactor some #[cfg()] / #[cfg(not())] pairs --- gstreamer-video/Cargo.toml | 1 + gstreamer-video/src/lib.rs | 3 + gstreamer-video/src/video_info.rs | 249 +++++++++++++++--------------- gstreamer/src/lib.rs | 2 - gstreamer/src/message.rs | 129 ++++++++-------- gstreamer/src/subclass/clock.rs | 17 +- 6 files changed, 202 insertions(+), 199 deletions(-) diff --git a/gstreamer-video/Cargo.toml b/gstreamer-video/Cargo.toml index a829255e4..933aa8d89 100644 --- a/gstreamer-video/Cargo.toml +++ b/gstreamer-video/Cargo.toml @@ -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"] } diff --git a/gstreamer-video/src/lib.rs b/gstreamer-video/src/lib.rs index c34213bc9..abce52e3e 100644 --- a/gstreamer-video/src/lib.rs +++ b/gstreamer-video/src/lib.rs @@ -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; diff --git a/gstreamer-video/src/video_info.rs b/gstreamer-video/src/video_info.rs index 11bacd432..81ae322d9 100644 --- a/gstreamer-video/src/video_info.rs +++ b/gstreamer-video/src/video_info.rs @@ -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, - )) - } } } diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index b4c0b8a0b..4c7847937 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -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; diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index 1d677cc65..16e762e21 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -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, + ) + } } }); } diff --git a/gstreamer/src/subclass/clock.rs b/gstreamer/src/subclass/clock.rs index cb01d1abc..d25e98649 100644 --- a/gstreamer/src/subclass/clock.rs +++ b/gstreamer/src/subclass/clock.rs @@ -211,15 +211,14 @@ impl 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); + } } }