use std::{ffi::OsString, io::Result};
use super::{
util::{FunctionDir, Status},
Function, Handle,
};
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct LoopbackBuilder {
pub qlen: Option<u32>,
pub bulk_buflen: Option<u32>,
}
impl LoopbackBuilder {
#[must_use]
pub fn build(self) -> (Loopback, Handle) {
let dir = FunctionDir::new();
(Loopback { dir: dir.clone() }, Handle::new(LoopbackFunction { builder: self, dir }))
}
}
#[derive(Debug)]
struct LoopbackFunction {
builder: LoopbackBuilder,
dir: FunctionDir,
}
impl Function for LoopbackFunction {
fn driver(&self) -> OsString {
"Loopback".into()
}
fn dir(&self) -> FunctionDir {
self.dir.clone()
}
fn register(&self) -> Result<()> {
if let Some(qlen) = self.builder.qlen {
self.dir.write("qlen", qlen.to_string())?;
}
if let Some(bulk_buflen) = self.builder.bulk_buflen {
self.dir.write("bulk_buflen", bulk_buflen.to_string())?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct Loopback {
dir: FunctionDir,
}
impl Loopback {
pub fn new() -> (Loopback, Handle) {
Self::builder().build()
}
pub fn builder() -> LoopbackBuilder {
LoopbackBuilder::default()
}
pub fn status(&self) -> Status {
self.dir.status()
}
}