Skip to main content

ffmpeg_the_third/codec/subtitle/
rect_mut.rs

1use libc::c_int;
2use std::ffi::CString;
3use std::marker::PhantomData;
4use std::ptr::NonNull;
5
6use super::{Flags, RectRef, Subtitle};
7use crate::ffi::*;
8use crate::{AsMutPtr, AsPtr};
9
10pub struct RectMut<'s> {
11    ptr: NonNull<AVSubtitleRect>,
12    _marker: PhantomData<&'s mut Subtitle>,
13}
14
15impl<'s> RectMut<'s> {
16    /// # Safety
17    /// `ptr` must be a valid pointer to an [`AVSubtitleRect`].
18    /// Ensure that the returned lifetime is correctly bounded.
19    pub unsafe fn from_ptr(ptr: NonNull<AVSubtitleRect>) -> Self {
20        Self {
21            ptr,
22            _marker: PhantomData,
23        }
24    }
25
26    pub fn as_ref(&self) -> RectRef<'s> {
27        unsafe { RectRef::from_ptr(self.ptr) }
28    }
29
30    pub fn set_flags(&mut self, flags: Flags) {
31        unsafe {
32            (*self.as_mut_ptr()).flags = flags.bits();
33        }
34    }
35
36    pub fn set_x(&mut self, value: usize) {
37        unsafe {
38            (*self.as_mut_ptr()).x = value as c_int;
39        }
40    }
41
42    pub fn set_y(&mut self, value: usize) {
43        unsafe {
44            (*self.as_mut_ptr()).y = value as c_int;
45        }
46    }
47
48    pub fn set_width(&mut self, value: u32) {
49        unsafe {
50            (*self.as_mut_ptr()).w = value as c_int;
51        }
52    }
53
54    pub fn set_height(&mut self, value: u32) {
55        unsafe {
56            (*self.as_mut_ptr()).h = value as c_int;
57        }
58    }
59
60    pub fn set_colors(&mut self, value: usize) {
61        unsafe {
62            (*self.as_mut_ptr()).nb_colors = value as c_int;
63        }
64    }
65
66    pub fn set_text(&mut self, value: &str) {
67        let value = CString::new(value).unwrap();
68
69        unsafe {
70            (*self.as_mut_ptr()).text = av_strdup(value.as_ptr());
71        }
72    }
73
74    pub fn set_ass(&mut self, value: &str) {
75        let value = CString::new(value).unwrap();
76
77        unsafe {
78            (*self.as_mut_ptr()).ass = av_strdup(value.as_ptr());
79        }
80    }
81}
82
83impl<'s> AsPtr<AVSubtitleRect> for RectMut<'s> {
84    fn as_ptr(&self) -> *const AVSubtitleRect {
85        self.ptr.as_ptr()
86    }
87}
88
89impl<'s> AsMutPtr<AVSubtitleRect> for RectMut<'s> {
90    fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
91        self.ptr.as_ptr()
92    }
93}