rust: types: Make Opaque::get const
authorBoqun Feng <boqun.feng@gmail.com>
Mon, 1 Apr 2024 21:45:36 +0000 (14:45 -0700)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 5 May 2024 11:19:37 +0000 (13:19 +0200)
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>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Wedson Almeida Filho <walmeida@microsoft.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Link: https://lore.kernel.org/r/20240401214543.1242286-1-boqun.feng@gmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/types.rs

index 8fad61268465a717adfaeff119cfdc107f8ccf63..2e7c9008621f5dce10437ef577ebb2ea3d9d2d7f 100644 (file)
@@ -270,7 +270,7 @@ impl<T> Opaque<T> {
     }
 
     /// 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>()
     }