[v1,28/28] rust: types: add `Opaque` type

Message ID 20221110164152.26136-29-ojeda@kernel.org
State New
Headers
Series Rust core additions |

Commit Message

Miguel Ojeda Nov. 10, 2022, 4:41 p.m. UTC
  From: Wedson Almeida Filho <wedsonaf@gmail.com>

Add the `Opaque` type, which is meant to be used with FFI objects
that are never interpreted by Rust code, e.g.:

    struct Waiter {
        completion: Opaque<bindings::completion>,
        next: *mut Waiter,
    }

It has the advantage that the objects don't have to be
zero-initialised before calling their init functions, making
the code performance closer to C.

Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/kernel/types.rs | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
  

Comments

Gary Guo Nov. 14, 2022, 3:03 p.m. UTC | #1
On Thu, 10 Nov 2022 17:41:40 +0100
Miguel Ojeda <ojeda@kernel.org> wrote:

> From: Wedson Almeida Filho <wedsonaf@gmail.com>
> 
> Add the `Opaque` type, which is meant to be used with FFI objects
> that are never interpreted by Rust code, e.g.:
> 
>     struct Waiter {
>         completion: Opaque<bindings::completion>,
>         next: *mut Waiter,
>     }
> 
> It has the advantage that the objects don't have to be
> zero-initialised before calling their init functions, making
> the code performance closer to C.
> 
> Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
> [Reworded, adapted for upstream and applied latest changes]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
>  rust/kernel/types.rs | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index 3b0c44769708..e84e51ec9716 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -2,6 +2,31 @@
>  
>  //! Kernel types.
>  
> +use core::{cell::UnsafeCell, mem::MaybeUninit};
> +
> +/// Stores an opaque value.
> +///
> +/// 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>>);

Shouldn't this be `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)))
> +    }
> +
> +    /// Creates an uninitialised value.
> +    pub const fn uninit() -> Self {
> +        Self(MaybeUninit::uninit())
> +    }
> +
> +    /// Returns a raw pointer to the opaque data.
> +    pub fn get(&self) -> *mut T {
> +        UnsafeCell::raw_get(self.0.as_ptr())
> +    }
> +}
> +
>  /// A sum type that always holds either a value of type `L` or `R`.
>  pub enum Either<L, R> {
>      /// Constructs an instance of [`Either`] containing a value of type `L`.
  

Patch

diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 3b0c44769708..e84e51ec9716 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -2,6 +2,31 @@ 
 
 //! Kernel types.
 
+use core::{cell::UnsafeCell, mem::MaybeUninit};
+
+/// Stores an opaque value.
+///
+/// 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>>);
+
+impl<T> Opaque<T> {
+    /// Creates a new opaque value.
+    pub const fn new(value: T) -> Self {
+        Self(MaybeUninit::new(UnsafeCell::new(value)))
+    }
+
+    /// Creates an uninitialised value.
+    pub const fn uninit() -> Self {
+        Self(MaybeUninit::uninit())
+    }
+
+    /// Returns a raw pointer to the opaque data.
+    pub fn get(&self) -> *mut T {
+        UnsafeCell::raw_get(self.0.as_ptr())
+    }
+}
+
 /// A sum type that always holds either a value of type `L` or `R`.
 pub enum Either<L, R> {
     /// Constructs an instance of [`Either`] containing a value of type `L`.