From nobody Fri Dec 19 02:51:00 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E870BC636D4 for ; Tue, 7 Feb 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231952AbjBGSwy (ORCPT ); Tue, 7 Feb 2023 13:52:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232212AbjBGSwv (ORCPT ); Tue, 7 Feb 2023 13:52:51 -0500 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2CF49EF0; Tue, 7 Feb 2023 10:52:48 -0800 (PST) Received: by mail-qv1-xf29.google.com with SMTP id k28so9724401qve.5; Tue, 07 Feb 2023 10:52:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=wmko9U6xz9AxcE4cRj9bysFLw2CXZVZtRuLkE86PSJA=; b=kNZGlZmQpFMwNMGoZy2/mcgEFIa+lvEq0MYcXzAQK3qX+HTN5H4fwMzEe81b5O880H D22mUtuh3l92jbls5hULW1+M4F1Rqfulm1du6ml7mgcQClVdolH6xoLgODA2PDm8zHp6 UtXY/bsl8CSTLthlOmblI3nD8MD4C4aGPo6WfuKJvrKqAAfmYn0D7Ft0jc5GTky+rFWn 9tczATkj8F7CdwArhAxU3evcSLF1cY2rM0/A9UozcwmQ6T8ceyCmaIDW6lnwQ93xfu+Y +sfF/oHkmW8mjjesgwd63pWzyMRlDmoxcAaFG9ZyJReGQGRVkMFVwDel+Q9yvvJtDo1R W9ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=wmko9U6xz9AxcE4cRj9bysFLw2CXZVZtRuLkE86PSJA=; b=SL9Zje1jVu6IIFHJCxb7WDMxB1DrXQWEbZ0bwYwdv3mgvghOnVMwH1yhEXzXCegVGG wAJU74WGn+IB02/1Aur76pvDY656wDTntL3BPpW2m29VDShxImrI9KTTHQp0cRkiOWqu nHbIprVNQzRC5QsJn1iD8DwO4p9vfNlPwX2J9MtVVstcRxCY2ul+FrADeVFIv2RcPoPz dL8wwRIA+gi1JsXn6IxX4Fsv3cKwDQqF1q9xpzcBrvYshRDFAnmTCspRcK7UsLUeYJb8 M/snQ/g+XxIlB2CJl506Jnm0B8sdvywG4jNv92V5DwOQ6B3AbiDBFP09QlOBcERxYxqQ 2rdw== X-Gm-Message-State: AO0yUKXaNWw61PDX9d5A0RgfNlStjbDJh/RbB04MOdou4FlAtal0T6uP XgYOzk0a5jHOtgyJud+74xY= X-Google-Smtp-Source: AK7set/LuYmzN5/m8rFk4BNLQnUmbQnLnDMH0XltaV9w4TM9F1NK6YBQ2/XEccxAX505vc0pgL2VyQ== X-Received: by 2002:a05:6214:5298:b0:56b:ee96:4d32 with SMTP id kj24-20020a056214529800b0056bee964d32mr6706308qvb.51.1675795967835; Tue, 07 Feb 2023 10:52:47 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id p13-20020a05620a22ad00b007204305dee4sm9840932qkh.19.2023.02.07.10.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Feb 2023 10:52:47 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id D8DA527C0054; Tue, 7 Feb 2023 13:52:46 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Tue, 07 Feb 2023 13:52:46 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudegkedgudduhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepheefvddtkeejtdelffekgfdtheeugeefffekgefghfejhfduieekjeeg heetvdefnecuffhomhgrihhnpehpthhrrdgrshenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghr shhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvg hngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 7 Feb 2023 13:52:45 -0500 (EST) From: Boqun Feng To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Vincenzo Palazzo , Will Deacon , Peter Zijlstra , Mark Rutland , Greg Kroah-Hartman , =?UTF-8?q?Sergio=20Gonz=C3=A1lez=20Collado?= , Finn Behrens Subject: [PATCH v2 1/2] rust: sync: impl {Debug,Display} for {Unique,}Arc Date: Tue, 7 Feb 2023 10:52:15 -0800 Message-Id: <20230207185216.1314638-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230207185216.1314638-1-boqun.feng@gmail.com> References: <20230207185216.1314638-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows printing the inner data of `Arc` and its friends if the inner data implements `Display` or `Debug`. It's useful for logging and debugging purpose. Signed-off-by: Boqun Feng Reviwed-by: Vincenzo Palazzo Reviewed-by: Andreas Hindborg Reviewed-by: Bj=C3=B6rn Roy Baron Reviewed-by: Gary Guo Reviewed-by: Vincenzo Palazzo --- rust/kernel/sync/arc.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 519a6ec43644..e6176d9b5b29 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -22,6 +22,7 @@ use crate::{ }; use alloc::boxed::Box; use core::{ + fmt, marker::{PhantomData, Unsize}, mem::{ManuallyDrop, MaybeUninit}, ops::{Deref, DerefMut}, @@ -522,3 +523,27 @@ impl DerefMut for UniqueArc { unsafe { &mut self.inner.ptr.as_mut().data } } } + +impl fmt::Display for UniqueArc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.deref(), f) + } +} + +impl fmt::Display for Arc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.deref(), f) + } +} + +impl fmt::Debug for UniqueArc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self.deref(), f) + } +} + +impl fmt::Debug for Arc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self.deref(), f) + } +} --=20 2.39.1 From nobody Fri Dec 19 02:51:00 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5204C636D3 for ; Tue, 7 Feb 2023 18:52:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229640AbjBGSw6 (ORCPT ); Tue, 7 Feb 2023 13:52:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232361AbjBGSwv (ORCPT ); Tue, 7 Feb 2023 13:52:51 -0500 Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B251103; Tue, 7 Feb 2023 10:52:50 -0800 (PST) Received: by mail-qt1-x82b.google.com with SMTP id c2so17892199qtw.5; Tue, 07 Feb 2023 10:52:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=pw0ByORIxbzjhrXTdLg80T0eGpn6Y+jK2lz5pJ++YH8=; b=ZncQL7xoNfDQKYP2yNCeYPlLtuGapA/miBtdGv+YudqBd6A4483uU9LQ9tZ7Z6MALU WdMoI7xGiho0iARaGqGVxHmQIcesZ59Y2/6KR572sn1DMC0CfV2MqOx5rN1c5zBCtyJ1 dt3tEWWt5tht3s9NrenVZa/Y4XcPWOWTZKtSsfLVlzVzPyjD2OxC+TEzgqLgVqS2ZO7K nV27ZVwikFgP1d6l67GLr/UKOM/cU93+dU7luJ1MGhMvX01hv/BWabyWiTS4waYuThA+ GzZxf2XjNpnO8ORApL/h1OsjWrTfp9ol35xjPhuIxTCJobkyNyTHzmsUugyROy4HsyIj qLWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=pw0ByORIxbzjhrXTdLg80T0eGpn6Y+jK2lz5pJ++YH8=; b=3fQdLBFziCr6fxyviDPq7KLC/c0fhmLLhRvTz/6ujEzshmqatLKKavVWpafsOvg2IG B9+cTftS2iYcG4+HtP+4eRcJo8chEXjk1xtfctVAPznRXitTtKbw5ruz6MgsPMZ27uRk 1YY0XApK+bys4BIsQfdlF7zLb7ef9Yjy5lJ1jovf+Zc+WYYYCFsI+9R11wYduqfEfUNp hiKuQMNWzBFUf+gcK1hWZaK+2ACKvNAUd7zmL2VIekpyNQBWFe3uiuL6hdu0IIhIzj7h VwDfceJg6Iu/5dMK0qJjp+RY6MhB+832/j6RMPtei6de0ktEZqObXtkP5PUyNhJR+h1x cpTw== X-Gm-Message-State: AO0yUKUs8rXtY9agCrZEObIhfkTbeznsAoodHIZFvw6DvoKDaBkrNGWk 1Us496UvPpoOAeh3N3I+EpcxgTrJQP4= X-Google-Smtp-Source: AK7set+CfALORVXU2P4BIeSCVej6n+PHzACGFPleZxICuoyIdhEftiR2lBEGapJleGAMbQIgzOgrsw== X-Received: by 2002:ac8:7fc2:0:b0:3ba:1360:ec0a with SMTP id b2-20020ac87fc2000000b003ba1360ec0amr7070190qtk.41.1675795969132; Tue, 07 Feb 2023 10:52:49 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id f9-20020ac84989000000b003b869f71eedsm9729924qtq.66.2023.02.07.10.52.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Feb 2023 10:52:48 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 4022D27C0054; Tue, 7 Feb 2023 13:52:48 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 07 Feb 2023 13:52:48 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudegkedgudduhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfgggtgfesthekredtredtjeenucfhrhhomhepueho qhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtf frrghtthgvrhhnpeehffeiheehhfevhefhleefjedtiedtjefhveekfeejiedugeekkeeu uefhieevkeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeeh tdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmse hfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 7 Feb 2023 13:52:47 -0500 (EST) From: Boqun Feng To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Vincenzo Palazzo , Will Deacon , Peter Zijlstra , Mark Rutland , Greg Kroah-Hartman , =?UTF-8?q?Sergio=20Gonz=C3=A1lez=20Collado?= , Finn Behrens Subject: [PATCH v2 2/2] sample: rust: print: Add sampe code for Arc printing Date: Tue, 7 Feb 2023 10:52:16 -0800 Message-Id: <20230207185216.1314638-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230207185216.1314638-1-boqun.feng@gmail.com> References: <20230207185216.1314638-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This both demonstrates the usage of different print format in Rust and serves as a selftest for the `Display` and `Debug` implementation of `Arc` and its friends. Signed-off-by: Boqun Feng Reviewed-by: Bj=C3=B6rn Roy Baron Reviewed-by: Finn Behrens Reviewed-by: Vincenzo Palazzo Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo --- samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs index 8b39d9cef6d1..165a8d7b1c07 100644 --- a/samples/rust/rust_print.rs +++ b/samples/rust/rust_print.rs @@ -15,6 +15,30 @@ module! { =20 struct RustPrint; =20 +fn arc_print() -> Result { + use kernel::sync::*; + + let a =3D Arc::try_new(1)?; + let b =3D UniqueArc::try_new("hello, world")?; + + // Prints the value of data in `a`. + pr_info!("{}", a); + + // Uses ":?" to print debug fmt of `b`. + pr_info!("{:?}", b); + + let a: Arc<&str> =3D b.into(); + let c =3D a.clone(); + + // Uses `dbg` to print, will move `c`. + dbg!(c); + + // Prints debug fmt with pretty-print "#" and number-in-hex "x". + pr_info!("{:#x?}", a); + + Ok(()) +} + impl kernel::Module for RustPrint { fn init(_module: &'static ThisModule) -> Result { pr_info!("Rust printing macros sample (init)\n"); @@ -43,6 +67,8 @@ impl kernel::Module for RustPrint { pr_cont!(" is {}", "continued"); pr_cont!(" with {}\n", "args"); =20 + arc_print()?; + Ok(RustPrint) } } --=20 2.39.1