1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: MIT

use super::*;

/// Trait which should be implemented by subclass of [`FwFcp`][crate::FwFcp].
pub trait FwFcpImpl: ObjectImpl + FwRespImpl {
    fn responded(&self, fcp: &Self::Type, generation: u32, tstamp: u32, frame: &[u8]) {
        self.parent_responded(fcp, generation, tstamp, frame)
    }
}

/// Trait which is automatically implemented to implementator of [`FwFcpImpl`][self::FwFcpImpl].
pub trait FwFcpImplExt: ObjectSubclass {
    fn parent_responded(&self, fcp: &Self::Type, generation: u32, tstamp: u32, frame: &[u8]);
}

impl<T: FwFcpImpl> FwFcpImplExt for T {
    fn parent_responded(&self, fcp: &Self::Type, generation: u32, tstamp: u32, frame: &[u8]) {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::HinawaFwFcpClass;
            let f = (*parent_class)
                .responded
                .expect("No parent class implementation for \"responded\"");
            f(
                fcp.unsafe_cast_ref::<FwFcp>().to_glib_none().0,
                generation,
                tstamp,
                frame.as_ptr(),
                frame.len() as u32,
            )
        }
    }
}

unsafe impl<T: FwFcpImpl> IsSubclassable<T> for FwFcp {
    fn class_init(class: &mut Class<Self>) {
        Self::parent_class_init::<T>(class);

        let klass = class.as_mut();
        klass.responded = Some(fw_fcp_responded::<T>);
    }
}

unsafe extern "C" fn fw_fcp_responded<T: FwFcpImpl>(
    ptr: *mut ffi::HinawaFwFcp,
    generation: c_uint,
    tstamp: c_uint,
    frame: *const u8,
    length: c_uint,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let wrap: Borrowed<FwFcp> = from_glib_borrow(ptr);

    imp.responded(
        wrap.unsafe_cast_ref(),
        generation,
        tstamp,
        std::slice::from_raw_parts(frame, length as usize),
    )
}