[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CANiq72n91z7heDU5MDk_=jYY8h8VJsfboevmFS0vrD-zQCKq5Q@mail.gmail.com>
Date: Tue, 20 Jun 2023 19:44:27 +0200
From: Miguel Ojeda <miguel.ojeda.sandonis@...il.com>
To: Alice Ryhl <alice@...l.io>, Gary Guo <gary@...yguo.net>
Cc: Jakub Kicinski <kuba@...nel.org>, Andrew Lunn <andrew@...n.ch>,
FUJITA Tomonori <fujita.tomonori@...il.com>, netdev@...r.kernel.org,
rust-for-linux@...r.kernel.org, aliceryhl@...gle.com
Subject: Re: [PATCH 0/5] Rust abstractions for network device drivers
On Tue, Jun 20, 2023 at 6:55 PM Alice Ryhl <alice@...l.io> wrote:
>
> We could probably have the destructor call some method that the linker
> wont be able to find, then mark the destructor with #[inline] so that
> dead-code elimination removes it if unused.
>
> (At least, in godbolt the inline marker was necessary for the destructor
> to get removed when not used.)
Yeah, and we could use `build_assert!(false);` to ensure we don't ever
call it (by users, or by the the custom destructor methods) -- it
seems to work [1], but I am Cc'ing Gary in case he has some concerns
(IIRC we discussed this in the past; I may be forgetting an issue with
this).
Cheers,
Miguel
[1]
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index c20b37e88ab2..7c313c75ff14 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -261,3 +261,28 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
// instead of `!`. See
<https://github.com/rust-lang/rust-bindgen/issues/2094>.
loop {}
}
+
+/// Custom destructor example.
+pub struct S;
+
+impl S {
+ /// Custom destructor 1.
+ pub fn close1(self) {
+ pr_info!("close1");
+ core::mem::forget(self); // If commented out, it is a build error.
+ }
+
+ /// Custom destructor 2.
+ pub fn close2(self) {
+ pr_info!("close2");
+ core::mem::forget(self); // If commented out, it is a build error.
+ }
+}
+
+impl Drop for S {
+ #[inline(always)]
+ fn drop(&mut self) {
+ // This should never be called.
+ build_assert!(false);
+ }
+}
diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs
index 54de32b78cec..f07064ff6341 100644
--- a/samples/rust/rust_minimal.rs
+++ b/samples/rust/rust_minimal.rs
@@ -3,6 +3,7 @@
//! Rust minimal sample.
use kernel::prelude::*;
+use kernel::S;
module! {
type: RustMinimal,
@@ -26,6 +27,12 @@ impl kernel::Module for RustMinimal {
numbers.try_push(108)?;
numbers.try_push(200)?;
+ let _s = S;
+ _s.close1(); // If commented out, it is a build error.
+
+ let _s = S;
+ _s.close2(); // If commented out, it is a build error.
+
Ok(RustMinimal { numbers })
}
}
Powered by blists - more mailing lists