Add a way to do registering and deregistering of commands that is just a tiny bit more "Rust-y".
Signed-off-by: Daniel Axtens <d...@axtens.net> --- grub-core/Makefile.am | 2 +- grub-core/lib/rust/grub/src/command.rs | 50 ++++++++++++++++++++++++++ grub-core/lib/rust/grub/src/lib.rs | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 grub-core/lib/rust/grub/src/command.rs diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am index 71c1444bcf7d..b565803efb05 100644 --- a/grub-core/Makefile.am +++ b/grub-core/Makefile.am @@ -38,7 +38,7 @@ lib/rust/grub/src/bindings.rs: lib/rust/bindings.h CLEANFILES += lib/rust/grub/src/bindings.rs -COMMON_RUSTSOURCES = lib/rust/grub/src/bindings.rs lib/rust/grub/src/lib.rs +COMMON_RUSTSOURCES = lib/rust/grub/src/bindings.rs lib/rust/grub/src/lib.rs lib/rust/grub/src/command.rs else COMMON_RUSTSOURCES = endif diff --git a/grub-core/lib/rust/grub/src/command.rs b/grub-core/lib/rust/grub/src/command.rs new file mode 100644 index 000000000000..e96b0ad78608 --- /dev/null +++ b/grub-core/lib/rust/grub/src/command.rs @@ -0,0 +1,50 @@ +extern crate alloc; +use crate::bindings; +use alloc::string::String; +use cty; + +pub type GrubCommandFunc = unsafe extern "C" fn( + cmd: *mut bindings::grub_command, + argc: cty::c_int, + argv: *mut *mut cty::c_char, +) -> bindings::grub_err_t; + +pub struct GrubCommand { + grub_command: bindings::grub_command_t, +} + +impl GrubCommand { + pub fn new( + name: &'static str, + func: GrubCommandFunc, + summary: &'static str, + description: &'static str, + ) -> GrubCommand { + let grub_command = register_command_prio(name, func, summary, description, 0); + GrubCommand { grub_command } + } +} + +impl Drop for GrubCommand { + fn drop(&mut self) { + unsafe { bindings::grub_unregister_command(self.grub_command) }; + } +} + +fn register_command_prio( + name: &'static str, + func: GrubCommandFunc, + summary: &'static str, + description: &'static str, + prio: cty::c_int, +) -> bindings::grub_command_t { + unsafe { + bindings::grub_register_command_prio( + name.as_ptr() as *const _, + Some(func), + summary.as_ptr() as *const _, + description.as_ptr() as *const _, + prio, + ) + } +} diff --git a/grub-core/lib/rust/grub/src/lib.rs b/grub-core/lib/rust/grub/src/lib.rs index c34f332c88af..02411292ba12 100644 --- a/grub-core/lib/rust/grub/src/lib.rs +++ b/grub-core/lib/rust/grub/src/lib.rs @@ -6,6 +6,7 @@ #![feature(alloc_error_handler)] pub mod bindings; +pub mod command; use bindings::grub_size_t; use core::alloc::GlobalAlloc; use core::alloc::Layout; -- 2.30.2 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel