gstreamer/
utils.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::prelude::*;
4
5// rustdoc-stripper-ignore-next
6/// Trait that allows accessing `Display` implementation on types external to this crate.
7pub trait Displayable {
8    type DisplayImpl: std::fmt::Display;
9
10    fn display(self) -> Self::DisplayImpl;
11}
12
13#[must_use = "if unused the object lock will immediately be released"]
14pub struct ObjectLockGuard<'a, T: ?Sized> {
15    obj: &'a T,
16    mutex: &'a mut glib::ffi::GMutex,
17}
18
19impl<'a, T> ObjectLockGuard<'a, T>
20where
21    T: IsA<crate::Object>,
22{
23    #[inline]
24    pub fn acquire(obj: &'a T) -> ObjectLockGuard<'a, T> {
25        skip_assert_initialized!();
26        unsafe {
27            let mutex = &mut (*obj.as_ref().as_ptr()).lock;
28            glib::ffi::g_mutex_lock(mutex);
29            Self { obj, mutex }
30        }
31    }
32}
33
34impl<T> AsRef<T> for ObjectLockGuard<'_, T> {
35    #[inline]
36    fn as_ref(&self) -> &T {
37        self.obj
38    }
39}
40
41impl<T> std::ops::Deref for ObjectLockGuard<'_, T> {
42    type Target = T;
43
44    #[inline]
45    fn deref(&self) -> &Self::Target {
46        self.obj
47    }
48}
49
50impl<T> std::fmt::Debug for ObjectLockGuard<'_, T>
51where
52    T: std::fmt::Debug,
53{
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        self.obj.fmt(f)
56    }
57}
58
59impl<T> std::cmp::PartialEq for ObjectLockGuard<'_, T>
60where
61    T: std::cmp::PartialEq,
62{
63    fn eq(&self, other: &Self) -> bool {
64        self.obj.eq(other)
65    }
66}
67
68impl<T> std::cmp::Eq for ObjectLockGuard<'_, T> where T: std::cmp::Eq {}
69
70impl<T> Drop for ObjectLockGuard<'_, T>
71where
72    T: ?Sized,
73{
74    #[inline]
75    fn drop(&mut self) {
76        unsafe {
77            glib::ffi::g_mutex_unlock(self.mutex);
78        }
79    }
80}