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
use crate::bitmasks::nv_link::PacketTypes;
use crate::enum_wrappers::nv_link::UtilizationCountUnit;
use crate::error::NvmlError;
use crate::ffi::bindings::*;
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;

/// Defines NvLink counter controls.
// TODO: Write a test going to / from C repr
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UtilizationControl {
    pub units: UtilizationCountUnit,
    pub packet_filter: PacketTypes,
}

impl UtilizationControl {
    /// Obtain this struct's C counterpart.
    pub fn as_c(&self) -> nvmlNvLinkUtilizationControl_t {
        nvmlNvLinkUtilizationControl_t {
            units: self.units.as_c(),
            pktfilter: self.packet_filter.bits(),
        }
    }
}

impl TryFrom<nvmlNvLinkUtilizationControl_t> for UtilizationControl {
    type Error = NvmlError;

    /**
    Construct `UtilizationControl` from the corresponding C struct.

    The `packet_filter` bitmask is created via the `PacketTypes::from_bits_truncate`
    method, meaning that any bits that don't correspond to flags present in this
    version of the wrapper will be dropped.

    # Errors

    * `UnexpectedVariant`, for which you can read the docs for
    */
    fn try_from(value: nvmlNvLinkUtilizationControl_t) -> Result<Self, Self::Error> {
        let bits = value.pktfilter;

        Ok(UtilizationControl {
            units: UtilizationCountUnit::try_from(value.units)?,
            packet_filter: PacketTypes::from_bits_truncate(bits),
        })
    }
}