glib_signal/pointer.rs
1use {
2 glib::{translate::ToGlibPtr, types::StaticType, value::FromValue, Type, Value},
3 std::ops::Deref,
4};
5
6#[derive(Copy, Clone, Debug)]
7#[repr(transparent)]
8pub struct Pointer<T>(pub *mut T);
9
10impl<T> Pointer<T> {
11 pub fn as_ptr_mut(&self) -> *mut T {
12 self.0
13 }
14
15 pub fn as_ptr(&self) -> *const T {
16 self.0 as *const _
17 }
18
19 pub fn into_inner(self) -> *mut T {
20 self.0
21 }
22}
23
24impl<T> From<*mut T> for Pointer<T> {
25 fn from(ptr: *mut T) -> Self {
26 Self(ptr)
27 }
28}
29
30impl<T> Into<*mut T> for Pointer<T> {
31 fn into(self) -> *mut T {
32 self.into_inner()
33 }
34}
35
36impl<T> Into<*const T> for Pointer<T> {
37 fn into(self) -> *const T {
38 self.into_inner() as *const _
39 }
40}
41
42impl<T> Deref for Pointer<T> {
43 type Target = *mut T;
44
45 fn deref(&self) -> &Self::Target {
46 &self.0
47 }
48}
49
50unsafe impl<'a, T> FromValue<'a> for Pointer<T> {
51 type Checker = glib::value::GenericValueTypeChecker<Pointer<T>>;
52
53 unsafe fn from_value(value: &'a Value) -> Self {
54 let value = value.to_glib_none();
55 Self(glib::gobject_ffi::g_value_get_pointer(value.0) as *mut T)
56 }
57}
58
59impl<T> StaticType for Pointer<T> {
60 fn static_type() -> Type {
61 Type::POINTER
62 }
63}