From patchwork Mon Mar 17 15:30:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14019448 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 58933C282EC for ; Mon, 17 Mar 2025 15:30:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BCACA10E0E0; Mon, 17 Mar 2025 15:30:21 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="O2WqQ59s"; dkim-atps=neutral Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1DFEC10E163 for ; Mon, 17 Mar 2025 15:30:17 +0000 (UTC) Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-4766631a6a4so46376491cf.2 for ; Mon, 17 Mar 2025 08:30:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742225417; x=1742830217; darn=lists.freedesktop.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=GhDi7jicB3Gv5oqI3TBvIGb8fiLoyX3gf2bLY34vPcA=; b=O2WqQ59s0OoEV+FpOAES8BsvOaKejwh0isM0caqOHLCplahLNzXPtadBknHr8wZ7JF +CbCeGz6y9wupWvy+NNYvnu6HVFEMWNBfkW+6dxio9EkwTjAf5hnkzpazZ74xWt9UlvW gPlnDa87qKoOiACRllDqkxb3NbuHTrH9Ga62DHWCmyN2C+R5bYT03/yGg6LbATr4Ghz3 k80yKYrPLW/iuzN0GO/uY+ucC4V1Y/m00jqfhmR7KL2vzaSzLE98CxzhJMv+wSb6Ljsp Nt1KpLYMpW7EzvEij4PFuBsaXFDrZG97UZ7ecLu5oEK6OOU1goYoi4AJYREATp9v3WXU L6yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742225417; x=1742830217; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GhDi7jicB3Gv5oqI3TBvIGb8fiLoyX3gf2bLY34vPcA=; b=hhi38I9MuYAW7+E6yZ1yMrj3TSsm+vnYXZGRNnPrSxtMZYC6Nd8TmYZ8cumvt7WKeU lOFFM+PWlz4/C54oK855ci1sTcECKWSHsqX2M9MRzvzuf1dBLy+tk3afVk/2qc8axd6k PeLBUxDcE3vvvtuabD1zsImwFePRgQqdtFCGuZFLGsS4aBAGFRqec87ze6gsCWVtVHr4 Hvil4K/EqlKPG+OoG4447Yj4mA4lOsBiuON2a9qQClxUBgWJNafwFLDW4vhE5PXnoDdM funwTtraFxDmT4hD3EQ5jkFLDtDQUiOoe9xUP7IBibAqyR8tvJIvFkic9lHc9ccFT6fa FsHA== X-Forwarded-Encrypted: i=1; AJvYcCVtccOFJRzIhebV2KRhSQye5SZZR3lDVC1HW1L+8kLqN0Iac+z7cxBQVMU+UM5cvsQeUZtSiRnBHW4=@lists.freedesktop.org X-Gm-Message-State: AOJu0YxXqD74I//r7t0rzzcOlKARqgUVk6I0JHp89CqrUYqzxlCvfRcc zNP0E4f6VAx9gxj8+AbkLFWYPvP0mNSQqwg3l2gzOsqyobHwLfzW X-Gm-Gg: ASbGncvkgXU7NTTxnUUTmHDy79pvnkMCeqig6YdXPadsuj0I5IK1hz935JgfblVRk5v 90TvUU2/hYgcUsh0o7MKsMMR0VI+xTr4yJe87XVL1Wf8RDTI6mVgymJEBgDvyo1xVqIOHL3DsKR 92hmjY+aoMvMjX1YkYBd9TIguvAu8dU8SRAZX8Cu4YLHdKwfZsyEW6pNyn+LLV5yCifocJOhpP5 PK8puetIOTT/wOSerBY8EDhjHax1p5HQ8wktxgdbkA2iBEh3vRjj23N1q0Cy8qqgSQKD3Y4wlIu uN/iCw7CxFGM6ZniqkB+TV84aP+gCcdoRs5/13vB7l9gnVjRJu4vbtccFesP4Fa5Bn51FhlBSGZ 1NQt+Vg== X-Google-Smtp-Source: AGHT+IF6QKQ27zO/djSgLkh3SLlbu4CCHZ6bVaY3V5bdQy4+E/A82ztgFL3TtEYWb5+dRwVxvOIv5Q== X-Received: by 2002:a05:6214:c47:b0:6e6:5f08:e77d with SMTP id 6a1803df08f44-6eaeaa64726mr197312436d6.19.1742225416788; Mon, 17 Mar 2025 08:30:16 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2600:4041:5be7:7c00:6895:24c6:e898:e320]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade34c51fsm55736766d6.109.2025.03.17.08.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Mar 2025 08:30:16 -0700 (PDT) From: Tamir Duberstein Date: Mon, 17 Mar 2025 11:30:03 -0400 Subject: [PATCH v9 1/4] rust: move `CStr`'s `Display` to helper struct MIME-Version: 1.0 Message-Id: <20250317-cstr-core-v9-1-51d6cc522f62@gmail.com> References: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> In-Reply-To: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> To: Michal Rostecki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Brendan Higgins , David Gow , Rae Moar , Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Greg Kroah-Hartman , "Rafael J. Wysocki" , Luis Chamberlain , Russ Weight , FUJITA Tomonori , Rob Herring , Saravana Kannan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Bjorn Helgaas , Arnd Bergmann Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, llvm@lists.linux.dev, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Remove `impl Display for CStr` in preparation for replacing `CStr` with `core::ffi::CStr` which doesn't impl `Display`. Add `CStr::display` returning a helper struct to replace the lost functionality; this matches the APIs exposed by `std::ffi::OSstr` and `std::path::Path` for printing non-Unicode data. Signed-off-by: Tamir Duberstein --- rust/kernel/kunit.rs | 9 +++++--- rust/kernel/str.rs | 63 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 824da0e9738a..630b947c708c 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -56,6 +56,7 @@ macro_rules! kunit_assert { break 'out; } + static NAME: &'static $crate::str::CStr = $crate::c_str!($name); static FILE: &'static $crate::str::CStr = $crate::c_str!($file); static LINE: i32 = core::line!() as i32 - $diff; static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); @@ -71,11 +72,13 @@ macro_rules! kunit_assert { // // This mimics KUnit's failed assertion format. $crate::kunit::err(format_args!( - " # {}: ASSERTION FAILED at {FILE}:{LINE}\n", - $name + " # {NAME}: ASSERTION FAILED at {FILE}:{LINE}\n", + NAME = NAME.display(), + FILE = FILE.display(), )); $crate::kunit::err(format_args!( - " Expected {CONDITION} to be true, but is false\n" + " Expected {CONDITION} to be true, but is false\n", + CONDITION = CONDITION.display(), )); $crate::kunit::err(format_args!( " Failure not reported to KUnit since this is a non-KUnit task\n" diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 28e2201604d6..50eb4266047a 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -376,27 +376,66 @@ pub fn to_ascii_uppercase(&self) -> Result { Ok(s) } -} -impl fmt::Display for CStr { - /// Formats printable ASCII characters, escaping the rest. + /// Returns an object that implements [`Display`] for safely printing a [`CStr`] that may + /// contain non-Unicode data. If you would like an implementation which escapes the [`CStr`] + /// please use [`Debug`] instead. + /// + /// [`Display`]: fmt::Display + /// [`Debug`]: fmt::Debug + /// + /// # Examples /// /// ``` /// # use kernel::c_str; /// # use kernel::fmt; - /// # use kernel::str::CStr; /// # use kernel::str::CString; /// let penguin = c_str!("