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