use std::io::Write;
use std::path::PathBuf;
use crate::error::ErrorKind::*;
use crate::error::*;
use crate::read_string_from;
use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
#[derive(Debug, Clone)]
pub struct RdmaController {
base: PathBuf,
path: PathBuf,
}
impl ControllerInternal for RdmaController {
fn control_type(&self) -> Controllers {
Controllers::Rdma
}
fn get_path(&self) -> &PathBuf {
&self.path
}
fn get_path_mut(&mut self) -> &mut PathBuf {
&mut self.path
}
fn get_base(&self) -> &PathBuf {
&self.base
}
fn apply(&self, _res: &Resources) -> Result<()> {
Ok(())
}
}
impl ControllIdentifier for RdmaController {
fn controller_type() -> Controllers {
Controllers::Rdma
}
}
impl<'a> From<&'a Subsystem> for &'a RdmaController {
fn from(sub: &'a Subsystem) -> &'a RdmaController {
unsafe {
match sub {
Subsystem::Rdma(c) => c,
_ => {
assert_eq!(1, 0);
let v = std::mem::MaybeUninit::uninit();
v.assume_init()
}
}
}
}
}
impl RdmaController {
pub fn new(root: PathBuf) -> Self {
Self {
base: root.clone(),
path: root,
}
}
pub fn current(&self) -> Result<String> {
self.open_path("rdma.current", false)
.and_then(read_string_from)
}
pub fn set_max(&self, max: &str) -> Result<()> {
self.open_path("rdma.max", true).and_then(|mut file| {
file.write_all(max.as_ref()).map_err(|e| {
Error::with_cause(WriteFailed("rdma.max".to_string(), max.to_string()), e)
})
})
}
}