From 040772ab61216e1b1324b67a88f223eee89a9a4f Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 30 Nov 2020 20:33:18 +0100 Subject: [PATCH] audio,video: Manual enum to_string returns 'static; add NULL check --- gstreamer-audio/src/audio_format.rs | 13 ++++++++----- gstreamer-video/src/video_format.rs | 13 ++++++++----- gstreamer-video/src/video_info.rs | 13 ++++++++++--- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/gstreamer-audio/src/audio_format.rs b/gstreamer-audio/src/audio_format.rs index 30642c5a8..0030e8730 100644 --- a/gstreamer-audio/src/audio_format.rs +++ b/gstreamer-audio/src/audio_format.rs @@ -118,14 +118,17 @@ impl crate::AudioFormat { } pub fn to_str<'a>(self) -> &'a str { - if self == crate::AudioFormat::Unknown { + if self == Self::Unknown { return "UNKNOWN"; } - unsafe { - CStr::from_ptr(ffi::gst_audio_format_to_string(self.to_glib())) - .to_str() - .unwrap() + CStr::from_ptr( + ffi::gst_audio_format_to_string(self.to_glib()) + .as_ref() + .expect("gst_audio_format_to_string returned NULL"), + ) + .to_str() + .expect("gst_audio_format_to_string returned an invalid string") } } diff --git a/gstreamer-video/src/video_format.rs b/gstreamer-video/src/video_format.rs index 82cf399a0..ff506d296 100644 --- a/gstreamer-video/src/video_format.rs +++ b/gstreamer-video/src/video_format.rs @@ -313,14 +313,17 @@ impl crate::VideoFormat { } pub fn to_str<'a>(self) -> &'a str { - if self == crate::VideoFormat::Unknown { + if self == Self::Unknown { return "UNKNOWN"; } - unsafe { - CStr::from_ptr(ffi::gst_video_format_to_string(self.to_glib())) - .to_str() - .unwrap() + CStr::from_ptr( + ffi::gst_video_format_to_string(self.to_glib()) + .as_ref() + .expect("gst_video_format_to_string returned NULL"), + ) + .to_str() + .expect("gst_video_format_to_string returned an invalid string") } } diff --git a/gstreamer-video/src/video_info.rs b/gstreamer-video/src/video_info.rs index d90257104..02a3e66c1 100644 --- a/gstreamer-video/src/video_info.rs +++ b/gstreamer-video/src/video_info.rs @@ -935,10 +935,17 @@ impl glib::translate::FromGlibPtrFull<*mut ffi::GstVideoInfo> for VideoInfo { #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_12")))] impl crate::VideoFieldOrder { pub fn to_str<'a>(self) -> &'a str { + if self == Self::Unknown { + return "UNKNOWN"; + } unsafe { - CStr::from_ptr(ffi::gst_video_field_order_to_string(self.to_glib())) - .to_str() - .unwrap() + CStr::from_ptr( + ffi::gst_video_field_order_to_string(self.to_glib()) + .as_ref() + .expect("gst_video_field_order_to_string returned NULL"), + ) + .to_str() + .expect("gst_video_field_order_to_string returned an invalid string") } } }