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