nvapi/
types.rs

1use std::ffi::CStr;
2use std::{fmt, ops};
3use void::Void;
4use sys;
5
6pub trait RawConversion {
7    type Target;
8    type Error;
9
10    fn convert_raw(&self) -> Result<Self::Target, Self::Error>;
11}
12
13impl RawConversion for sys::types::NvAPI_ShortString {
14    type Target = String;
15    type Error = Void;
16
17    fn convert_raw(&self) -> Result<Self::Target, Self::Error> {
18        unsafe {
19            Ok(CStr::from_ptr(self.as_ptr()).to_string_lossy().into_owned())
20        }
21    }
22}
23
24#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
25#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
26pub struct Celsius(pub i32);
27
28impl fmt::Display for Celsius {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        write!(f, "{}C", self.0)
31    }
32}
33
34impl fmt::Debug for Celsius {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        fmt::Display::fmt(self, f)
37    }
38}
39
40/// Nvidia encodes temperature as `<< 8` for some reason sometimes.
41#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
42#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
43pub struct CelsiusShifted(pub i32);
44
45impl fmt::Display for CelsiusShifted {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        write!(f, "{}C", self.get())
48    }
49}
50
51impl fmt::Debug for CelsiusShifted {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        fmt::Display::fmt(self, f)
54    }
55}
56
57impl CelsiusShifted {
58    pub fn get(&self) -> i32 {
59        self.0 >> 8
60    }
61}
62
63impl From<CelsiusShifted> for Celsius {
64    fn from(c: CelsiusShifted) -> Self {
65        Celsius(c.get())
66    }
67}
68
69impl From<Celsius> for CelsiusShifted {
70    fn from(c: Celsius) -> Self {
71        CelsiusShifted(c.0 << 8)
72    }
73}
74
75#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
76#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
77pub struct Microvolts(pub u32);
78
79impl fmt::Display for Microvolts {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        let value = self.0 as f32 / 1000.0;
82        if let Some(precision) = f.precision() {
83            write!(f, "{:.*} mV", precision, value)
84        } else {
85            write!(f, "{} mV", value)
86        }
87    }
88}
89
90impl fmt::Debug for Microvolts {
91    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92        fmt::Display::fmt(self, f)
93    }
94}
95
96#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
97#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
98pub struct MicrovoltsDelta(pub i32);
99
100impl fmt::Display for MicrovoltsDelta {
101    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102        let value = self.0 as f32 / 1000.0;
103        if let Some(precision) = f.precision() {
104            write!(f, "{:.*} mV", precision, value)
105        } else {
106            write!(f, "{} mV", value)
107        }
108    }
109}
110
111impl fmt::Debug for MicrovoltsDelta {
112    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113        fmt::Display::fmt(self, f)
114    }
115}
116
117#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
118#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
119pub struct Kilohertz(pub u32);
120
121impl From<u32> for Kilohertz {
122    fn from(p: u32) -> Self {
123        Kilohertz(p)
124    }
125}
126
127impl fmt::Display for Kilohertz {
128    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
129        if self.0 < 1000 {
130            write!(f, "{} kHz", self.0)
131        } else {
132            let value = self.0 as f32 / 1000.0;
133            if let Some(precision) = f.precision() {
134                write!(f, "{:.*} MHz", precision, value)
135            } else {
136                write!(f, "{} MHz", value)
137            }
138        }
139    }
140}
141
142impl fmt::Debug for Kilohertz {
143    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144        fmt::Display::fmt(self, f)
145    }
146}
147
148impl ops::Sub for Kilohertz {
149    type Output = KilohertzDelta;
150
151    fn sub(self, rhs: Self) -> Self::Output {
152        KilohertzDelta(self.0 as i32 - rhs.0 as i32)
153    }
154}
155
156impl ops::Sub<KilohertzDelta> for Kilohertz {
157    type Output = Kilohertz;
158
159    fn sub(self, rhs: KilohertzDelta) -> Self::Output {
160        Kilohertz((self.0 as i32 - rhs.0 as i32) as u32)
161    }
162}
163
164impl ops::Add<KilohertzDelta> for Kilohertz {
165    type Output = Kilohertz;
166
167    fn add(self, rhs: KilohertzDelta) -> Self::Output {
168        Kilohertz((self.0 as i32 + rhs.0 as i32) as u32)
169    }
170}
171
172#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
173#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
174pub struct Kilohertz2(pub u32);
175
176impl Kilohertz2 {
177    pub fn get(&self) -> u32 {
178        self.0 / 2
179    }
180}
181
182impl From<Kilohertz2> for Kilohertz {
183    fn from(p: Kilohertz2) -> Self {
184        Kilohertz(p.get())
185    }
186}
187
188impl From<Kilohertz> for Kilohertz2 {
189    fn from(v: Kilohertz) -> Self {
190        Kilohertz2(v.0 * 2)
191    }
192}
193
194impl From<u32> for Kilohertz2 {
195    fn from(p: u32) -> Self {
196        Kilohertz2(p)
197    }
198}
199
200impl fmt::Display for Kilohertz2 {
201    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
202        let v = self.get();
203        if v < 1000 {
204            write!(f, "{} kHz", v)
205        } else {
206            let value = self.0 as f32 / 1000.0;
207            if let Some(precision) = f.precision() {
208                write!(f, "{:.*} MHz", precision, value)
209            } else {
210                write!(f, "{} MHz", value)
211            }
212        }
213    }
214}
215
216impl fmt::Debug for Kilohertz2 {
217    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
218        fmt::Display::fmt(self, f)
219    }
220}
221
222#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
223#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
224pub struct KilohertzDelta(pub i32);
225
226impl From<i32> for KilohertzDelta {
227    fn from(p: i32) -> Self {
228        KilohertzDelta(p)
229    }
230}
231
232impl fmt::Display for KilohertzDelta {
233    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234        if (self.0).abs() < 1000 {
235            write!(f, "{} kHz", self.0)
236        } else {
237            let value = self.0 as f32 / 1000.0;
238            if let Some(precision) = f.precision() {
239                write!(f, "{:.*} MHz", precision, value)
240            } else {
241                write!(f, "{} MHz", value)
242            }
243        }
244    }
245}
246
247impl fmt::Debug for KilohertzDelta {
248    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
249        fmt::Display::fmt(self, f)
250    }
251}
252
253impl ops::Add for KilohertzDelta {
254    type Output = KilohertzDelta;
255
256    fn add(self, rhs: Self) -> Self::Output {
257        KilohertzDelta(self.0 + rhs.0)
258    }
259}
260
261impl ops::Sub for KilohertzDelta {
262    type Output = KilohertzDelta;
263
264    fn sub(self, rhs: Self) -> Self::Output {
265        KilohertzDelta(self.0 - rhs.0)
266    }
267}
268
269impl ops::Mul<i32> for KilohertzDelta {
270    type Output = KilohertzDelta;
271
272    fn mul(self, rhs: i32) -> Self::Output {
273        KilohertzDelta(self.0 * rhs)
274    }
275}
276
277impl ops::Div<i32> for KilohertzDelta {
278    type Output = KilohertzDelta;
279
280    fn div(self, rhs: i32) -> Self::Output {
281        KilohertzDelta(self.0 / rhs)
282    }
283}
284
285#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
286#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
287pub struct Kilohertz2Delta(pub i32);
288
289impl From<i32> for Kilohertz2Delta {
290    fn from(p: i32) -> Self {
291        Kilohertz2Delta(p)
292    }
293}
294
295impl Kilohertz2Delta {
296    pub fn get(&self) -> i32 {
297        self.0 / 2
298    }
299}
300
301impl From<Kilohertz2Delta> for KilohertzDelta {
302    fn from(p: Kilohertz2Delta) -> Self {
303        KilohertzDelta(p.get())
304    }
305}
306
307impl From<KilohertzDelta> for Kilohertz2Delta {
308    fn from(v: KilohertzDelta) -> Self {
309        Kilohertz2Delta(v.0 * 2)
310    }
311}
312
313impl fmt::Display for Kilohertz2Delta {
314    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
315        let v = self.get();
316        if v.abs() < 1000 {
317            write!(f, "{} kHz", v)
318        } else {
319            let value = self.0 as f32 / 1000.0;
320            if let Some(precision) = f.precision() {
321                write!(f, "{:.*} MHz", precision, value)
322            } else {
323                write!(f, "{} MHz", value)
324            }
325        }
326    }
327}
328
329impl fmt::Debug for Kilohertz2Delta {
330    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
331        fmt::Display::fmt(self, f)
332    }
333}
334
335#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
336#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
337pub struct Kibibytes(pub u32);
338
339impl ops::Sub for Kibibytes {
340    type Output = Kibibytes;
341
342    fn sub(self, rhs: Self) -> Self::Output {
343        Kibibytes(self.0 - rhs.0)
344    }
345}
346
347impl fmt::Display for Kibibytes {
348    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
349        if self.0 < 1000 {
350            write!(f, "{} KiB", self.0)
351        } else if self.0 < 1000000 {
352            let value = self.0 as f32 / 1024.0;
353            if let Some(precision) = f.precision() {
354                write!(f, "{:.*} MiB", precision, value)
355            } else {
356                write!(f, "{} MiB", value)
357            }
358        } else {
359            let value = self.0 as f32 / 1048576.0;
360            if let Some(precision) = f.precision() {
361                write!(f, "{:.*} GiB", precision, value)
362            } else {
363                write!(f, "{} GiB", value)
364            }
365        }
366    }
367}
368
369impl fmt::Debug for Kibibytes {
370    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
371        fmt::Display::fmt(self, f)
372    }
373}
374
375#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
376#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
377pub struct Percentage(pub u32);
378
379impl fmt::Display for Percentage {
380    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
381        write!(f, "{}%", self.0)
382    }
383}
384
385impl fmt::Debug for Percentage {
386    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
387        fmt::Display::fmt(self, f)
388    }
389}
390
391impl Percentage {
392    pub fn from_raw(v: u32) -> Result<Self, sys::ArgumentRangeError> {
393        match v {
394            v @ 0...100 => Ok(Percentage(v)),
395            _ => Err(sys::ArgumentRangeError),
396        }
397    }
398}
399
400#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
401#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
402pub struct Percentage1000(pub u32);
403
404impl fmt::Display for Percentage1000 {
405    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
406        let value = self.0 as f32 / 1000.0;
407        if let Some(precision) = f.precision() {
408            write!(f, "{:.*}%", precision, value)
409        } else {
410            write!(f, "{}%", value)
411        }
412    }
413}
414
415impl fmt::Debug for Percentage1000 {
416    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
417        fmt::Display::fmt(self, f)
418    }
419}
420
421impl Percentage1000 {
422    pub fn get(&self) -> u32 {
423        self.0 / 1000
424    }
425}
426
427impl From<Percentage1000> for Percentage {
428    fn from(p: Percentage1000) -> Self {
429        Percentage(p.get())
430    }
431}
432
433impl From<Percentage> for Percentage1000 {
434    fn from(p: Percentage) -> Self {
435        Percentage1000(p.0 * 1000)
436    }
437}
438
439#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
440#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
441pub struct Range<T> {
442    pub min: T,
443    pub max: T,
444}
445
446impl<T: fmt::Display> fmt::Display for Range<T> {
447    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
448        write!(f, "{} ~ {}", self.min, self.max)
449    }
450}
451
452impl<T: fmt::Debug> fmt::Debug for Range<T> {
453    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
454        write!(f, "{:?} ~ {:?}", self.min, self.max)
455    }
456}
457
458impl<T> Range<T> {
459    pub fn range_from<U>(r: Range<U>) -> Self where T: From<U> {
460        Range {
461            min: r.min.into(),
462            max: r.max.into(),
463        }
464    }
465
466    pub fn from_scalar(v: T) -> Self where T: Clone {
467        Range {
468            min: v.clone(),
469            max: v,
470        }
471    }
472}
473
474#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
475#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Default)]
476pub struct Delta<T> {
477    pub value: T,
478    pub range: Range<T>,
479}