1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{marker::PhantomData, mem};

use glib::translate::*;

use crate::MemoryFlags;

/// Parameters to control the allocation of memory
#[derive(Debug, Clone)]
#[doc(alias = "GstAllocationParams")]
#[repr(transparent)]
pub struct AllocationParams(ffi::GstAllocationParams);

unsafe impl Send for AllocationParams {}
unsafe impl Sync for AllocationParams {}

impl Default for AllocationParams {
    fn default() -> Self {
        unsafe {
            let mut params = mem::MaybeUninit::uninit();
            ffi::gst_allocation_params_init(params.as_mut_ptr());
            AllocationParams(params.assume_init())
        }
    }
}

impl AllocationParams {
    #[doc(alias = "get_flags")]
    #[inline]
    pub fn flags(&self) -> MemoryFlags {
        unsafe { from_glib(self.0.flags) }
    }

    #[doc(alias = "get_align")]
    #[inline]
    pub fn align(&self) -> usize {
        self.0.align
    }

    #[doc(alias = "get_prefix")]
    #[inline]
    pub fn prefix(&self) -> usize {
        self.0.prefix
    }

    #[doc(alias = "get_padding")]
    #[inline]
    pub fn padding(&self) -> usize {
        self.0.padding
    }

    /// Create a new [`AllocationParams`][crate::AllocationParams] on the heap. This function is for
    /// use in GStreamer language bindings. In your own code, you can just
    /// declare a [`AllocationParams`][crate::AllocationParams] on the stack or in a struct, and
    /// call [`init()`][Self::init()] to initialize it.
    ///
    /// You do not need to call [`init()`][Self::init()] on the instance
    /// returned by this function.
    ///
    /// # Returns
    ///
    /// a new [`AllocationParams`][crate::AllocationParams]
    pub fn new(flags: MemoryFlags, align: usize, prefix: usize, padding: usize) -> Self {
        assert_initialized_main_thread!();
        let params = unsafe {
            ffi::GstAllocationParams {
                flags: flags.into_glib(),
                align,
                prefix,
                padding,
                ..mem::zeroed()
            }
        };

        params.into()
    }

    #[inline]
    pub fn as_ptr(&self) -> *const ffi::GstAllocationParams {
        &self.0
    }
}

impl From<ffi::GstAllocationParams> for AllocationParams {
    #[inline]
    fn from(params: ffi::GstAllocationParams) -> Self {
        skip_assert_initialized!();
        AllocationParams(params)
    }
}

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::GstAllocationParams> for AllocationParams {
    type Storage = PhantomData<&'a Self>;

    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GstAllocationParams, Self> {
        Stash(&self.0, PhantomData)
    }
}

impl FromGlib<ffi::GstAllocationParams> for AllocationParams {
    #[allow(unused_unsafe)]
    #[inline]
    unsafe fn from_glib(value: ffi::GstAllocationParams) -> Self {
        skip_assert_initialized!();
        Self::from(value)
    }
}