xref: /openbmc/linux/rust/kernel/types.rs (revision 65844828)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Kernel types.
4 
5 use core::{cell::UnsafeCell, mem::MaybeUninit};
6 
7 /// Stores an opaque value.
8 ///
9 /// This is meant to be used with FFI objects that are never interpreted by Rust code.
10 #[repr(transparent)]
11 pub struct Opaque<T>(MaybeUninit<UnsafeCell<T>>);
12 
13 impl<T> Opaque<T> {
14     /// Creates a new opaque value.
15     pub const fn new(value: T) -> Self {
16         Self(MaybeUninit::new(UnsafeCell::new(value)))
17     }
18 
19     /// Creates an uninitialised value.
20     pub const fn uninit() -> Self {
21         Self(MaybeUninit::uninit())
22     }
23 
24     /// Returns a raw pointer to the opaque data.
25     pub fn get(&self) -> *mut T {
26         UnsafeCell::raw_get(self.0.as_ptr())
27     }
28 }
29 
30 /// A sum type that always holds either a value of type `L` or `R`.
31 pub enum Either<L, R> {
32     /// Constructs an instance of [`Either`] containing a value of type `L`.
33     Left(L),
34 
35     /// Constructs an instance of [`Either`] containing a value of type `R`.
36     Right(R),
37 }
38