From: Timo Grautstueck <timo.grautstueck@web.de>
To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: miguel.ojeda.sandonis@gmail.com,
Timo Grautstueck <timo.grautstueck@web.de>,
Miguel Ojeda <ojeda@kernel.org>
Subject: [PATCH] rust: types: 'real-life' example for Either
Date: Mon, 7 Oct 2024 17:53:15 +0200 [thread overview]
Message-ID: <20241007155315.1145503-1-timo.grautstueck@web.de> (raw)
In-Reply-To: <CANiq72n25iBCJtDV=XD2q5AEg4Awh9hyeKM0YwbeE0pv0H2hLA@mail.gmail.com>
Added a 'real-life' example for the type `Either`. This example
uses a work queue and extends the first example in `workqueue.rs`
(commit ID: 15b286d) to demonstrate how to hold and distinguish
between two different data types.
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1122
Signed-off-by: Timo Grautstueck <timo.grautstueck@web.de>
---
rust/kernel/types.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 5ea9126c8c93..a56192141a0c 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -322,6 +322,60 @@ pub const fn raw_get(this: *const Self) -> *mut T {
/// let left_value: Either<i32, &str> = Either::Left(7);
/// let right_value: Either<i32, &str> = Either::Right("right value");
/// ```
+///
+/// The following example demonstrates how we can create a struct
+/// that uses `Either` to hold either an integer or a string.
+/// This struct will be scheduled on the workqueue, and when executed,
+/// we will perform different actions depending on whether it holds
+/// a `Left` value (integer) or a `Right` value (string).
+///
+/// ```
+/// use kernel::prelude::*;
+/// use kernel::sync::Arc;
+/// use kernel::types::Either;
+/// use kernel::workqueue::{self, new_work, Work, WorkItem};
+///
+/// #[pin_data]
+/// struct WorkStruct {
+/// value: Either<i32, &'static str>,
+/// #[pin]
+/// work: Work<WorkStruct>,
+/// }
+///
+/// impl_has_work! {
+/// impl HasWork<Self> for WorkStruct { self.work }
+/// }
+///
+/// impl WorkStruct {
+/// fn new(value: Either<i32, &'static str>) -> Result<Arc<Self>> {
+/// Arc::pin_init(pin_init!(WorkStruct {
+/// value,
+/// work <- new_work!("WorkStruct::work"),
+/// }), GFP_KERNEL)
+/// }
+/// }
+///
+/// impl WorkItem for WorkStruct {
+/// type Pointer = Arc<WorkStruct>;
+///
+/// fn run(this: Arc<WorkStruct>) {
+/// match &this.value {
+/// Either::Left(left_value) => {
+/// pr_info!("Left value: {}", left_value);
+/// pr_info!("Left value times two: {}", left_value << 1);
+/// }
+/// Either::Right(right_value) => {
+/// pr_info!("Right value: {}", right_value);
+/// pr_info!("Length of right value: {}", right_value.len());
+/// }
+/// }
+/// }
+/// }
+///
+/// fn enqueue_work(work_item: Arc<WorkStruct>) {
+/// let _ = workqueue::system().enqueue(work_item);
+/// }
+/// ```
pub enum Either<L, R> {
/// Constructs an instance of [`Either`] containing a value of type `L`.
Left(L),
--
2.46.2
next prev parent reply other threads:[~2024-10-07 15:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-18 21:20 [PATCH v2] rust: types: Add examples for the `Either` type Nell Shamrell-Harrington
2024-09-29 21:38 ` Trevor Gross
2024-09-30 7:56 ` Alice Ryhl
2024-10-01 22:57 ` Miguel Ojeda
2024-10-07 15:53 ` Timo Grautstueck [this message]
2024-10-07 18:47 ` [PATCH] rust: types: 'real-life' example for Either Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241007155315.1145503-1-timo.grautstueck@web.de \
--to=timo.grautstueck@web.de \
--cc=linux-kernel@vger.kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®