From 9440621ff839f073e5ffb8b59b60cbf1011402d5 Mon Sep 17 00:00:00 2001 From: meh Date: Sat, 20 Mar 2021 22:51:14 +0100 Subject: [PATCH] refactor: remove the lifetime from an owned dictionary --- examples/metadata.rs | 16 +++-- src/codec/decoder/decoder.rs | 2 +- src/codec/encoder/audio.rs | 4 +- src/codec/encoder/subtitle.rs | 2 +- src/codec/encoder/video.rs | 4 +- src/format/chapter/chapter_mut.rs | 4 +- src/format/context/output.rs | 4 +- src/format/mod.rs | 8 +-- src/format/stream/stream_mut.rs | 2 +- src/util/dictionary/immutable.rs | 2 +- src/util/dictionary/owned.rs | 116 ++++++++++++++---------------- src/util/frame/mod.rs | 2 +- 12 files changed, 81 insertions(+), 85 deletions(-) diff --git a/examples/metadata.rs b/examples/metadata.rs index c937770..daa33f7 100644 --- a/examples/metadata.rs +++ b/examples/metadata.rs @@ -24,18 +24,22 @@ fn main() { } println!( - "duration (seconds): {:.2}", - context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE) + "duration (seconds): {:?}", + context + .duration() + .map(|d| d as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)) ); for stream in context.streams() { println!("stream index {}:", stream.index()); println!("\ttime_base: {}", stream.time_base()); - println!("\tstart_time: {}", stream.start_time()); - println!("\tduration (stream timebase): {}", stream.duration()); + println!("\tstart_time: {:?}", stream.start_time()); + println!("\tduration (stream timebase): {:?}", stream.duration()); println!( - "\tduration (seconds): {:.2}", - stream.duration() as f64 * f64::from(stream.time_base()) + "\tduration (seconds): {:?}", + stream + .duration() + .map(|d| d as f64 * f64::from(stream.time_base())) ); println!("\tframes: {}", stream.frames()); println!("\tdisposition: {:?}", stream.disposition()); diff --git a/src/codec/decoder/decoder.rs b/src/codec/decoder/decoder.rs index 39f25b3..cb26bc3 100644 --- a/src/codec/decoder/decoder.rs +++ b/src/codec/decoder/decoder.rs @@ -39,7 +39,7 @@ impl Decoder { pub fn open_as_with( mut self, codec: D, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { if let Some(codec) = codec.decoder() { diff --git a/src/codec/encoder/audio.rs b/src/codec/encoder/audio.rs index ba18f85..d5a188f 100644 --- a/src/codec/encoder/audio.rs +++ b/src/codec/encoder/audio.rs @@ -40,7 +40,7 @@ impl Audio { } } - pub fn open_with(mut self, options: Dictionary<'_>) -> Result { + pub fn open_with(mut self, options: Dictionary) -> Result { unsafe { let mut opts = options.disown(); let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts); @@ -57,7 +57,7 @@ impl Audio { pub fn open_as_with( mut self, codec: E, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { if let Some(codec) = codec.encoder() { diff --git a/src/codec/encoder/subtitle.rs b/src/codec/encoder/subtitle.rs index 9c02a93..592c11f 100644 --- a/src/codec/encoder/subtitle.rs +++ b/src/codec/encoder/subtitle.rs @@ -41,7 +41,7 @@ impl Subtitle { pub fn open_as_with( mut self, codec: E, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { if let Some(codec) = codec.encoder() { diff --git a/src/codec/encoder/video.rs b/src/codec/encoder/video.rs index c3e0ba2..e0a4358 100644 --- a/src/codec/encoder/video.rs +++ b/src/codec/encoder/video.rs @@ -42,7 +42,7 @@ impl Video { } #[inline] - pub fn open_with(mut self, options: Dictionary<'_>) -> Result { + pub fn open_with(mut self, options: Dictionary) -> Result { unsafe { let mut opts = options.disown(); let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts); @@ -60,7 +60,7 @@ impl Video { pub fn open_as_with( mut self, codec: E, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { if let Some(codec) = codec.encoder() { diff --git a/src/format/chapter/chapter_mut.rs b/src/format/chapter/chapter_mut.rs index a055baa..c71296c 100644 --- a/src/format/chapter/chapter_mut.rs +++ b/src/format/chapter/chapter_mut.rs @@ -56,8 +56,8 @@ impl<'a> ChapterMut<'a> { // dictionary.set() allocates the AVDictionary the first time a key/value is // inserted so we want to update the metadata dictionary afterwards unsafe { - let mut dictionary = Dictionary::own(self.metadata().as_mut_ptr()); - dictionary.set(key.as_ref(), value.as_ref()); + let dictionary = Dictionary::own(self.metadata().as_mut_ptr()); + dictionary.as_mut().set(key.as_ref(), value.as_ref()); (*self.as_mut_ptr()).metadata = dictionary.disown(); } } diff --git a/src/format/context/output.rs b/src/format/context/output.rs index 3da8386..807dda2 100644 --- a/src/format/context/output.rs +++ b/src/format/context/output.rs @@ -63,7 +63,7 @@ impl Output { } } - pub fn write_header_with(&mut self, options: Dictionary<'_>) -> Result, Error> { + pub fn write_header_with(&mut self, options: Dictionary) -> Result { unsafe { let mut opts = options.disown(); let res = avformat_write_header(self.as_mut_ptr(), &mut opts); @@ -161,7 +161,7 @@ impl Output { Ok(chapter) } - pub fn set_metadata(&mut self, dictionary: Dictionary<'_>) { + pub fn set_metadata(&mut self, dictionary: Dictionary) { unsafe { (*self.as_mut_ptr()).metadata = dictionary.disown(); } diff --git a/src/format/mod.rs b/src/format/mod.rs index 8f03ef1..b10041c 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -92,7 +92,7 @@ pub fn open(path_or_url: impl AsRef, format: &Format) -> Result, format: &Format, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { let mut ps = ptr::null_mut(); @@ -153,7 +153,7 @@ pub fn input(path_or_url: impl AsRef) -> Result { pub fn input_with_dictionary( path_or_url: impl AsRef, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { let mut ps = ptr::null_mut(); @@ -218,7 +218,7 @@ pub fn output(path_or_url: impl AsRef) -> Result pub fn output_with( path_or_url: impl AsRef, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { let mut ps = ptr::null_mut(); @@ -275,7 +275,7 @@ pub fn output_as( pub fn output_as_with( path_or_url: impl AsRef, mut format: format::Output, - options: Dictionary<'_>, + options: Dictionary, ) -> Result { unsafe { let mut ps = ptr::null_mut(); diff --git a/src/format/stream/stream_mut.rs b/src/format/stream/stream_mut.rs index ebf0f61..d437ca3 100644 --- a/src/format/stream/stream_mut.rs +++ b/src/format/stream/stream_mut.rs @@ -59,7 +59,7 @@ impl<'a> StreamMut<'a> { } } - pub fn set_metadata(&mut self, metadata: Dictionary<'_>) { + pub fn set_metadata(&mut self, metadata: Dictionary) { unsafe { let metadata = metadata.disown(); (*self.as_mut_ptr()).metadata = metadata; diff --git a/src/util/dictionary/immutable.rs b/src/util/dictionary/immutable.rs index 305bf67..87e8550 100644 --- a/src/util/dictionary/immutable.rs +++ b/src/util/dictionary/immutable.rs @@ -49,7 +49,7 @@ impl<'a> Ref<'a> { unsafe { Iter::new(self.as_ptr()) } } - pub fn to_owned<'b>(&self) -> Owned<'b> { + pub fn to_owned(&self) -> Owned { self.iter().collect() } } diff --git a/src/util/dictionary/owned.rs b/src/util/dictionary/owned.rs index 7c7222b..d15eaa8 100644 --- a/src/util/dictionary/owned.rs +++ b/src/util/dictionary/owned.rs @@ -1,111 +1,103 @@ -use std::{ - fmt, - iter::FromIterator, - ops::{Deref, DerefMut}, - ptr, -}; +use std::{fmt, iter::FromIterator, ptr}; -use super::mutable; +use super::{immutable, mutable}; use crate::ffi::*; -pub struct Owned<'a> { - inner: mutable::Ref<'a>, +pub struct Owned { + ptr: *mut AVDictionary, } -impl<'a> Default for Owned<'a> { +impl Default for Owned { fn default() -> Self { Self::new() } } -impl<'a> Owned<'a> { +impl Owned { pub unsafe fn own(ptr: *mut AVDictionary) -> Self { - Owned { - inner: mutable::Ref::wrap(ptr), - } + Owned { ptr } } pub unsafe fn disown(mut self) -> *mut AVDictionary { - let result = self.inner.as_mut_ptr(); - self.inner = mutable::Ref::wrap(ptr::null_mut()); - + let result = self.ptr; + self.ptr = ptr::null_mut(); result } -} -impl<'a> Owned<'a> { - pub fn new() -> Self { - unsafe { - Owned { - inner: mutable::Ref::wrap(ptr::null_mut()), - } - } + pub fn as_ptr(&self) -> *const AVDictionary { + self.ptr + } + + pub fn as_mut_ptr(&mut self) -> *mut AVDictionary { + self.ptr } } -impl<'a, 'b> FromIterator<(&'b str, &'b str)> for Owned<'a> { - fn from_iter>(iterator: T) -> Self { - let mut result = Owned::new(); +impl Owned { + pub fn new() -> Self { + Owned { + ptr: ptr::null_mut(), + } + } + + pub fn as_ref(&self) -> immutable::Ref { + unsafe { immutable::Ref::wrap(self.ptr) } + } + + pub fn as_mut(&self) -> mutable::Ref { + unsafe { mutable::Ref::wrap(self.ptr) } + } +} + +impl<'a> FromIterator<(&'a str, &'a str)> for Owned { + fn from_iter>(iterator: T) -> Self { + let result = Owned::new(); for (key, value) in iterator { - result.set(key, value); + result.as_mut().set(key, value); } result } } -impl<'a, 'b> FromIterator<&'b (&'b str, &'b str)> for Owned<'a> { - fn from_iter>(iterator: T) -> Self { - let mut result = Owned::new(); +impl<'a> FromIterator<&'a (&'a str, &'a str)> for Owned { + fn from_iter>(iterator: T) -> Self { + let result = Owned::new(); for &(key, value) in iterator { - result.set(key, value); + result.as_mut().set(key, value); } result } } -impl<'a> FromIterator<(String, String)> for Owned<'a> { +impl FromIterator<(String, String)> for Owned { fn from_iter>(iterator: T) -> Self { - let mut result = Owned::new(); + let result = Owned::new(); for (key, value) in iterator { - result.set(&key, &value); + result.as_mut().set(&key, &value); } result } } -impl<'a, 'b> FromIterator<&'b (String, String)> for Owned<'a> { - fn from_iter>(iterator: T) -> Self { - let mut result = Owned::new(); +impl<'a> FromIterator<&'a (String, String)> for Owned { + fn from_iter>(iterator: T) -> Self { + let result = Owned::new(); for &(ref key, ref value) in iterator { - result.set(key, value); + result.as_mut().set(key, value); } result } } -impl<'a> Deref for Owned<'a> { - type Target = mutable::Ref<'a>; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -impl<'a> DerefMut for Owned<'a> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } -} - -impl<'a> Clone for Owned<'a> { +impl Clone for Owned { fn clone(&self) -> Self { let mut dictionary = Owned::new(); dictionary.clone_from(self); @@ -115,23 +107,23 @@ impl<'a> Clone for Owned<'a> { fn clone_from(&mut self, source: &Self) { unsafe { - let mut ptr = self.as_mut_ptr(); - av_dict_copy(&mut ptr, source.as_ptr(), 0); - self.inner = mutable::Ref::wrap(ptr); + av_dict_copy(&mut self.ptr, source.as_ptr(), 0); } } } -impl<'a> Drop for Owned<'a> { +impl Drop for Owned { fn drop(&mut self) { unsafe { - av_dict_free(&mut self.inner.as_mut_ptr()); + if !self.ptr.is_null() { + av_dict_free(&mut self.ptr); + } } } } -impl<'a> fmt::Debug for Owned<'a> { +impl fmt::Debug for Owned { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - self.inner.fmt(fmt) + unsafe { mutable::Ref::wrap(self.ptr) }.fmt(fmt) } } diff --git a/src/util/frame/mod.rs b/src/util/frame/mod.rs index ba1ad3f..52a3ff2 100644 --- a/src/util/frame/mod.rs +++ b/src/util/frame/mod.rs @@ -144,7 +144,7 @@ impl Frame { } #[inline] - pub fn set_metadata(&mut self, value: Dictionary<'_>) { + pub fn set_metadata(&mut self, value: Dictionary) { unsafe { av_frame_set_metadata(self.as_mut_ptr(), value.disown()); }