From db30c121a0f879fc9bb5019d64e9a60aa68a398d Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Tue, 10 Aug 2021 17:38:55 +0300 Subject: [PATCH] const ElementMetadata constructor The usual use of this will be through the `ElementImpl::metadata` method, which requires a `&'static` reference to `ElementMetadata` to be returned, so we better make it easy to construct these (without forcing people to resort to `Lazy`'n'stuff) --- gstreamer/src/subclass/element.rs | 49 +++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/gstreamer/src/subclass/element.rs b/gstreamer/src/subclass/element.rs index 44f79007b..f8cfb1863 100644 --- a/gstreamer/src/subclass/element.rs +++ b/gstreamer/src/subclass/element.rs @@ -13,25 +13,26 @@ use crate::StateChangeError; use crate::StateChangeReturn; use crate::StateChangeSuccess; +use std::borrow::Cow; use std::sync::atomic; #[derive(Debug, Clone)] pub struct ElementMetadata { - long_name: String, - classification: String, - description: String, - author: String, - additional: Vec<(String, String)>, + long_name: Cow<'static, str>, + classification: Cow<'static, str>, + description: Cow<'static, str>, + author: Cow<'static, str>, + additional: Cow<'static, [(Cow<'static, str>, Cow<'static, str>)]>, } impl ElementMetadata { pub fn new(long_name: &str, classification: &str, description: &str, author: &str) -> Self { Self { - long_name: long_name.into(), - classification: classification.into(), - description: description.into(), - author: author.into(), - additional: vec![], + long_name: Cow::Owned(long_name.into()), + classification: Cow::Owned(classification.into()), + description: Cow::Owned(description.into()), + author: Cow::Owned(author.into()), + additional: Cow::Borrowed(&[]), } } @@ -43,17 +44,33 @@ impl ElementMetadata { additional: &[(&str, &str)], ) -> Self { Self { - long_name: long_name.into(), - classification: classification.into(), - description: description.into(), - author: author.into(), + long_name: Cow::Owned(long_name.into()), + classification: Cow::Owned(classification.into()), + description: Cow::Owned(description.into()), + author: Cow::Owned(author.into()), additional: additional .iter() .copied() - .map(|(key, value)| (key.into(), value.into())) + .map(|(key, value)| (Cow::Owned(key.into()), Cow::Owned(value.into()))) .collect(), } } + + pub const fn with_cow( + long_name: Cow<'static, str>, + classification: Cow<'static, str>, + description: Cow<'static, str>, + author: Cow<'static, str>, + additional: Cow<'static, [(Cow<'static, str>, Cow<'static, str>)]>, + ) -> Self { + Self { + long_name, + classification, + description, + author, + additional, + } + } } pub trait ElementImpl: ElementImplExt + ObjectImpl + Send + Sync { @@ -384,7 +401,7 @@ unsafe impl IsSubclassable for Element { metadata.author.to_glib_none().0, ); - for (key, value) in &metadata.additional { + for (key, value) in &metadata.additional[..] { ffi::gst_element_class_add_metadata( klass, key.to_glib_none().0,