clang_rt_xray/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3use clang_rt_xray_sys::interface::*;
4
5pub mod raw;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct FunctionID(pub(crate) i32);
9
10impl FunctionID {
11    pub fn iter() -> impl Iterator<Item = Self> {
12        // SAFETY: Thread-safe call, safe in any context.
13        let max_id = unsafe { __xray_max_function_id() };
14        // C interface is a bit retarded here, but it is what it is.
15        // Function IDs are positive i32 values, starting at 1.
16        let max_id = i32::try_from(max_id).unwrap_or(0);
17        (1..=max_id).map(Self)
18    }
19
20    pub fn count() -> usize {
21        // SAFETY: Thread-safe call, safe in any context.
22        unsafe { __xray_max_function_id() }
23    }
24
25    // TODO: cast into appropriate pointer? at least some pointer-like thing? void*?
26    pub fn address(&self) -> usize {
27        // SAFETY: Thread-safe call, safe in any context, FunctionID stores only valid IDs.
28        unsafe { __xray_function_address(self.0) }
29    }
30}