From 07ce2d64b8c5f606a3bd95d2a3943931d14cf41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 20 Dec 2017 20:13:31 +0200 Subject: [PATCH] Use the Into trait instead of custom into_*() functions --- gst-plugin-simple/src/demuxer.rs | 4 ++-- gst-plugin-simple/src/sink.rs | 8 ++++---- gst-plugin-simple/src/source.rs | 8 ++++---- gst-plugin/src/error.rs | 26 ++++++++++++++++++++------ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/gst-plugin-simple/src/demuxer.rs b/gst-plugin-simple/src/demuxer.rs index b4e6d409..4ae8c5aa 100644 --- a/gst-plugin-simple/src/demuxer.rs +++ b/gst-plugin-simple/src/demuxer.rs @@ -405,7 +405,7 @@ impl Demuxer { } _ => (), } - return flow_error.to_native(); + return flow_error.into(); } } }; @@ -465,7 +465,7 @@ impl Demuxer { } _ => (), } - return flow_error.to_native(); + return flow_error.into(); } } } diff --git a/gst-plugin-simple/src/sink.rs b/gst-plugin-simple/src/sink.rs index 8cbc7335..ceabefd6 100644 --- a/gst-plugin-simple/src/sink.rs +++ b/gst-plugin-simple/src/sink.rs @@ -109,7 +109,7 @@ impl Sink { if uri_storage.1 { return Err( - UriError::new(gst::URIError::BadState, "Already started".to_string()).into_error(), + UriError::new(gst::URIError::BadState, "Already started".to_string()).into(), ); } @@ -118,14 +118,14 @@ impl Sink { if let Some(uri_str) = uri_str { match Url::parse(uri_str.as_str()) { Ok(uri) => { - try!((self.uri_validator)(&uri).map_err(|e| e.into_error())); + try!((self.uri_validator)(&uri).map_err(|e| e.into())); uri_storage.0 = Some(uri); Ok(()) } Err(err) => Err(UriError::new( gst::URIError::BadUri, format!("Failed to parse URI '{}': {}", uri_str, err), - ).into_error()), + ).into()), } } else { Ok(()) @@ -228,7 +228,7 @@ impl BaseSinkImpl for Sink { } _ => (), } - flow_error.to_native() + flow_error.into() } } } diff --git a/gst-plugin-simple/src/source.rs b/gst-plugin-simple/src/source.rs index 20124228..fa622941 100644 --- a/gst-plugin-simple/src/source.rs +++ b/gst-plugin-simple/src/source.rs @@ -128,7 +128,7 @@ impl Source { if uri_storage.1 { return Err( - UriError::new(gst::URIError::BadState, "Already started".to_string()).into_error(), + UriError::new(gst::URIError::BadState, "Already started".to_string()).into(), ); } @@ -137,14 +137,14 @@ impl Source { if let Some(uri_str) = uri_str { match Url::parse(uri_str.as_str()) { Ok(uri) => { - try!((self.uri_validator)(&uri).map_err(|e| e.into_error())); + try!((self.uri_validator)(&uri).map_err(|e| e.into())); uri_storage.0 = Some(uri); Ok(()) } Err(err) => Err(UriError::new( gst::URIError::BadUri, format!("Failed to parse URI '{}': {}", uri_str, err), - ).into_error()), + ).into()), } } else { Ok(()) @@ -275,7 +275,7 @@ impl BaseSrcImpl for Source { } _ => (), } - flow_error.to_native() + flow_error.into() } } } diff --git a/gst-plugin/src/error.rs b/gst-plugin/src/error.rs index d6529916..5bdb9a64 100644 --- a/gst-plugin/src/error.rs +++ b/gst-plugin/src/error.rs @@ -18,7 +18,7 @@ use glib::translate::ToGlibPtr; use gst; use gst::prelude::*; -#[derive(Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum FlowError { Flushing, Eos, @@ -26,8 +26,14 @@ pub enum FlowError { Error(gst::ErrorMessage), } -impl FlowError { - pub fn to_native(&self) -> gst::FlowReturn { +impl Into for FlowError { + fn into(self) -> gst::FlowReturn { + (&self).into() + } +} + +impl<'a> Into for &'a FlowError { + fn into(self) -> gst::FlowReturn { match *self { FlowError::Flushing => gst::FlowReturn::Flushing, FlowError::Eos => gst::FlowReturn::Eos, @@ -67,10 +73,10 @@ pub struct UriError { } impl UriError { - pub fn new(error: gst::URIError, message: String) -> UriError { + pub fn new>(error: gst::URIError, message: T) -> UriError { UriError { error: error, - message: message, + message: message.into(), } } @@ -81,8 +87,16 @@ impl UriError { pub fn error(&self) -> gst::URIError { self.error } +} - pub fn into_error(self) -> glib::Error { +impl Into for UriError { + fn into(self) -> glib::Error { + (&self).into() + } +} + +impl<'a> Into for &'a UriError { + fn into(self) -> glib::Error { glib::Error::new(self.error, &self.message) } }