Expand description
This struct represents a system control.
Fields
oid: Vec<c_int>Implementations
sourceimpl Ctl
impl Ctl
sourcepub fn new(name: &str) -> Result<Self, SysctlError>
pub fn new(name: &str) -> Result<Self, SysctlError>
Construct a Ctl from the name.
This is just a wrapper around Ctl::from_str.
Returns a result containing the struct Ctl on success or a SysctlError on failure.
Example
extern crate sysctl;
use sysctl::Ctl;
let ctl = Ctl::new("kern.osrelease");If the sysctl does not exist, Err(SysctlError::NotFound) is returned.
extern crate sysctl;
use sysctl::{Ctl, SysctlError};
let ctl = Ctl::new("this.sysctl.does.not.exist");
match ctl {
Err(SysctlError::NotFound(_)) => (),
Err(_) => panic!("Wrong error type returned"),
Ok(_) => panic!("Nonexistent sysctl seems to exist"),
}sourcepub fn name(&self) -> Result<String, SysctlError>
pub fn name(&self) -> Result<String, SysctlError>
Returns a result containing the sysctl name on success, or a SysctlError on failure.
Example
let ctl = Ctl::new("kern.osrelease").expect("could not get sysctl");
assert_eq!(ctl.name().expect("could not get name"), "kern.osrelease");sourcepub fn value_type(&self) -> Result<CtlType, SysctlError>
pub fn value_type(&self) -> Result<CtlType, SysctlError>
Returns a result containing the sysctl value type on success, or a Sysctl Error on failure.
Example
let ctl = Ctl::new("kern.osrelease")
.expect("Could not get kern.osrelease sysctl");
let value_type = ctl.value_type()
.expect("Could not get kern.osrelease value type");
assert_eq!(value_type, CtlType::String);sourcepub fn description(&self) -> Result<String, SysctlError>
pub fn description(&self) -> Result<String, SysctlError>
Returns a result containing the sysctl description if success, or an Error on failure.
Example
extern crate sysctl;
use sysctl::Ctl;
fn main() {
let osrevision = sysctl::Ctl::new("kern.osrevision")
.expect("could not get kern.osrevision sysctl");
println!("Description: {:?}", osrevision.description())
}sourcepub fn value(&self) -> Result<CtlValue, SysctlError>
pub fn value(&self) -> Result<CtlValue, SysctlError>
Returns a result containing the sysctl value on success, or a SysctlError on failure.
Example
extern crate sysctl;
extern crate libc;
fn main() {
let osrevision = sysctl::Ctl::new("kern.osrevision")
.expect("could not get kern.osrevisio sysctl");
println!("Value: {:?}", osrevision.value());
}sourcepub fn value_as<T>(&self) -> Result<Box<T>, SysctlError>
pub fn value_as<T>(&self) -> Result<Box<T>, SysctlError>
A generic method that takes returns a result containing the sysctl value if success, or a SysctlError on failure.
May only be called for sysctls of type Opaque or Struct.
Example
extern crate sysctl;
extern crate libc;
use libc::c_int;
#[derive(Debug)]
#[repr(C)]
struct ClockInfo {
hz: c_int, /* clock frequency */
tick: c_int, /* micro-seconds per hz tick */
spare: c_int,
stathz: c_int, /* statistics clock frequency */
profhz: c_int, /* profiling clock frequency */
}
fn main() {
let clockrate = sysctl::Ctl::new("kern.clockrate")
.expect("could not get clockrate sysctl");
println!("{:?}", clockrate.value_as::<ClockInfo>());
}sourcepub fn set_value(&self, value: CtlValue) -> Result<CtlValue, SysctlError>
pub fn set_value(&self, value: CtlValue) -> Result<CtlValue, SysctlError>
Sets the value of a sysctl. Fetches and returns the new value if successful, or returns a SysctlError on failure.
Example
extern crate sysctl;
use sysctl::Ctl;
fn main() {
let usbdebug = Ctl::new("hw.usb.debug")
.expect("could not get hw.usb.debug control");
let set = usbdebug.set_value(sysctl::CtlValue::Int(1))
.expect("could not set value");
assert_eq!(set, sysctl::CtlValue::Int(1));
println!("hw.usb.debug: -> {:?}", set);
}sourcepub fn flags(&self) -> Result<CtlFlags, SysctlError>
pub fn flags(&self) -> Result<CtlFlags, SysctlError>
Get the flags for a sysctl.
Returns a Result containing the flags on success, or a SysctlError on failure.
Example
extern crate sysctl;
use sysctl::{Ctl, CtlFlags};
fn main() {
let osrev = Ctl::new("kern.osrevision")
.expect("could not get control");
let readable = osrev.flags()
.expect("could not get flags")
.contains(CtlFlags::RD);
assert!(readable);
}sourcepub fn info(&self) -> Result<CtlInfo, SysctlError>
pub fn info(&self) -> Result<CtlInfo, SysctlError>
Returns a Result containing the control metadata for a sysctl.
Returns a Result containing the CtlInfo struct on success, or a SysctlError on failure.
Example
extern crate sysctl;
use sysctl::{Ctl, CtlInfo};
fn main() {
let osrev = Ctl::new("kern.osrevision")
.expect("could not get control");
let info = osrev.info()
.expect("could not get info");
// kern.osrevision is not a structure.
assert_eq!(info.struct_type(), None);
}Trait Implementations
sourceimpl IntoIterator for Ctl
impl IntoIterator for Ctl
Ctl implements the IntoIterator trait to allow for easy iteration over nodes.
Example
extern crate sysctl;
use sysctl::Ctl;
let kern = Ctl::new("kern");
for ctl in kern {
let name = ctl.name().expect("could not get name");
println!("{}", name);
}