gpu_telemetry 0.2.1

High-level GPU telemetry background logger
Documentation
//! All error handling in this crate.
//!
//! More information about error handling in [`Error`].

use std::{error, fmt::Display};

/// Includes both a message and an error kind.
#[derive(Debug)]
pub struct Error {
    /// The message attached to this error.
    pub msg: &'static str,

    /// The kind of error that occured.
    ///
    /// More information about what that means in [`ErrorKind`] docs.
    pub kind: ErrorKind,
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.kind, self.msg)
    }
}

impl error::Error for Error {}

/// Any kind of  error that can occur in GPU telemetry.
#[derive(Debug)]
pub enum ErrorKind {
    /// Synchronization error between GPU and CPU or CPU threads.
    SyncError,

    /// Invalid interval used to initialize monitors (duration of zero).
    InvalidInterval,

    /// Invalid event handle, only when an internal error occurs on DX.
    InvalidEventHandle,

    /// Waiting failed, usually related to [`Self::InvalidEventHandle`].
    WaitFailed,

    /// Failed to create an adapter and hence could not find a device.
    FailedAdapterCreation,

    /// Failed to create an event for DX backend.
    FailedEventCreation,

    /// Failed to query the GPU for telemetry.
    FailedInfoQuery,

    /// Devices don't have required features for telemetry.
    UnsupportedFeature,
}

impl Display for ErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidInterval => write!(f, "invalid interval"),
            Self::InvalidEventHandle => write!(f, "invalid event handle"),
            Self::SyncError => write!(f, "sync error"),
            Self::WaitFailed => write!(f, "wait failed"),
            Self::FailedAdapterCreation => write!(f, "failed adapter creation"),
            Self::FailedEventCreation => write!(f, "failed event creation"),
            Self::FailedInfoQuery => write!(f, "failed info query"),
            Self::UnsupportedFeature => write!(f, "unsupported feature"),
        }
    }
}