ffmpeg_rs/util/dictionary/
owned.rs

1use std::fmt;
2use std::iter::FromIterator;
3use std::ops::{Deref, DerefMut};
4use std::ptr;
5
6use super::mutable;
7use 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        Owned {
22            inner: mutable::Ref::wrap(ptr),
23        }
24    }
25
26    pub unsafe fn disown(mut self) -> *mut AVDictionary {
27        let result = self.inner.as_mut_ptr();
28        self.inner = mutable::Ref::wrap(ptr::null_mut());
29
30        result
31    }
32}
33
34impl<'a> Owned<'a> {
35    pub fn new() -> Self {
36        unsafe {
37            Owned {
38                inner: mutable::Ref::wrap(ptr::null_mut()),
39            }
40        }
41    }
42}
43
44impl<'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::new();
47
48        for (key, value) in iterator {
49            result.set(key, value);
50        }
51
52        result
53    }
54}
55
56impl<'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::new();
59
60        for &(key, value) in iterator {
61            result.set(key, value);
62        }
63
64        result
65    }
66}
67
68impl<'a> FromIterator<(String, String)> for Owned<'a> {
69    fn from_iter<T: IntoIterator<Item = (String, String)>>(iterator: T) -> Self {
70        let mut result = Owned::new();
71
72        for (key, value) in iterator {
73            result.set(&key, &value);
74        }
75
76        result
77    }
78}
79
80impl<'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::new();
83
84        for &(ref key, ref value) in iterator {
85            result.set(key, value);
86        }
87
88        result
89    }
90}
91
92impl<'a> Deref for Owned<'a> {
93    type Target = mutable::Ref<'a>;
94
95    fn deref(&self) -> &Self::Target {
96        &self.inner
97    }
98}
99
100impl<'a> DerefMut for Owned<'a> {
101    fn deref_mut(&mut self) -> &mut Self::Target {
102        &mut self.inner
103    }
104}
105
106impl<'a> Clone for Owned<'a> {
107    fn clone(&self) -> Self {
108        let mut dictionary = 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 = 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
123impl<'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
131impl<'a> fmt::Debug for Owned<'a> {
132    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
133        self.inner.fmt(fmt)
134    }
135}