1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use glib::prelude::*; |
4 | |
5 | // rustdoc-stripper-ignore-next |
6 | /// Trait that allows accessing `Display` implementation on types external to this crate. |
7 | pub 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" ] |
14 | pub struct ObjectLockGuard<'a, T: ?Sized> { |
15 | obj: &'a T, |
16 | mutex: &'a mut glib::ffi::GMutex, |
17 | } |
18 | |
19 | impl<'a, T> ObjectLockGuard<'a, T> |
20 | where |
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 GMutex = &mut (*obj.as_ref().as_ptr()).lock; |
28 | glib::ffi::g_mutex_lock(mutex); |
29 | Self { obj, mutex } |
30 | } |
31 | } |
32 | } |
33 | |
34 | impl<T> AsRef<T> for ObjectLockGuard<'_, T> { |
35 | #[inline ] |
36 | fn as_ref(&self) -> &T { |
37 | self.obj |
38 | } |
39 | } |
40 | |
41 | impl<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 | |
50 | impl<T> std::fmt::Debug for ObjectLockGuard<'_, T> |
51 | where |
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 | |
59 | impl<T> std::cmp::PartialEq for ObjectLockGuard<'_, T> |
60 | where |
61 | T: std::cmp::PartialEq, |
62 | { |
63 | fn eq(&self, other: &Self) -> bool { |
64 | self.obj.eq(other) |
65 | } |
66 | } |
67 | |
68 | impl<T> std::cmp::Eq for ObjectLockGuard<'_, T> where T: std::cmp::Eq {} |
69 | |
70 | impl<T> Drop for ObjectLockGuard<'_, T> |
71 | where |
72 | T: ?Sized, |
73 | { |
74 | #[inline ] |
75 | fn drop(&mut self) { |
76 | unsafe { |
77 | glib::ffi::g_mutex_unlock(self.mutex); |
78 | } |
79 | } |
80 | } |
81 | |