* [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`
@ 2023-06-14 11:53 Alice Ryhl
2023-06-14 14:27 ` Benno Lossin
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Alice Ryhl @ 2023-06-14 11:53 UTC (permalink / raw)
To: rust-for-linux
Cc: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl,
linux-kernel, patches
When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use
`UnsafeCell` as the outer type. Intuitively, this is because a
`MaybeUninit<T>` might not contain a `T`, but we always want the effect
of the `UnsafeCell`, even if the inner value is uninitialized.
Now, strictly speaking, this doesn't really make a difference. The
compiler will always apply the `UnsafeCell` effect even if the inner
value is uninitialized. But I think we should follow the convention
here.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
rust/kernel/types.rs | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 1e5380b16ed5..fb41635f1e1f 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -224,17 +224,17 @@ fn drop(&mut self) {
///
/// This is meant to be used with FFI objects that are never interpreted by Rust code.
#[repr(transparent)]
-pub struct Opaque<T>(MaybeUninit<UnsafeCell<T>>);
+pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
impl<T> Opaque<T> {
/// Creates a new opaque value.
pub const fn new(value: T) -> Self {
- Self(MaybeUninit::new(UnsafeCell::new(value)))
+ Self(UnsafeCell::new(MaybeUninit::new(value)))
}
/// Creates an uninitialised value.
pub const fn uninit() -> Self {
- Self(MaybeUninit::uninit())
+ Self(UnsafeCell::new(MaybeUninit::uninit()))
}
/// Creates a pin-initializer from the given initializer closure.
@@ -258,7 +258,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 {
- UnsafeCell::raw_get(self.0.as_ptr())
+ UnsafeCell::get(&self.0).cast::<T>()
}
/// Gets the value behind `this`.
@@ -266,7 +266,7 @@ pub fn get(&self) -> *mut T {
/// This function is useful to get access to the value without creating intermediate
/// references.
pub const fn raw_get(this: *const Self) -> *mut T {
- UnsafeCell::raw_get(this.cast::<UnsafeCell<T>>())
+ UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
}
}
base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3
--
2.41.0.162.gfafddb0af9-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`
2023-06-14 11:53 [PATCH] rust: make `UnsafeCell` the outer type in `Opaque` Alice Ryhl
@ 2023-06-14 14:27 ` Benno Lossin
2023-06-14 14:37 ` Alice Ryhl
2023-06-14 15:30 ` Martin Rodriguez Reboredo
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Benno Lossin @ 2023-06-14 14:27 UTC (permalink / raw)
To: Alice Ryhl
Cc: rust-for-linux, Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, linux-kernel,
patches
On 14.06.23 13:53, Alice Ryhl wrote:
> When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use
> `UnsafeCell` as the outer type. Intuitively, this is because a
> `MaybeUninit<T>` might not contain a `T`, but we always want the effect
> of the `UnsafeCell`, even if the inner value is uninitialized.
>
> Now, strictly speaking, this doesn't really make a difference. The
> compiler will always apply the `UnsafeCell` effect even if the inner
> value is uninitialized. But I think we should follow the convention
> here.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Small comment below, but I think it is fine the way it is.
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
> ---
> rust/kernel/types.rs | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index 1e5380b16ed5..fb41635f1e1f 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -224,17 +224,17 @@ fn drop(&mut self) {
> ///
> /// This is meant to be used with FFI objects that are never interpreted by Rust code.
> #[repr(transparent)]
> -pub struct Opaque<T>(MaybeUninit<UnsafeCell<T>>);
> +pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
>
> impl<T> Opaque<T> {
> /// Creates a new opaque value.
> pub const fn new(value: T) -> Self {
> - Self(MaybeUninit::new(UnsafeCell::new(value)))
> + Self(UnsafeCell::new(MaybeUninit::new(value)))
> }
>
> /// Creates an uninitialised value.
> pub const fn uninit() -> Self {
> - Self(MaybeUninit::uninit())
> + Self(UnsafeCell::new(MaybeUninit::uninit()))
> }
>
> /// Creates a pin-initializer from the given initializer closure.
> @@ -258,7 +258,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 {
> - UnsafeCell::raw_get(self.0.as_ptr())
> + UnsafeCell::get(&self.0).cast::<T>()
Is there a reason you don't do `self.0.get().cast::<T>()`?
--
Cheers,
Benno
> }
>
> /// Gets the value behind `this`.
> @@ -266,7 +266,7 @@ pub fn get(&self) -> *mut T {
> /// This function is useful to get access to the value without creating intermediate
> /// references.
> pub const fn raw_get(this: *const Self) -> *mut T {
> - UnsafeCell::raw_get(this.cast::<UnsafeCell<T>>())
> + UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
> }
> }
>
>
> base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3
> --
> 2.41.0.162.gfafddb0af9-goog
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`
2023-06-14 14:27 ` Benno Lossin
@ 2023-06-14 14:37 ` Alice Ryhl
0 siblings, 0 replies; 7+ messages in thread
From: Alice Ryhl @ 2023-06-14 14:37 UTC (permalink / raw)
To: Benno Lossin
Cc: rust-for-linux, Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, linux-kernel,
patches
On 6/14/23 16:27, Benno Lossin wrote:>> @@ -258,7 +258,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 {
>> - UnsafeCell::raw_get(self.0.as_ptr())
>> + UnsafeCell::get(&self.0).cast::<T>()
>
> Is there a reason you don't do `self.0.get().cast::<T>()`?
>
Not really. I just modified what was already there.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`
2023-06-14 11:53 [PATCH] rust: make `UnsafeCell` the outer type in `Opaque` Alice Ryhl
2023-06-14 14:27 ` Benno Lossin
@ 2023-06-14 15:30 ` Martin Rodriguez Reboredo
2023-06-14 18:42 ` Gary Guo
2023-08-09 23:21 ` Miguel Ojeda
3 siblings, 0 replies; 7+ messages in thread
From: Martin Rodriguez Reboredo @ 2023-06-14 15:30 UTC (permalink / raw)
To: Alice Ryhl, rust-for-linux
Cc: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, linux-kernel,
patches
On 6/14/23 08:53, Alice Ryhl wrote:
> When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use
> `UnsafeCell` as the outer type. Intuitively, this is because a
> `MaybeUninit<T>` might not contain a `T`, but we always want the effect
> of the `UnsafeCell`, even if the inner value is uninitialized.
>
> Now, strictly speaking, this doesn't really make a difference. The
> compiler will always apply the `UnsafeCell` effect even if the inner
> value is uninitialized. But I think we should follow the convention
> here.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> [...]
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`
2023-06-14 11:53 [PATCH] rust: make `UnsafeCell` the outer type in `Opaque` Alice Ryhl
2023-06-14 14:27 ` Benno Lossin
2023-06-14 15:30 ` Martin Rodriguez Reboredo
@ 2023-06-14 18:42 ` Gary Guo
2023-06-14 19:04 ` Benno Lossin
2023-08-09 23:21 ` Miguel Ojeda
3 siblings, 1 reply; 7+ messages in thread
From: Gary Guo @ 2023-06-14 18:42 UTC (permalink / raw)
To: Alice Ryhl
Cc: rust-for-linux, Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor,
Boqun Feng, Björn Roy Baron, Benno Lossin, linux-kernel,
patches
On Wed, 14 Jun 2023 11:53:28 +0000
Alice Ryhl <aliceryhl@google.com> wrote:
> When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use
> `UnsafeCell` as the outer type. Intuitively, this is because a
> `MaybeUninit<T>` might not contain a `T`, but we always want the effect
> of the `UnsafeCell`, even if the inner value is uninitialized.
>
> Now, strictly speaking, this doesn't really make a difference. The
> compiler will always apply the `UnsafeCell` effect even if the inner
> value is uninitialized. But I think we should follow the convention
> here.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
> ---
> rust/kernel/types.rs | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index 1e5380b16ed5..fb41635f1e1f 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -224,17 +224,17 @@ fn drop(&mut self) {
> ///
> /// This is meant to be used with FFI objects that are never interpreted by Rust code.
> #[repr(transparent)]
> -pub struct Opaque<T>(MaybeUninit<UnsafeCell<T>>);
> +pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
>
> impl<T> Opaque<T> {
> /// Creates a new opaque value.
> pub const fn new(value: T) -> Self {
> - Self(MaybeUninit::new(UnsafeCell::new(value)))
> + Self(UnsafeCell::new(MaybeUninit::new(value)))
> }
>
> /// Creates an uninitialised value.
> pub const fn uninit() -> Self {
> - Self(MaybeUninit::uninit())
> + Self(UnsafeCell::new(MaybeUninit::uninit()))
> }
>
> /// Creates a pin-initializer from the given initializer closure.
> @@ -258,7 +258,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 {
> - UnsafeCell::raw_get(self.0.as_ptr())
> + UnsafeCell::get(&self.0).cast::<T>()
> }
>
> /// Gets the value behind `this`.
> @@ -266,7 +266,7 @@ pub fn get(&self) -> *mut T {
> /// This function is useful to get access to the value without creating intermediate
> /// references.
> pub const fn raw_get(this: *const Self) -> *mut T {
> - UnsafeCell::raw_get(this.cast::<UnsafeCell<T>>())
> + UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
This can just be `this.cast_mut().cast()` since all types involved are
transparent.
> }
> }
>
>
> base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`
2023-06-14 18:42 ` Gary Guo
@ 2023-06-14 19:04 ` Benno Lossin
0 siblings, 0 replies; 7+ messages in thread
From: Benno Lossin @ 2023-06-14 19:04 UTC (permalink / raw)
To: Gary Guo
Cc: Alice Ryhl, rust-for-linux, Miguel Ojeda, Wedson Almeida Filho,
Alex Gaynor, Boqun Feng, Björn Roy Baron, linux-kernel,
patches
> > /// Gets the value behind `this`.
> > @@ -266,7 +266,7 @@ pub fn get(&self) -> *mut T {
> > /// This function is useful to get access to the value without creating intermediate
> > /// references.
> > pub const fn raw_get(this: *const Self) -> *mut T {
> > - UnsafeCell::raw_get(this.cast::<UnsafeCell<T>>())
> > + UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
>
>
> This can just be `this.cast_mut().cast()` since all types involved are
> transparent.
I would advise against that, see [1]. It would be bad for people to
assume that one is always allowed to do that. I also like it explicit here.
[1]: https://github.com/rust-lang/unsafe-code-guidelines/issues/281
--
Cheers,
Benno
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`
2023-06-14 11:53 [PATCH] rust: make `UnsafeCell` the outer type in `Opaque` Alice Ryhl
` (2 preceding siblings ...)
2023-06-14 18:42 ` Gary Guo
@ 2023-08-09 23:21 ` Miguel Ojeda
3 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2023-08-09 23:21 UTC (permalink / raw)
To: Alice Ryhl
Cc: rust-for-linux, Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
linux-kernel, patches
On Wed, Jun 14, 2023 at 1:53 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use
> `UnsafeCell` as the outer type. Intuitively, this is because a
> `MaybeUninit<T>` might not contain a `T`, but we always want the effect
> of the `UnsafeCell`, even if the inner value is uninitialized.
>
> Now, strictly speaking, this doesn't really make a difference. The
> compiler will always apply the `UnsafeCell` effect even if the inner
> value is uninitialized. But I think we should follow the convention
> here.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Applied to `rust-next`, thanks everyone!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-08-09 23:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-14 11:53 [PATCH] rust: make `UnsafeCell` the outer type in `Opaque` Alice Ryhl
2023-06-14 14:27 ` Benno Lossin
2023-06-14 14:37 ` Alice Ryhl
2023-06-14 15:30 ` Martin Rodriguez Reboredo
2023-06-14 18:42 ` Gary Guo
2023-06-14 19:04 ` Benno Lossin
2023-08-09 23:21 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®