ffmpeg_the_third/util/dictionary/
owned.rs

1use std::fmt;
2use std::ops::{Deref, DerefMut};
3use std::ptr;
4
5use super::mutable;
6use crate::ffi::*;
7
8pub struct Owned<'a> {
9    inner: mutable::Ref<'a>,
10}
11
12impl<'a> Default for Owned<'a> {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl<'a> Owned<'a> {
19    pub unsafe fn own(ptr: *mut AVDictionary) -> Self {
20        Owned {
21            inner: mutable::Ref::wrap(ptr),
22        }
23    }
24
25    pub unsafe fn disown(mut self) -> *mut AVDictionary {
26        let result = self.inner.as_mut_ptr();
27        self.inner = mutable::Ref::wrap(ptr::null_mut());
28
29        result
30    }
31}
32
33impl<'a> Owned<'a> {
34    pub fn new() -> Self {
35        unsafe {
36            Owned {
37                inner: mutable::Ref::wrap(ptr::null_mut()),
38            }
39        }
40    }
41}
42
43impl<'a, 'b> FromIterator<(&'b str, &'b str)> for Owned<'a> {
44    fn from_iter<T: IntoIterator<Item = (&'b str, &'b str)>>(iterator: T) -> Self {
45        let mut result = Owned::new();
46
47        for (key, value) in iterator {
48            result.set(key, value);
49        }
50
51        result
52    }
53}
54
55impl<'a, 'b> FromIterator<&'b (&'b str, &'b str)> for Owned<'a> {
56    fn from_iter<T: IntoIterator<Item = &'b (&'b str, &'b str)>>(iterator: T) -> Self {
57        let mut result = Owned::new();
58
59        for &(key, value) in iterator {
60            result.set(key, value);
61        }
62
63        result
64    }
65}
66
67impl<'a> FromIterator<(String, String)> for Owned<'a> {
68    fn from_iter<T: IntoIterator<Item = (String, String)>>(iterator: T) -> Self {
69        let mut result = Owned::new();
70
71        for (key, value) in iterator {
72            result.set(&key, &value);
73        }
74
75        result
76    }
77}
78
79impl<'a, 'b> FromIterator<&'b (String, String)> for Owned<'a> {
80    fn from_iter<T: IntoIterator<Item = &'b (String, String)>>(iterator: T) -> Self {
81        let mut result = Owned::new();
82
83        for (key, value) in iterator {
84            result.set(key, value);
85        }
86
87        result
88    }
89}
90
91impl<'a> Deref for Owned<'a> {
92    type Target = mutable::Ref<'a>;
93
94    fn deref(&self) -> &Self::Target {
95        &self.inner
96    }
97}
98
99impl<'a> DerefMut for Owned<'a> {
100    fn deref_mut(&mut self) -> &mut Self::Target {
101        &mut self.inner
102    }
103}
104
105impl<'a> Clone for Owned<'a> {
106    fn clone(&self) -> Self {
107        let mut dictionary = Owned::new();
108        dictionary.clone_from(self);
109
110        dictionary
111    }
112
113    fn clone_from(&mut self, source: &Self) {
114        unsafe {
115            let mut ptr = self.as_mut_ptr();
116            av_dict_copy(&mut ptr, source.as_ptr(), 0);
117            self.inner = mutable::Ref::wrap(ptr);
118        }
119    }
120}
121
122impl<'a> Drop for Owned<'a> {
123    fn drop(&mut self) {
124        unsafe {
125            av_dict_free(&mut self.inner.as_mut_ptr());
126        }
127    }
128}
129
130impl<'a> fmt::Debug for Owned<'a> {
131    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
132        self.inner.fmt(fmt)
133    }
134}