Skip to content

Commit 56e8fd1

Browse files
committed
Rust enums for FFI => constants (resolves #4)
1 parent 8aaa0a3 commit 56e8fd1

File tree

16 files changed

+797
-760
lines changed

16 files changed

+797
-760
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ error-chain = "0.10"
2020
bitflags = "0.8"
2121
serde = { version = "1.0", optional = true, features = ["derive"] }
2222
nvml-wrapper-sys = { path = "nvml-wrapper-sys" }
23-
wrapcenum-derive = "0.1"
23+
wrapcenum-derive = { path = "../wrapcenum-derive" }
2424

2525
[features]
2626
default = []

nvml-wrapper-sys/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Generate bindings: bindgen --constified-enum "nvml.+" --ctypes-prefix raw
2+
// --no-doc-comments --raw-line "#![allow(non_upper_case_globals)]" --raw-line
3+
// "#![allow(non_camel_case_types)]" --raw-line "#![allow(non_snake_case)]"
4+
// --raw-line "#![allow(dead_code)]" --raw-line "use std::os::raw;" -o
5+
// genned_bindings.rs nvml.h
6+
17
#[cfg(target_os = "windows")]
28
fn main() {
39
println!("cargo:rustc-link-lib=nvml");

nvml-wrapper-sys/src/bindings.rs

Lines changed: 582 additions & 609 deletions
Large diffs are not rendered by default.

src/bitmasks/nv_link.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ bitflags! {
1010
// Checked against local
1111
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1212
pub flags PacketTypes: u32 {
13-
const NO_OP = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_NOP as u32,
14-
const READ = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_READ as u32,
15-
const WRITE = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_WRITE as u32,
13+
const NO_OP = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_NOP as u32,
14+
const READ = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_READ as u32,
15+
const WRITE = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_WRITE as u32,
1616
/// Reduction atomic requests.
17-
const RATOM = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_RATOM as u32,
17+
const RATOM = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_RATOM as u32,
1818
/// Non-reduction atomic requests.
19-
const NON_RATOM = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_NRATOM as u32,
19+
const NON_RATOM = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_NRATOM as u32,
2020
/// Flush requests.
21-
const FLUSH = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_FLUSH as u32,
21+
const FLUSH = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_FLUSH as u32,
2222
/// Responses with data.
23-
const WITH_DATA = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_RESPDATA as u32,
23+
const WITH_DATA = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_RESPDATA as u32,
2424
/// Responses without data.
25-
const NO_DATA = PKT_TYPES_NVML_NVLINK_COUNTER_PKTFILTER_RESPNODATA as u32,
25+
const NO_DATA = nvmlNvLinkUtilizationCountPktTypes_enum_NVML_NVLINK_COUNTER_PKTFILTER_RESPNODATA as u32,
2626
}
2727
}

src/device.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl<'nvml> Device<'nvml> {
9595
does not support the feature that is being queried (e.g. enabling/disabling auto
9696
boosted clocks is not supported by this `Device`).
9797
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
98+
* `UnexpectedVariant`, for which you can read the docs for
9899
* `Unknown`, on any unexpected error
99100
100101
# Device Support
@@ -108,7 +109,7 @@ impl<'nvml> Device<'nvml> {
108109
let mut restricted_state: nvmlEnableState_t = mem::zeroed();
109110
nvml_try(nvmlDeviceGetAPIRestriction(self.device, api.as_c(), &mut restricted_state))?;
110111

111-
Ok(bool_from_state(restricted_state))
112+
Ok(bool_from_state(restricted_state)?)
112113
}
113114
}
114115

@@ -156,6 +157,7 @@ impl<'nvml> Device<'nvml> {
156157
* `InvalidArg`, if the device is invalid
157158
* `NotSupported`, if this `Device` does not support auto boosted clocks
158159
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
160+
* `UnexpectedVariant`, for which you can read the docs for
159161
* `Unknown`, on any unexpected error
160162
161163
# Device Support
@@ -170,8 +172,8 @@ impl<'nvml> Device<'nvml> {
170172
let mut is_enabled_default: nvmlEnableState_t = mem::zeroed();
171173
nvml_try(nvmlDeviceGetAutoBoostedClocksEnabled(self.device, &mut is_enabled, &mut is_enabled_default))?;
172174

173-
Ok(AutoBoostClocksEnabledInfo{ is_enabled: bool_from_state(is_enabled),
174-
is_enabled_default: bool_from_state(is_enabled_default) })
175+
Ok(AutoBoostClocksEnabledInfo{ is_enabled: bool_from_state(is_enabled)?,
176+
is_enabled_default: bool_from_state(is_enabled_default)? })
175177
}
176178
}
177179

@@ -272,6 +274,7 @@ impl<'nvml> Device<'nvml> {
272274
* `InvalidArg`, if the device is invalid
273275
* `NotSupported`, if this `Device` does not support this feature
274276
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
277+
* `UnexpectedVariant`, for which you can read the docs for
275278
* `Unknown`, on any unexpected error
276279
277280
# Device Support
@@ -285,7 +288,7 @@ impl<'nvml> Device<'nvml> {
285288
let mut info: nvmlBridgeChipHierarchy_t = mem::zeroed();
286289
nvml_try(nvmlDeviceGetBridgeChipInfo(self.device, &mut info))?;
287290

288-
Ok(BridgeChipHierarchy::from(info))
291+
Ok(BridgeChipHierarchy::try_from(info)?)
289292
}
290293
}
291294

@@ -449,8 +452,8 @@ impl<'nvml> Device<'nvml> {
449452

450453
// Passing null doesn't mean we want the count, it's just allowed
451454
match nvmlDeviceGetComputeRunningProcesses(self.device, &mut count, ptr::null_mut()) {
452-
nvmlReturn_t::NVML_SUCCESS => Ok(0),
453-
nvmlReturn_t::NVML_ERROR_INSUFFICIENT_SIZE => Ok(count),
455+
nvmlReturn_enum_NVML_SUCCESS => Ok(0),
456+
nvmlReturn_enum_NVML_ERROR_INSUFFICIENT_SIZE => Ok(count),
454457
// We know that this wil be an error
455458
other => nvml_try(other).map(|_| 0),
456459
}
@@ -633,6 +636,7 @@ impl<'nvml> Device<'nvml> {
633636
* `InvalidArg`, if the device is invalid
634637
* `NotSupported`, if this `Device` does not support this feature
635638
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
639+
* `UnexpectedVariant`, for which you can read the docs for
636640
* `Unknown`, on any unexpected error
637641
*/
638642
// Checked against local
@@ -643,7 +647,7 @@ impl<'nvml> Device<'nvml> {
643647
let mut state: nvmlEnableState_t = mem::zeroed();
644648
nvml_try(nvmlDeviceGetDisplayActive(self.device, &mut state))?;
645649

646-
Ok(bool_from_state(state))
650+
Ok(bool_from_state(state)?)
647651
}
648652
}
649653

@@ -658,6 +662,7 @@ impl<'nvml> Device<'nvml> {
658662
* `InvalidArg`, if the device is invalid
659663
* `NotSupported`, if this `Device` does not support this feature
660664
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
665+
* `UnexpectedVariant`, for which you can read the docs for
661666
* `Unknown`, on any unexpected error
662667
*/
663668
// Checked against local
@@ -668,7 +673,7 @@ impl<'nvml> Device<'nvml> {
668673
let mut state: nvmlEnableState_t = mem::zeroed();
669674
nvml_try(nvmlDeviceGetDisplayMode(self.device, &mut state))?;
670675

671-
Ok(bool_from_state(state))
676+
Ok(bool_from_state(state)?)
672677
}
673678
}
674679

@@ -717,6 +722,7 @@ impl<'nvml> Device<'nvml> {
717722
* `InvalidArg`, if the device is invalid
718723
* `NotSupported`, if this `Device` does not support this feature
719724
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
725+
* `UnexpectedVariant`, for which you can read the docs for
720726
* `Unknown`, on any unexpected error
721727
722728
# Device Support
@@ -732,8 +738,8 @@ impl<'nvml> Device<'nvml> {
732738
let mut pending: nvmlEnableState_t = mem::zeroed();
733739
nvml_try(nvmlDeviceGetEccMode(self.device, &mut current, &mut pending))?;
734740

735-
Ok(EccModeState{ currently_enabled: bool_from_state(current),
736-
pending_enabled: bool_from_state(pending) })
741+
Ok(EccModeState{ currently_enabled: bool_from_state(current)?,
742+
pending_enabled: bool_from_state(pending)? })
737743
}
738744
}
739745

@@ -831,6 +837,7 @@ impl<'nvml> Device<'nvml> {
831837
* `InvalidArg`, if the device is invalid
832838
* `NotSupported`, if this `Device` does not support this feature
833839
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
840+
* `UnexpectedVariant`, for which you can read the docs for
834841
* `Unknown`, on any unexpected error
835842
836843
# Device Support
@@ -847,8 +854,8 @@ impl<'nvml> Device<'nvml> {
847854
let mut pending: nvmlGpuOperationMode_t = mem::zeroed();
848855
nvml_try(nvmlDeviceGetGpuOperationMode(self.device, &mut current, &mut pending))?;
849856

850-
Ok(OperationModeState{ current: current.into(),
851-
pending: pending.into() })
857+
Ok(OperationModeState{ current: OperationMode::try_from(current)?,
858+
pending: OperationMode::try_from(pending)? })
852859
}
853860
}
854861

@@ -892,6 +899,7 @@ impl<'nvml> Device<'nvml> {
892899
* `Uninitialized`, if the library has not been successfully initialized
893900
* `InvalidArg`, if the device is invalid
894901
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
902+
* `UnexpectedVariant`, for which you can read the docs for
895903
* `Unknown`, on any unexpected error
896904
*/
897905
// Tested as part of `.running_graphics_processes()`
@@ -903,8 +911,8 @@ impl<'nvml> Device<'nvml> {
903911

904912
// Passing null doesn't indicate that we want the count. It's just allowed.
905913
match nvmlDeviceGetGraphicsRunningProcesses(self.device, &mut count, ptr::null_mut()) {
906-
nvmlReturn_t::NVML_SUCCESS => Ok(0),
907-
nvmlReturn_t::NVML_ERROR_INSUFFICIENT_SIZE => Ok(count),
914+
nvmlReturn_enum_NVML_SUCCESS => Ok(0),
915+
nvmlReturn_enum_NVML_ERROR_INSUFFICIENT_SIZE => Ok(count),
908916
// We know that this will be an error
909917
other => nvml_try(other).map(|_| 0),
910918
}
@@ -1361,6 +1369,7 @@ impl<'nvml> Device<'nvml> {
13611369
* `InvalidArg`, if the device is invalid
13621370
* `NotSupported`, if this `Device` does not support this feature
13631371
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
1372+
* `UnexpectedVariant`, for which you can read the docs for
13641373
* `Unknown`, on any unexpected error
13651374
13661375
# Device Support
@@ -1374,7 +1383,7 @@ impl<'nvml> Device<'nvml> {
13741383
let mut state: nvmlPstates_t = mem::zeroed();
13751384
nvml_try(nvmlDeviceGetPerformanceState(self.device, &mut state))?;
13761385

1377-
Ok(state.into())
1386+
Ok(PerformanceState::try_from(state)?)
13781387
}
13791388
}
13801389

@@ -1389,6 +1398,7 @@ impl<'nvml> Device<'nvml> {
13891398
* `InvalidArg`, if the device is invalid
13901399
* `NotSupported`, if this `Device` does not support this feature
13911400
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
1401+
* `UnexpectedVariant`, for which you can read the docs for
13921402
* `Unknown`, on any unexpected error
13931403
13941404
# Platform Support
@@ -1403,7 +1413,7 @@ impl<'nvml> Device<'nvml> {
14031413
let mut state: nvmlEnableState_t = mem::zeroed();
14041414
nvml_try(nvmlDeviceGetPersistenceMode(self.device, &mut state))?;
14051415

1406-
Ok(bool_from_state(state))
1416+
Ok(bool_from_state(state)?)
14071417
}
14081418
}
14091419

@@ -1504,7 +1514,7 @@ impl<'nvml> Device<'nvml> {
15041514
let mut state: nvmlEnableState_t = mem::zeroed();
15051515
nvml_try(nvmlDeviceGetPowerManagementMode(self.device, &mut state))?;
15061516

1507-
Ok(bool_from_state(state))
1517+
Ok(bool_from_state(state)?)
15081518
}
15091519
}
15101520

@@ -1517,7 +1527,7 @@ impl<'nvml> Device<'nvml> {
15171527
let mut state: nvmlPstates_t = mem::zeroed();
15181528
nvml_try(nvmlDeviceGetPowerState(self.device, &mut state))?;
15191529

1520-
Ok(state.into())
1530+
Ok(PerformanceState::try_from(state)?)
15211531
}
15221532
}
15231533

@@ -1617,6 +1627,7 @@ impl<'nvml> Device<'nvml> {
16171627
* `InvalidArg`, if the device is invalid
16181628
* `NotSupported`, if this `Device` doesn't support this feature
16191629
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
1630+
* `UnexpectedVariant`, for which you can read the docs for
16201631
* `Unknown`, on any unexpected error
16211632
16221633
# Device Support
@@ -1630,7 +1641,7 @@ impl<'nvml> Device<'nvml> {
16301641
let mut state: nvmlEnableState_t = mem::zeroed();
16311642
nvml_try(nvmlDeviceGetRetiredPagesPendingStatus(self.device, &mut state))?;
16321643

1633-
Ok(bool_from_state(state))
1644+
Ok(bool_from_state(state)?)
16341645
}
16351646
}
16361647

@@ -1890,7 +1901,7 @@ impl<'nvml> Device<'nvml> {
18901901
for_mem_clock,
18911902
&mut count,
18921903
items.as_mut_ptr()) {
1893-
nvmlReturn_t::NVML_ERROR_INSUFFICIENT_SIZE =>
1904+
nvmlReturn_enum_NVML_ERROR_INSUFFICIENT_SIZE =>
18941905
// `count` is now the size that is required. Return it in the error.
18951906
bail!(ErrorKind::InsufficientSize(count as usize)),
18961907
value => nvml_try(value)?,
@@ -1937,7 +1948,7 @@ impl<'nvml> Device<'nvml> {
19371948
match nvmlDeviceGetSupportedMemoryClocks(self.device,
19381949
&mut count,
19391950
items.as_mut_ptr()) {
1940-
nvmlReturn_t::NVML_ERROR_INSUFFICIENT_SIZE =>
1951+
nvmlReturn_enum_NVML_ERROR_INSUFFICIENT_SIZE =>
19411952
// `count` is now the size that is required. Return it in the error.
19421953
bail!(ErrorKind::InsufficientSize(count as usize)),
19431954
value => nvml_try(value)?,
@@ -2001,6 +2012,7 @@ impl<'nvml> Device<'nvml> {
20012012
# Errors
20022013
* `InvalidArg`, if either `Device` is invalid
20032014
* `NotSupported`, if this `Device` or the OS does not support this feature
2015+
* `UnexpectedVariant`, for which you can read the docs for
20042016
* `Unknown`, an error has occurred in the underlying topology discovery
20052017
20062018
# Platform Support
@@ -2015,7 +2027,7 @@ impl<'nvml> Device<'nvml> {
20152027
let mut level: nvmlGpuTopologyLevel_t = mem::zeroed();
20162028
nvml_try(nvmlDeviceGetTopologyCommonAncestor(self.device, other_device.device, &mut level))?;
20172029

2018-
Ok(level.into())
2030+
Ok(TopologyLevel::try_from(level)?)
20192031
}
20202032
}
20212033

@@ -2493,6 +2505,7 @@ impl<'nvml> Device<'nvml> {
24932505
* `Uninitialized`, if the library has not been successfully initialized
24942506
* `InvalidArg`, if the `Device` is invalid
24952507
* `NotSupported`, if this `Device` does not support this feature
2508+
* `UnexpectedVariant`, for which you can read the docs for
24962509
* `Unknown`, on any unexpected error
24972510
24982511
# Device Support
@@ -2506,7 +2519,7 @@ impl<'nvml> Device<'nvml> {
25062519
let mut state: nvmlEnableState_t = mem::zeroed();
25072520
nvml_try(nvmlDeviceGetAccountingMode(self.device, &mut state))?;
25082521

2509-
Ok(bool_from_state(state))
2522+
Ok(bool_from_state(state)?)
25102523
}
25112524
}
25122525

@@ -2549,9 +2562,9 @@ impl<'nvml> Device<'nvml> {
25492562
// Null also indicates that we want the count
25502563
match nvmlDeviceGetAccountingPids(self.device, &mut count, ptr::null_mut()) {
25512564
// List is empty
2552-
nvmlReturn_t::NVML_SUCCESS => Ok(0),
2565+
nvmlReturn_enum_NVML_SUCCESS => Ok(0),
25532566
// Count is set to pids count
2554-
nvmlReturn_t::NVML_ERROR_INSUFFICIENT_SIZE => Ok(count),
2567+
nvmlReturn_enum_NVML_ERROR_INSUFFICIENT_SIZE => Ok(count),
25552568
// We know this is an error
25562569
other => nvml_try(other).map(|_| 0),
25572570
}
@@ -3178,6 +3191,7 @@ impl<'nvml> Device<'nvml> {
31783191
* `Uninitialized`, if the library has not been successfully initialized
31793192
* `NotSupported`, if this `Device` doesn't support this feature
31803193
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
3194+
* `UnexpectedVariant`, for which you can read the docs for
31813195
* `Unknown`, on any unexpected error
31823196
31833197
In addition, all of the errors returned by:
@@ -3229,7 +3243,7 @@ impl<'nvml> Device<'nvml> {
32293243
let mut state: nvmlEnableState_t = mem::zeroed();
32303244
nvml_try(nvmlDeviceQueryDrainState(&mut pci_info.try_into_c()?, &mut state))?;
32313245

3232-
Ok(bool_from_state(state))
3246+
Ok(bool_from_state(state)?)
32333247
}
32343248
}
32353249

0 commit comments

Comments
 (0)