rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: types: Make Opaque::get const
@ 2024-04-01 21:45 Boqun Feng
  2024-04-01 23:21 ` Wedson Almeida Filho
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Boqun Feng @ 2024-04-01 21:45 UTC (permalink / raw)
  To: rust-for-linux, linux-kernel
  Cc: Boqun Feng, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Martin Rodriguez Reboredo, Obei Sideg

To support a potential usage:

    static foo: Opaque<Foo> = ..; // Or defined in an extern block.

    ...

    fn bar() {
        let ptr = foo.get();
    }

`Opaque::get` need to be `const`, otherwise compiler will complain
because calls on statics are limited to const functions.

Also `Opaque::get` should be naturally `const` since it's a composition
of two `const` functions: `UnsafeCell::get` and `ptr::cast`.

Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
 rust/kernel/types.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 0949f9971074..25953c8f8acf 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -315,7 +315,7 @@ pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
     }
 
     /// Returns a raw pointer to the opaque data.
-    pub fn get(&self) -> *mut T {
+    pub const fn get(&self) -> *mut T {
         UnsafeCell::get(&self.value).cast::<T>()
     }
 
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-05-05 22:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-01 21:45 [PATCH] rust: types: Make Opaque::get const Boqun Feng
2024-04-01 23:21 ` Wedson Almeida Filho
2024-04-02  9:46 ` Alice Ryhl
2024-04-02 12:49 ` Benno Lossin
2024-05-05 22:26 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).