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