1 | use std::ffi::CString; |
2 | use std::fmt; |
3 | use std::marker::PhantomData; |
4 | use std::ops::Deref; |
5 | |
6 | use super::immutable; |
7 | use ffi::*; |
8 | |
9 | pub struct Ref<'a> { |
10 | ptr: *mut AVDictionary, |
11 | imm: immutable::Ref<'a>, |
12 | |
13 | _marker: PhantomData<&'a ()>, |
14 | } |
15 | |
16 | impl<'a> Ref<'a> { |
17 | pub unsafe fn wrap(ptr: *mut AVDictionary) -> Self { |
18 | Ref { |
19 | ptr, |
20 | imm: immutable::Ref::wrap(ptr), |
21 | _marker: PhantomData, |
22 | } |
23 | } |
24 | |
25 | pub unsafe fn as_mut_ptr(&self) -> *mut AVDictionary { |
26 | self.ptr |
27 | } |
28 | } |
29 | |
30 | impl<'a> Ref<'a> { |
31 | pub fn set(&mut self, key: &str, value: &str) { |
32 | unsafe { |
33 | let key: CString = CString::new(key).unwrap(); |
34 | let value: CString = CString::new(value).unwrap(); |
35 | let mut ptr: *mut {unknown} = self.as_mut_ptr(); |
36 | |
37 | if av_dict_set(&mut ptr, key.as_ptr(), value.as_ptr(), 0) < 0 { |
38 | panic!("out of memory" ); |
39 | } |
40 | |
41 | self.ptr = ptr; |
42 | self.imm = immutable::Ref::wrap(ptr); |
43 | } |
44 | } |
45 | } |
46 | |
47 | impl<'a> Deref for Ref<'a> { |
48 | type Target = immutable::Ref<'a>; |
49 | |
50 | fn deref(&self) -> &Self::Target { |
51 | &self.imm |
52 | } |
53 | } |
54 | |
55 | impl<'a> fmt::Debug for Ref<'a> { |
56 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
57 | self.imm.fmt(fmt) |
58 | } |
59 | } |
60 | |