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