pub trait Sysctl {
// Required methods
fn new(name: &str) -> Result<Self, SysctlError>
where Self: Sized;
fn new_with_type(
name: &str,
ctl_type: CtlType,
fmt: &str,
) -> Result<Self, SysctlError>
where Self: Sized;
fn name(&self) -> Result<String, SysctlError>;
fn value_type(&self) -> Result<CtlType, SysctlError>;
fn description(&self) -> Result<String, SysctlError>;
fn value(&self) -> Result<CtlValue, SysctlError>;
fn value_as<T>(&self) -> Result<Box<T>, SysctlError>;
fn value_string(&self) -> Result<String, SysctlError>;
fn set_value(&self, value: CtlValue) -> Result<CtlValue, SysctlError>;
fn set_value_string(&self, value: &str) -> Result<String, SysctlError>;
fn flags(&self) -> Result<CtlFlags, SysctlError>;
fn info(&self) -> Result<CtlInfo, SysctlError>;
}
Required Methods§
Sourcefn new(name: &str) -> Result<Self, SysctlError>where
Self: Sized,
fn new(name: &str) -> Result<Self, SysctlError>where
Self: Sized,
Construct a Ctl from the name.
Returns a result containing the struct Ctl on success or a SysctlError on failure.
§Example
let ctl = sysctl::Ctl::new("kern.ostype");
If the sysctl does not exist, Err(SysctlError::NotFound)
is returned.
let ctl = sysctl::Ctl::new("this.sysctl.does.not.exist");
match ctl {
Err(sysctl::SysctlError::NotFound(_)) => (),
Err(e) => panic!(format!("Wrong error type returned: {:?}", e)),
Ok(_) => panic!("Nonexistent sysctl seems to exist"),
}
Sourcefn new_with_type(
name: &str,
ctl_type: CtlType,
fmt: &str,
) -> Result<Self, SysctlError>where
Self: Sized,
fn new_with_type(
name: &str,
ctl_type: CtlType,
fmt: &str,
) -> Result<Self, SysctlError>where
Self: Sized,
Construct a Ctl from the name, type and format.
Returns a result containing the struct Ctl on success or a SysctlError on failure.
§Example
let ctl = sysctl::Ctl::new_with_type("kern.ostype", CtlType::String, "");
If the sysctl does not exist, Err(SysctlError::NotFound)
is returned.
let ctl = sysctl::Ctl::new_with_type("this.sysctl.does.not.exist", CtlType::String, "");
match ctl {
Err(sysctl::SysctlError::NotFound(_)) => (),
Err(e) => panic!("Wrong error type returned: {:?}", e),
Ok(_) => panic!("Nonexistent sysctl seems to exist"),
}
Sourcefn name(&self) -> Result<String, SysctlError>
fn name(&self) -> Result<String, SysctlError>
Returns a result containing the sysctl name on success, or a SysctlError on failure.
§Example
if let Ok(ctl) = sysctl::Ctl::new("kern.ostype") {
assert_eq!(ctl.name().unwrap(), "kern.ostype");
}
Sourcefn value_type(&self) -> Result<CtlType, SysctlError>
fn value_type(&self) -> Result<CtlType, SysctlError>
Returns a result containing the sysctl value type on success, or a Sysctl Error on failure.
§Example
if let Ok(ctl) = sysctl::Ctl::new("kern.ostype") {
let value_type = ctl.value_type().unwrap();
assert_eq!(value_type, sysctl::CtlType::String);
}
Sourcefn description(&self) -> Result<String, SysctlError>
fn description(&self) -> Result<String, SysctlError>
Returns a result containing the sysctl description if success, or an Error on failure.
§Example
if let Ok(ctl) = sysctl::Ctl::new("kern.ostype") {
println!("Description: {:?}", ctl.description())
}
Sourcefn value(&self) -> Result<CtlValue, SysctlError>
fn value(&self) -> Result<CtlValue, SysctlError>
Returns a result containing the sysctl value on success, or a SysctlError on failure.
§Example
if let Ok(ctl) = sysctl::Ctl::new("kern.ostype") {
println!("Value: {:?}", ctl.value());
}
Sourcefn value_as<T>(&self) -> Result<Box<T>, SysctlError>
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
#[derive(Debug)]
#[repr(C)]
struct ClockInfo {
hz: libc::c_int, /* clock frequency */
tick: libc::c_int, /* micro-seconds per hz tick */
spare: libc::c_int,
stathz: libc::c_int, /* statistics clock frequency */
profhz: libc::c_int, /* profiling clock frequency */
}
if let Ok(ctl) = sysctl::Ctl::new("kern.clockrate") {
println!("{:?}", ctl.value_as::<ClockInfo>());
}
Sourcefn value_string(&self) -> Result<String, SysctlError>
fn value_string(&self) -> Result<String, SysctlError>
Returns a result containing the sysctl value as String on success, or a SysctlError on failure.
§Example
if let Ok(ctl) = sysctl::Ctl::new("kern.osrevision") {
println!("Value: {:?}", ctl.value_string());
}
Sourcefn set_value(&self, value: CtlValue) -> Result<CtlValue, SysctlError>
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
use sysctl::Sysctl;
fn main() {
if unsafe { libc::getuid() } == 0 {
if let Ok(ctl) = sysctl::Ctl::new("hw.usb.debug") {
let org = ctl.value().unwrap();
let set = ctl.set_value(sysctl::CtlValue::Int(1)).unwrap();
assert_eq!(set, sysctl::CtlValue::Int(1));
ctl.set_value(org).unwrap();
}
}
}
Sourcefn set_value_string(&self, value: &str) -> Result<String, SysctlError>
fn set_value_string(&self, value: &str) -> Result<String, SysctlError>
Sets the value of a sysctl with input as string. Fetches and returns the new value if successful, or returns a SysctlError on failure.
§Example
use sysctl::Sysctl;
fn main() {
if unsafe { libc::getuid() } == 0 {
if let Ok(ctl) = sysctl::Ctl::new("hw.usb.debug") {
let org = ctl.value_string().unwrap();
let set = ctl.set_value_string("1");
println!("hw.usb.debug: -> {:?}", set);
ctl.set_value_string(&org).unwrap();
}
}
}
Sourcefn flags(&self) -> Result<CtlFlags, SysctlError>
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
if let Ok(ctl) = sysctl::Ctl::new("kern.ostype") {
let readable = ctl.flags().unwrap().contains(sysctl::CtlFlags::RD);
assert!(readable);
}
Sourcefn info(&self) -> Result<CtlInfo, SysctlError>
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
use sysctl::Sysctl;
fn main() {
if let Ok(ctl) = sysctl::Ctl::new("kern.osrevision") {
let info = ctl.info().unwrap();
// kern.osrevision is not a structure.
assert_eq!(info.struct_type(), None);
}
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.