audio,video: Manual enum to_string returns 'static; add NULL check

This commit is contained in:
Marijn Suijten 2020-11-30 20:33:18 +01:00
parent 9ff39bae6f
commit 040772ab61
3 changed files with 26 additions and 13 deletions

View file

@ -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")
}
}

View file

@ -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")
}
}

View file

@ -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")
}
}
}