rust: types: Make Opaque::get const
PATCH 1/1 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.
--- rust/kernel/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/types.rs +1 -1
@@ -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 >() }
Signature · 1 line
2.44.0
Quoted 11 lines · Boqun Feng
To support a potential usage:
static foo: Opaque<Foo> = ..; // Or defined in an extern block. ... fn bar() { let ptr = foo.get(); }
Quoted 7 lines
Opaque::getneed to beconst, otherwise compiler will complain because calls on statics are limited to const functions.Also
Opaque::getshould be naturallyconstsince it's a composition of twoconstfunctions:UnsafeCell::getandptr::cast.Signed-off-by Boqun Feng <[email protected]>
Quoted 11 lines · Boqun Feng
To support a potential usage:
static foo: Opaque<Foo> = ..; // Or defined in an extern block. ... fn bar() { let ptr = foo.get(); }
Quoted 7 lines
Opaque::getneed to beconst, otherwise compiler will complain because calls on statics are limited to const functions.Also
Opaque::getshould be naturallyconstsince it's a composition of twoconstfunctions:UnsafeCell::getandptr::cast.Signed-off-by Boqun Feng <[email protected]>
Quoted 13 lines · Boqun Feng
To support a potential usage:
static foo: Opaque<Foo> = ..; // Or defined in an extern block. ... fn bar() { let ptr = foo.get(); }
Opaque::getneed to beconst, otherwise compiler will complain because calls on statics are limited to const functions.
Quoted 7 lines
Also
Opaque::getshould be naturallyconstsince it's a composition of twoconstfunctions:UnsafeCell::getandptr::cast.Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 2 lines
Cheers, Benno
Miguel Ojeda
AppliedQuoted 11 lines · Boqun Feng
To support a potential usage:
static foo: Opaque<Foo> = ..; // Or defined in an extern block. ... fn bar() { let ptr = foo.get(); }
Quoted 7 lines
Opaque::getneed to beconst, otherwise compiler will complain because calls on statics are limited to const functions.Also
Opaque::getshould be naturallyconstsince it's a composition of twoconstfunctions:UnsafeCell::getandptr::cast.Signed-off-by Boqun Feng <[email protected]>
Applied to rust-next -- thanks everyone!
Cheers,
Miguel