From nobody Wed Nov 27 21:34:26 2024 Received: from mout.web.de (mout.web.de [212.227.15.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E82EC1D14E3; Mon, 7 Oct 2024 15:55:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728316508; cv=none; b=IPzMb4f3/xgXG8bn029dA3KMaBH46ZuufNU2mmRcZSSYGNmqVikfcjUfTfrTI2PNVSUOUdK3xxSnOrTE48SSOnLxajLUvLScsFPH3V+Ij2L0SvROizD5dgbjA17l2STlkpQIzscf18J/KDMrh2bkicbvdmJ9n3DYSUvnSfu2Z34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728316508; c=relaxed/simple; bh=01cY3E7WMjbmgRpn35G0Wsg5xsXMcmTXUoC9DpxF3DY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mZ2STT3Y8V9Pnqev/gbModjdOFNF2CQIcusQJXSNc93UVvPqRa8wH1UCUrKTbKRaGMhA9qNX8Vqwstm+0KQKANrL40XvfXKeyR6AcCMEyNnWMXyoFQISESvx0LmgH3aks23TokEzfoN6GDc33N0mF/P7E2vzu2PvGZJhMaMhToU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=timo.grautstueck@web.de header.b=YuLSiBzB; arc=none smtp.client-ip=212.227.15.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=timo.grautstueck@web.de header.b="YuLSiBzB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1728316496; x=1728921296; i=timo.grautstueck@web.de; bh=tZyMhH5kp7QMFFRMcSTA1VhW+Z5ZM7Gk0Y6yV2wQRWY=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=YuLSiBzBM2oFmPTScD5Okvbju+TUytW4UMTeu5VGyxjkgzzDY7kno38LQhh4S/jY +JpKXU0ZT+J062EkbRE+A70LEkeUGeT+OP5eUt6PbbcpaDCtHZRAdt/bIim7HwOxT 6cWq8Ks2AjwbnaqeQJrD/coXTS2/dKJv1tdCwoPDB/vfLc6MZfpuu2N8X1dqa4QM7 Yg2sr4UQVBhza5a1oTh0nd/W6NfRQ5KYIBUOFBq8WDLj1rmBhhs4p+XbnSJlA5O2K ATsd5tn/1vi6jr1xL95W2cP0NFfxfHDTPximSYmH/suVlFlSvWWgMZBtMGhyolF2T I8eaewXyfkuu7fq/rQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from fedora ([95.223.78.106]) by smtp.web.de (mrweb005 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MzkST-1ttXE10Oxd-00xIwa; Mon, 07 Oct 2024 17:54:56 +0200 From: Timo Grautstueck To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: miguel.ojeda.sandonis@gmail.com, Timo Grautstueck , Miguel Ojeda Subject: [PATCH] rust: types: 'real-life' example for Either Date: Mon, 7 Oct 2024 17:53:15 +0200 Message-ID: <20241007155315.1145503-1-timo.grautstueck@web.de> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding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55txsBSL8FVGki2AgomKehHslJDtZ7FxiImRch9LmWffPYsEr+g7iz4WNqCi+ ImKO4ahgDEgl8/hPrmXySDlg== Content-Type: text/plain; charset="utf-8" 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 Link: https://github.com/Rust-for-Linux/linux/issues/1122 Signed-off-by: Timo Grautstueck --- 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 =3D Either::Left(7); /// let right_value: Either =3D 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, +/// #[pin] +/// work: Work, +/// } +/// +/// impl_has_work! { +/// impl HasWork for WorkStruct { self.work } +/// } +/// +/// impl WorkStruct { +/// fn new(value: Either) -> Result> { +/// Arc::pin_init(pin_init!(WorkStruct { +/// value, +/// work <- new_work!("WorkStruct::work"), +/// }), GFP_KERNEL) +/// } +/// } +/// +/// impl WorkItem for WorkStruct { +/// type Pointer =3D Arc; +/// +/// fn run(this: Arc) { +/// match &this.value { +/// Either::Left(left_value) =3D> { +/// pr_info!("Left value: {}", left_value); +/// pr_info!("Left value times two: {}", left_value << 1); +/// } +/// Either::Right(right_value) =3D> { +/// pr_info!("Right value: {}", right_value); +/// pr_info!("Length of right value: {}", right_value.len(= )); +/// } +/// } +/// } +/// } +/// +/// fn enqueue_work(work_item: Arc) { +/// let _ =3D workqueue::system().enqueue(work_item); +/// } +/// ``` pub enum Either { /// Constructs an instance of [`Either`] containing a value of type `L= `. Left(L), -- 2.46.2