Skip to content

Commit

Permalink
rust: types: Make Opaque::get const
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]>
Reviewed-by: Wedson Almeida Filho <[email protected]>
Reviewed-by: Benno Lossin <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
fbq authored and ojeda committed May 5, 2024
1 parent 2c10928 commit be2ca1e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
}

Expand Down

0 comments on commit be2ca1e

Please sign in to comment.