From 9a628848cfe3456051fff8b0b2cd749b58236e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Tue, 10 Sep 2024 21:23:26 +0200 Subject: [PATCH] tags: fix index() lifetime bind The signature for `TagListRef::index` didn't bind the lifetime of the returned `TagValue` to `&self`. This causes the following code to compile: ```rust 1 let title = { 2 let mut tags = TagList::new(); 3 { 4 let tags = tags.get_mut().unwrap(); 5 tags.add::(&"some title", TagMergeMode::Append); 6 } 7 8 let title = tags.index::<Title>(0).unwrap(); 9 assert_eq!(title.get(), "some title"); 10 11 title 12 }; 13 14 assert_eq!(title.get(), "some title"); ``` ... but it panics at runtime on the last `title.get()`: ``` Invalid tag type: WrongValueType(ValueTypeMismatchError { actual: <invalid>, requested: gchararray }) ``` Indeed, the `title` `TagValue` is freed with the `tags` on line 12. This commit fixes the function signature so the returned `TagValue` can't outlive its `TagListRef`. Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1520> --- gstreamer/src/tags.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gstreamer/src/tags.rs b/gstreamer/src/tags.rs index 6a46c63b3..bf2310741 100644 --- a/gstreamer/src/tags.rs +++ b/gstreamer/src/tags.rs @@ -483,7 +483,7 @@ impl TagListRef { #[doc(alias = "get_index")] #[doc(alias = "gst_tag_list_get_index")] - pub fn index<'a, T: Tag<'a>>(&self, idx: usize) -> Option<&'a TagValue<T::TagType>> { + pub fn index<'a, T: Tag<'a>>(&'a self, idx: usize) -> Option<&'a TagValue<T::TagType>> { self.index_generic(T::TAG_NAME, idx).map(|value| { if !value.is::<T::TagType>() { panic!(