scuffle_ffmpeg/
consts.rs

1pub(crate) const DEFAULT_BUFFER_SIZE: usize = 4096;
2
3/// Const is an owned value which is immutable, but also has a lifetime.
4/// This value exists because ffmpeg often has values that we 'own' but is linked
5/// to some lifetime.
6pub struct Const<'a, T>(pub(crate) T, pub(crate) std::marker::PhantomData<&'a ()>);
7
8impl<T: std::fmt::Debug> std::fmt::Debug for Const<'_, T> {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        self.0.fmt(f)
11    }
12}
13
14impl<T> Const<'_, T> {
15    pub(crate) const fn new(value: T) -> Self {
16        Self(value, std::marker::PhantomData)
17    }
18}
19
20impl<T> std::ops::Deref for Const<'_, T> {
21    type Target = T;
22
23    fn deref(&self) -> &Self::Target {
24        &self.0
25    }
26}
27
28/// Mut is an owned value which is mutable, but also has a lifetime.
29/// This value exists because ffmpeg often has values that we 'own' but is linked
30/// to some lifetime.
31pub struct Mut<'a, T>(pub(crate) T, pub(crate) std::marker::PhantomData<&'a ()>);
32
33impl<T: std::fmt::Debug> std::fmt::Debug for Mut<'_, T> {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        self.0.fmt(f)
36    }
37}
38
39impl<T> Mut<'_, T> {
40    pub(crate) const fn new(value: T) -> Self {
41        Self(value, std::marker::PhantomData)
42    }
43}
44
45impl<T> std::ops::Deref for Mut<'_, T> {
46    type Target = T;
47
48    fn deref(&self) -> &Self::Target {
49        &self.0
50    }
51}
52
53impl<T> std::ops::DerefMut for Mut<'_, T> {
54    fn deref_mut(&mut self) -> &mut Self::Target {
55        &mut self.0
56    }
57}
58
59#[cfg(test)]
60#[cfg_attr(all(test, coverage_nightly), coverage(off))]
61mod tests {
62    use crate::consts::Mut;
63
64    #[test]
65    fn test_mut_fmt_vec() {
66        let value = vec![1, 2, 3];
67        let mut_value = Mut::new(value);
68
69        assert_eq!(format!("{mut_value:?}"), "[1, 2, 3]");
70    }
71
72    #[test]
73    fn test_deref_for_mut_with_complex_type() {
74        let value = vec![1, 2, 3];
75        let mut_value = Mut::new(value);
76        let deref_value: &Vec<i32> = &mut_value;
77
78        assert_eq!(deref_value, &vec![1, 2, 3], "Dereferencing Mut should return the inner value");
79    }
80}