glib/subclass/
basic.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! This module contains basic instance and class structs to be used for
5//! `GObject` subclasses that don't require any additional data in these
6//! structs and don't provide any new virtual methods.
7
8use std::{fmt, ops};
9
10use super::prelude::*;
11use crate::prelude::*;
12
13// rustdoc-stripper-ignore-next
14/// A basic instance struct that does not store any additional data.
15#[repr(C)]
16pub struct InstanceStruct<T: ObjectSubclass> {
17    parent: <T::ParentType as ObjectType>::GlibType,
18}
19
20impl<T: ObjectSubclass> fmt::Debug for InstanceStruct<T>
21where
22    <T::ParentType as ObjectType>::GlibType: fmt::Debug,
23{
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        f.debug_struct("InstanceStruct")
26            .field("parent", &self.parent)
27            .finish()
28    }
29}
30
31unsafe impl<T: ObjectSubclass> super::types::InstanceStruct for InstanceStruct<T> {
32    type Type = T;
33}
34
35// rustdoc-stripper-ignore-next
36/// A basic class struct that does not store any additional data
37/// or virtual methods.
38#[repr(C)]
39pub struct ClassStruct<T: ObjectSubclass> {
40    parent_class: <T::ParentType as ObjectType>::GlibClassType,
41}
42
43impl<T: ObjectSubclass> fmt::Debug for ClassStruct<T>
44where
45    <T::ParentType as ObjectType>::GlibClassType: fmt::Debug,
46{
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        f.debug_struct("InstanceStruct")
49            .field("parent_class", &self.parent_class)
50            .finish()
51    }
52}
53
54unsafe impl<T: ObjectSubclass> super::types::ClassStruct for ClassStruct<T> {
55    type Type = T;
56}
57
58impl<T: ObjectSubclass> ops::Deref for ClassStruct<T> {
59    type Target = crate::Class<<T as ObjectSubclass>::Type>;
60
61    #[inline]
62    fn deref(&self) -> &Self::Target {
63        unsafe { &*(self as *const _ as *const Self::Target) }
64    }
65}
66
67impl<T: ObjectSubclass> ops::DerefMut for ClassStruct<T> {
68    #[inline]
69    fn deref_mut(&mut self) -> &mut Self::Target {
70        unsafe { &mut *(self as *mut _ as *mut Self::Target) }
71    }
72}