use std::cell::UnsafeCell;
use crate::bindings::{
vlib_cli_command_t, vlib_helper_get_global_main, vlib_helper_remove_cli_command,
};
pub struct CommandRegistration(UnsafeCell<vlib_cli_command_t>);
impl CommandRegistration {
pub const fn new(command: vlib_cli_command_t) -> Self {
Self(UnsafeCell::new(command))
}
pub unsafe fn register(&'static self) {
unsafe {
let vgm = vlib_helper_get_global_main();
let cm = &mut (*vgm).cli_main;
let reg = self.0.get();
(*reg).next_cli_command = cm.cli_command_registrations;
cm.cli_command_registrations = reg;
}
}
pub unsafe fn unregister(&self) {
unsafe {
let vgm = vlib_helper_get_global_main();
let cm = &mut (*vgm).cli_main;
vlib_helper_remove_cli_command(cm, self.0.get());
}
}
}
unsafe impl Send for CommandRegistration {}
unsafe impl Sync for CommandRegistration {}
#[cfg(test)]
mod tests {
use crate::{bindings::vlib_cli_command_t, vlib::cli::CommandRegistration};
#[test]
fn test_cmd_reg() {
let command = std::hint::black_box(CommandRegistration::new(vlib_cli_command_t::default()));
let command = Box::new(command);
let command = Box::into_raw(command);
unsafe {
(*command).register();
(*command).unregister();
let _ = Box::from_raw(command);
}
}
}