Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accessor methods for destructuring CtlValue #57

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## [ Unreleased ] - ReleaseDate
### Added

- Added accessor methods to destructure `CtlValue`.

## [0.5.2] - 2022-08-16
### Changed
- CI minimum version test failing. Adjust versions to fix.
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ targets = [
]

[dependencies]
enum-as-inner = "0.5.1"
libc = "^0.2.34"
byteorder = "^1.4"
thiserror = "^1.0.32"
Expand Down
12 changes: 2 additions & 10 deletions examples/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@ fn main() {
let d = ctl.description().expect("could not get sysctl description");
println!("Description: {:?}", d);

let val_enum = ctl.value().expect("could not get sysctl value");
println!("ClockInfo raw data (byte array): {:?}", val_enum);

if let sysctl::CtlValue::Struct(val) = val_enum {
// Make sure we got correct data size
assert_eq!(std::mem::size_of::<ClockInfo>(), val.len());
let val_ptr: *const u8 = val.as_ptr();
let struct_ptr: *const ClockInfo = val_ptr as *const ClockInfo;
let struct_ref: &ClockInfo = unsafe { &*struct_ptr };
println!("{:?}", struct_ref);
if let Ok(s) = ctl.value_as::<ClockInfo>() {
println!("{:?}", s);
}
}

Expand Down
17 changes: 7 additions & 10 deletions examples/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ fn main() {

let val_enum = ctl.value().expect("could not get value");

if let sysctl::CtlValue::Temperature(val) = val_enum {
println!(
"Temperature: {:.2}K, {:.2}F, {:.2}C",
val.kelvin(),
val.fahrenheit(),
val.celsius()
);
} else {
panic!("Error, not a temperature ctl!")
}
let temp = val_enum.as_temperature().unwrap();
println!(
"Temperature: {:.2}K, {:.2}F, {:.2}C",
temp.kelvin(),
temp.fahrenheit(),
temp.celsius()
);
}

#[cfg(not(target_os = "freebsd"))]
Expand Down
11 changes: 7 additions & 4 deletions src/ctl_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

#[cfg(target_os = "freebsd")]
use temperature::Temperature;
use enum_as_inner::EnumAsInner;

/// An Enum that holds all values returned by sysctl calls.
/// Extract inner value with `if let` or `match`.
/// Extract inner value with accessors like `as_int()`.
///
/// # Example
///
/// ```
/// # use sysctl::Sysctl;
/// if let Ok(ctl) = sysctl::Ctl::new("kern.osrevision") {
/// if let Ok(sysctl::CtlValue::Int(val)) = ctl.value() {
/// println!("Value: {}", val);
/// if let Ok(r) = ctl.value() {
/// if let Some(val) = r.as_int() {
/// println!("Value: {}", val);
/// }
/// }
/// }
/// ```
#[derive(Debug, PartialEq, PartialOrd)]
#[derive(Debug, EnumAsInner, PartialEq, PartialOrd)]
pub enum CtlValue {
None,
Node(Vec<u8>),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#[macro_use]
extern crate bitflags;
extern crate byteorder;
extern crate enum_as_inner;
extern crate libc;
extern crate thiserror;

Expand Down
32 changes: 12 additions & 20 deletions src/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ use std::f32;
/// ```
/// # use sysctl::Sysctl;
/// if let Ok(ctl) = sysctl::Ctl::new("dev.cpu.0.temperature") {
/// if let Ok(sysctl::CtlValue::Temperature(val)) = ctl.value() {
/// println!("Temperature: {:.2}K, {:.2}F, {:.2}C",
/// val.kelvin(),
/// val.fahrenheit(),
/// val.celsius());
/// } else {
/// panic!("Error, not a temperature ctl!")
/// }
/// let val = ctl.value().unwrap();
/// let temp = val.as_temperature().unwrap();
/// println!("Temperature: {:.2}K, {:.2}F, {:.2}C",
/// temp.kelvin(),
/// temp.fahrenheit(),
/// temp.celsius());
/// }
/// ```
/// Not available on MacOS
Expand Down Expand Up @@ -92,13 +90,10 @@ mod tests_freebsd {
.expect("Error parsing value to byte array");

let t = super::temperature(&info, &val).unwrap();
if let crate::CtlValue::Temperature(tt) = t {
assert!(tt.kelvin() - 333.0 < 0.1);
assert!(tt.celsius() - 59.85 < 0.1);
assert!(tt.fahrenheit() - 139.73 < 0.1);
} else {
assert!(false);
}
let tt = t.as_temperature().unwrap();
assert!(tt.kelvin() - 333.0 < 0.1);
assert!(tt.celsius() - 59.85 < 0.1);
assert!(tt.fahrenheit() - 139.73 < 0.1);
}

#[test]
Expand All @@ -114,10 +109,7 @@ mod tests_freebsd {
.expect("Error parsing value to byte array");

let t = super::temperature(&info, &val).unwrap();
if let crate::CtlValue::Temperature(tt) = t {
assert!(tt.kelvin() - 333.0 < 0.1);
} else {
assert!(false);
}
let tt = t.as_temperature().unwrap();
assert!(tt.kelvin() - 333.0 < 0.1);
}
}