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
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

//! Traits for classes

#![allow(clippy::not_unsafe_ptr_arg_deref)]

use raw_cstr::AsRawCstr;

use crate::{
    sys::{SIM_hap_delete_callback_id, SIM_hap_delete_callback_obj_id},
    ConfObject, HapHandle, Result,
};

/// A SIMICS Hap and the type of callbacks associated with it
pub trait Hap {
    /// The type of the name of the HAP, must be convertible to raw C string to pass to
    /// the simulator
    type Name: AsRawCstr;
    /// The name of the HAP.
    const NAME: Self::Name;

    /// A callback for a hap can be deleted by its handle
    fn delete_callback_id(handle: HapHandle) -> Result<()> {
        unsafe { SIM_hap_delete_callback_id(Self::NAME.as_raw_cstr()?, handle) };
        Ok(())
    }

    /// A callback for a hap can be deleted by the object it is associated with
    fn delete_callback_obj_id(obj: *mut ConfObject, handle: HapHandle) -> Result<()> {
        unsafe { SIM_hap_delete_callback_obj_id(Self::NAME.as_raw_cstr()?, obj, handle) };
        Ok(())
    }
}