Skip to main content

gstreamer_vulkan/
vulkan_operation.rs

1use crate::VulkanOperation;
2use crate::ffi;
3use glib::{prelude::*, translate::*};
4
5#[derive(Debug)]
6#[must_use = "Need to call `end`, otherwise drop will panic."]
7pub struct VulkanOperationGuard<'a> {
8    obj: &'a VulkanOperation,
9    ended: bool,
10}
11
12impl VulkanOperationGuard<'_> {
13    #[doc(alias = "gst_vulkan_operation_end")]
14    pub fn end(mut self) -> Result<(), glib::Error> {
15        self.ended = true;
16        unsafe {
17            let mut error = std::ptr::null_mut();
18            let is_ok = ffi::gst_vulkan_operation_end(self.obj.to_glib_none().0, &mut error);
19            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
20            if error.is_null() {
21                Ok(())
22            } else {
23                Err(from_glib_full(error))
24            }
25        }
26    }
27}
28
29impl Drop for VulkanOperationGuard<'_> {
30    fn drop(&mut self) {
31        if !self.ended {
32            panic!("Dropped a VulkanOperationGuard without calling `end`.")
33        }
34    }
35}
36impl PartialEq for VulkanOperationGuard<'_> {
37    fn eq(&self, other: &Self) -> bool {
38        self.obj == other.obj
39    }
40}
41impl Eq for VulkanOperationGuard<'_> {}
42
43pub trait VulkanOperationExtManual: IsA<VulkanOperation> + 'static {
44    // rustdoc-stripper-ignore-next
45    /// Returns a guard struct for the begun operation.
46    /// The `end` method on the guard **must** be called; Dropping it without results in a panic
47    #[doc(alias = "gst_vulkan_operation_begin")]
48    fn begin<'a>(&'a self) -> Result<VulkanOperationGuard<'a>, glib::Error> {
49        unsafe {
50            let mut error = std::ptr::null_mut();
51            let is_ok = ffi::gst_vulkan_operation_begin(self.as_ref().to_glib_none().0, &mut error);
52            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
53            if !error.is_null() {
54                return Err(from_glib_full(error));
55            }
56        }
57        Ok(VulkanOperationGuard {
58            obj: self.upcast_ref(),
59            ended: false,
60        })
61    }
62}
63impl<O: IsA<VulkanOperation>> VulkanOperationExtManual for O {}