Merge tag 'trace-v6.2-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[linux-block.git] / rust / kernel / types.rs
CommitLineData
ba20915b
WAF
1// SPDX-License-Identifier: GPL-2.0
2
3//! Kernel types.
4
b9ecf9b9
WAF
5use 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)]
11pub struct Opaque<T>(MaybeUninit<UnsafeCell<T>>);
12
13impl<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
ba20915b
WAF
30/// A sum type that always holds either a value of type `L` or `R`.
31pub 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}