nvml_wrapper/struct_wrappers/
nv_link.rs

1use crate::bitmasks::nv_link::PacketTypes;
2use crate::enum_wrappers::nv_link::UtilizationCountUnit;
3use crate::error::NvmlError;
4use crate::ffi::bindings::*;
5#[cfg(feature = "serde")]
6use serde_derive::{Deserialize, Serialize};
7use std::convert::TryFrom;
8
9/// Defines NvLink counter controls.
10// TODO: Write a test going to / from C repr
11#[derive(Debug, Clone, Eq, PartialEq, Hash)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13pub struct UtilizationControl {
14    pub units: UtilizationCountUnit,
15    pub packet_filter: PacketTypes,
16}
17
18impl UtilizationControl {
19    /// Obtain this struct's C counterpart.
20    pub fn as_c(&self) -> nvmlNvLinkUtilizationControl_t {
21        nvmlNvLinkUtilizationControl_t {
22            units: self.units.as_c(),
23            pktfilter: self.packet_filter.bits(),
24        }
25    }
26}
27
28impl TryFrom<nvmlNvLinkUtilizationControl_t> for UtilizationControl {
29    type Error = NvmlError;
30
31    /**
32    Construct `UtilizationControl` from the corresponding C struct.
33
34    The `packet_filter` bitmask is created via the `PacketTypes::from_bits_truncate`
35    method, meaning that any bits that don't correspond to flags present in this
36    version of the wrapper will be dropped.
37
38    # Errors
39
40    * `UnexpectedVariant`, for which you can read the docs for
41    */
42    fn try_from(value: nvmlNvLinkUtilizationControl_t) -> Result<Self, Self::Error> {
43        let bits = value.pktfilter;
44
45        Ok(UtilizationControl {
46            units: UtilizationCountUnit::try_from(value.units)?,
47            packet_filter: PacketTypes::from_bits_truncate(bits),
48        })
49    }
50}