2024-04-01 21:46:55

by Boqun Feng

[permalink] [raw]
Subject: [PATCH] rust: types: Make Opaque::get const

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]>
---
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



2024-04-01 23:22:20

by Wedson Almeida Filho

[permalink] [raw]
Subject: Re: [PATCH] rust: types: Make Opaque::get const

On Mon, 1 Apr 2024 at 18:46, Boqun Feng <[email protected]> wrote:
>
> 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: Wedson Almeida Filho <[email protected]>

2024-04-02 09:46:34

by Alice Ryhl

[permalink] [raw]
Subject: Re: [PATCH] rust: types: Make Opaque::get const

On Mon, Apr 1, 2024 at 11:46 PM Boqun Feng <[email protected]> wrote:
>
> 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]>

2024-04-02 12:50:35

by Benno Lossin

[permalink] [raw]
Subject: Re: [PATCH] rust: types: Make Opaque::get const

On 01.04.24 23:45, Boqun Feng wrote:
> 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]>
> ---
> rust/kernel/types.rs | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Benno Lossin <[email protected]>

--
Cheers,
Benno


2024-05-05 22:28:10

by Miguel Ojeda

[permalink] [raw]
Subject: Re: [PATCH] rust: types: Make Opaque::get const

On Mon, Apr 1, 2024 at 11:46 PM Boqun Feng <[email protected]> wrote:
>
> 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]>

Applied to `rust-next` -- thanks everyone!

Cheers,
Miguel