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

//! Conversions to and from `ConfObject` pointers

use crate::ConfObject;

/// Convert from a reference to a raw pointer
pub trait AsConfObject {
    /// Convert a reference to this object to a raw `ConfObject` pointer
    fn as_conf_object(&self) -> *const ConfObject {
        self as *const _ as *const ConfObject
    }

    /// Convert a mutable reference to this object to a raw `ConfObject` pointer
    fn as_conf_object_mut(&mut self) -> *mut ConfObject {
        self as *mut _ as *mut ConfObject
    }
}

/// Convert from a raw pointer to a reference
pub trait FromConfObject
where
    Self: Sized,
{
    /// Get a reference to this object from a raw `ConfObject` pointer
    ///
    /// # Safety
    ///
    /// This function dereferences a raw pointer. It must be called with a valid pointer which
    /// has a sufficient lifetime.
    unsafe fn from_conf_object<'a>(obj: *const ConfObject) -> &'a Self {
        &*(obj as *const Self)
    }

    /// Get a mutable reference to this object from a raw `ConfObject` pointer
    ///
    /// # Safety
    ///
    /// This function dereferences a raw pointer. It must be called with a valid pointer which
    /// has a sufficient lifetime.
    unsafe fn from_conf_object_mut<'a>(obj: *mut ConfObject) -> &'a mut Self {
        &mut *(obj as *mut Self)
    }
}