[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH 3/7] Rust: add a slightly more idiomatic wrapper around comma
From: |
Daniel Axtens |
Subject: |
[RFC PATCH 3/7] Rust: add a slightly more idiomatic wrapper around command handling |
Date: |
Tue, 24 Aug 2021 23:32:39 +1000 |
Add a way to do registering and deregistering of commands that is just
a tiny bit more "Rust-y".
Signed-off-by: Daniel Axtens <dja@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
- [RFC PATCH 0/7] Support writing grub modules in Rust, Daniel Axtens, 2021/08/24
- [RFC PATCH 1/7] emu: support grub_memalign, Daniel Axtens, 2021/08/24
- [RFC PATCH 2/7] Rust: module build infrastructure, Daniel Axtens, 2021/08/24
- [RFC PATCH 3/7] Rust: add a slightly more idiomatic wrapper around command handling,
Daniel Axtens <=
- [RFC PATCH 4/7] Rust: add the rust_hello module, Daniel Axtens, 2021/08/24
- [RFC PATCH 5/7] powerpc: Support Rust, Daniel Axtens, 2021/08/24
- [RFC PATCH 6/7] x86_64-efi: Support Rust, Daniel Axtens, 2021/08/24
- [RFC PATCH 7/7] arm64-efi: Support Rust, Daniel Axtens, 2021/08/24
- Re: [RFC PATCH 0/7] Support writing grub modules in Rust, Heinrich Schuchardt, 2021/08/25