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