From de10335167cf4beb141bf6c39d23a61e65a75e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 15 Sep 2017 18:35:54 +0300 Subject: [PATCH] Allow creating Error/Warning/Info messages only from specific kinds of glib::Error domains --- gstreamer/src/lib.rs | 2 +- gstreamer/src/message.rs | 75 ++++++++++++++++++++++++++-------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index b68b29ced..1e44eb217 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -64,7 +64,7 @@ pub use log::*; pub mod miniobject; pub use miniobject::{GstRc, MiniObject}; pub mod message; -pub use message::{Message, MessageRef, MessageView}; +pub use message::{Message, MessageErrorDomain, MessageRef, MessageView}; pub mod structure; pub use structure::{Structure, StructureRef}; pub mod caps; diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index 1549edf2a..a08e6d887 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -109,19 +109,19 @@ impl GstRc { EosBuilder::new() } - pub fn new_error(error: &glib::Error) -> ErrorBuilder { + pub fn new_error(error: T, message: &str) -> ErrorBuilder { assert_initialized_main_thread!(); - ErrorBuilder::new(error) + ErrorBuilder::new(error, message) } - pub fn new_warning(error: &glib::Error) -> WarningBuilder { + pub fn new_warning(error: T, message: &str) -> WarningBuilder { assert_initialized_main_thread!(); - WarningBuilder::new(error) + WarningBuilder::new(error, message) } - pub fn new_info(error: &glib::Error) -> InfoBuilder { + pub fn new_info(error: T, message: &str) -> InfoBuilder { assert_initialized_main_thread!(); - InfoBuilder::new(error) + InfoBuilder::new(error, message) } pub fn new_tag(tags: &TagList) -> TagBuilder { @@ -1112,7 +1112,7 @@ impl<'a> Redirect<'a> { macro_rules! message_builder_generic_impl { ($new_fn:expr) => { - pub fn src + Cast + Clone>(self, src: Option<&T>) -> Self { + pub fn src + Cast + Clone>(self, src: Option<&O>) -> Self { Self { src: src.map(|o| { let o = (*o).clone(); @@ -1181,22 +1181,31 @@ impl<'a> EosBuilder<'a> { message_builder_generic_impl!(|_, src| ffi::gst_message_new_eos(src)); } -pub struct ErrorBuilder<'a> { +pub trait MessageErrorDomain: glib::error::ErrorDomain {} + +impl MessageErrorDomain for ::CoreError {} +impl MessageErrorDomain for ::ResourceError {} +impl MessageErrorDomain for ::StreamError {} +impl MessageErrorDomain for ::LibraryError {} + +pub struct ErrorBuilder<'a, T> { src: Option, seqnum: Option, other_fields: Vec<(&'a str, &'a ToValue)>, - error: &'a glib::Error, + error: T, + message: &'a str, debug: Option<&'a str>, #[allow(unused)] details: Option, } -impl<'a> ErrorBuilder<'a> { - fn new(error: &'a glib::Error) -> Self { +impl<'a, T: MessageErrorDomain> ErrorBuilder<'a, T> { + fn new(error: T, message: &'a str) -> Self { skip_assert_initialized!(); Self { src: None, seqnum: None, other_fields: Vec::new(), error: error, + message: message, debug: None, details: None, } @@ -1225,40 +1234,46 @@ impl<'a> ErrorBuilder<'a> { Some(details) => details.into_ptr(), }; + let error = glib::Error::new(s.error, s.message); + ffi::gst_message_new_error_with_details( src, - mut_override(s.error.to_glib_none().0), + mut_override(error.to_glib_none().0), s.debug.to_glib_none().0, details, ) } #[cfg(not(feature = "v1_10"))] { + let error = glib::Error::new(s.error, s.message); + ffi::gst_message_new_error( src, - mut_override(s.error.to_glib_none().0), + mut_override(error.to_glib_none().0), s.debug.to_glib_none().0, ) } }); } -pub struct WarningBuilder<'a> { +pub struct WarningBuilder<'a, T> { src: Option, seqnum: Option, other_fields: Vec<(&'a str, &'a ToValue)>, - error: &'a glib::Error, + error: T, + message: &'a str, debug: Option<&'a str>, #[allow(unused)] details: Option, } -impl<'a> WarningBuilder<'a> { - fn new(error: &'a glib::Error) -> Self { +impl<'a, T: MessageErrorDomain> WarningBuilder<'a, T> { + fn new(error: T, message: &'a str) -> Self { skip_assert_initialized!(); Self { src: None, seqnum: None, other_fields: Vec::new(), error: error, + message: message, debug: None, details: None, } @@ -1287,40 +1302,46 @@ impl<'a> WarningBuilder<'a> { Some(details) => details.into_ptr(), }; + let error = glib::Error::new(s.error, s.message); + ffi::gst_message_new_warning_with_details( src, - mut_override(s.error.to_glib_none().0), + mut_override(error.to_glib_none().0), s.debug.to_glib_none().0, details, ) } #[cfg(not(feature = "v1_10"))] { + let error = glib::Error::new(s.error, s.message); + ffi::gst_message_new_warning( src, - mut_override(s.error.to_glib_none().0), + mut_override(error.to_glib_none().0), s.debug.to_glib_none().0, ) } }); } -pub struct InfoBuilder<'a> { +pub struct InfoBuilder<'a, T> { src: Option, seqnum: Option, other_fields: Vec<(&'a str, &'a ToValue)>, - error: &'a glib::Error, + error: T, + message: &'a str, debug: Option<&'a str>, #[allow(unused)] details: Option, } -impl<'a> InfoBuilder<'a> { - fn new(error: &'a glib::Error) -> Self { +impl<'a, T: MessageErrorDomain> InfoBuilder<'a, T> { + fn new(error: T, message: &'a str) -> Self { skip_assert_initialized!(); Self { src: None, seqnum: None, other_fields: Vec::new(), error: error, + message: message, debug: None, details: None, } @@ -1349,18 +1370,22 @@ impl<'a> InfoBuilder<'a> { Some(details) => details.into_ptr(), }; + let error = glib::Error::new(s.error, s.message); + ffi::gst_message_new_info_with_details( src, - mut_override(s.error.to_glib_none().0), + mut_override(error.to_glib_none().0), s.debug.to_glib_none().0, details, ) } #[cfg(not(feature = "v1_10"))] { + let error = glib::Error::new(s.error, s.message); + ffi::gst_message_new_info( src, - mut_override(s.error.to_glib_none().0), + mut_override(error.to_glib_none().0), s.debug.to_glib_none().0, ) }