From patchwork Sun Feb 2 12:20:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 13956499 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 061A31D798E; Sun, 2 Feb 2025 12:20:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738498858; cv=none; b=ZF3AtkMJD7G4bZaDhj5no/1ZBoYSJ1EWYq7fCKTt6kk9hKNG1H7Yis4ejtt8WLPTrRiSyb3KCnH/+0Hgb/oBYkLx+dF5E27xDFQ2mpDmM3vrBbqfF8AKNgUaSbqROKrCQmJhZT9xB4x4HSy/36YH39mJ3mv/nQ8NrFcENjTIMy0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738498858; c=relaxed/simple; bh=cEcJMB3RH+hwgPUqVVtE4kSla9Rnc/uiWqwGre9/JgE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YYr2ZmgDHw6rxmEDVtDB2Gt9XuopN9c+4480M0UZHyDqJSJBKCXlJfGnhoDqjF7hG6Dzd23hwtuNLtiPyFtepSlhqg+A2UI4lKy3alenyofWtsLQ7vdgUwztZpdhty1eHlFGMK0Sabzt/TwYxthZOTGGWjTBfaMvQ1nhpuHmrSs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aO18up+I; arc=none smtp.client-ip=209.85.160.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aO18up+I" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-46fa7678ef3so34105751cf.1; Sun, 02 Feb 2025 04:20:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738498856; x=1739103656; darn=vger.kernel.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=A4uuv0EGtSl3hUZmiuiEbo55gg9kPQ0rvaLmjMl5yVk=; b=aO18up+IpCoyrmnQCx0lxY2uR0nUn5s32tWytHAb4WUxRdPcmphq87z2sR7xA/fJo+ +XZjuS6OL098pi4wqhcxBOm6jR6Tu0OLa0Ar09lohT36h3TnqinQj3d313FDBFJV/GBs 5qN7EkdZSymw4FxYgc24Ij/z+FCmh5XACGoaDwy9bJezbvsZa0Y6TeaRZvV4f85tCD+O G6vkSn+qvoW0v7Ih0l84z8m3p2S7eKyB0bKFHfdeLfxOaf/p6TlJpQqjpoCdIB5W5vim k223BcKVnUA2RTmP/xf9AXtW3L8xf3FFwaJK6/82Jm/l30+XGOjPFYg5td9igZTsiMHx T/3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738498856; x=1739103656; 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=A4uuv0EGtSl3hUZmiuiEbo55gg9kPQ0rvaLmjMl5yVk=; b=WxD5Yk30nms9LFor+XnnO8VsKd9NJMexwGriFiO/5gzJU9m+RXalp84OF6eK33pdnA G2fz92FLoGYRMaBQ4Kfdv3twXLMNCAckHIIiQl43L5/TIZFtRxOPg7i4bI1ydCWgnvyf nngeAjM55Nm15zz3lAgwD2Go9T4wgIHF/37DRBsmx7RwCOmNXj8J/zlx4m8ozkc8+4ti 9e2KSCwtHp399Pg8ejK8Hce0XC2zuxH1dl6achqoEU37XO+n/jWuUmeMC3o3r2X4gr30 94HZffp5qvwqt5d3RkLzCLYWMlUhy+WPB1gVAWY7P0w75UREwwye6wbIs7FYPH+aiAI9 lLOA== X-Forwarded-Encrypted: i=1; AJvYcCWBiy0lXLPOKyM1nH/aj+HYr4rMaI4oGiHWcAiVpaZAN7mnPBXCCfz01/qBoNfv9aAx11m7NqTaUJKhm5M=@vger.kernel.org X-Gm-Message-State: AOJu0YzAEhCc1I+VUAHP6c3V3Vp8BbRDqTx96JIo3Ud0oTU8LlNnXKM/ 0AiBxrWDZFFPXUh8LlZAO7rKEcmK+sDPE/862Wnf4yCw8JS68GvG X-Gm-Gg: ASbGncsXCdXYACLzwiujTmjF6leGJsZHiwnWrBp1aete6A47FiMtMM/3vGqrvV/lZ6m LNTwoyxXEhxoNP4xyGvh4PFTYmYN8mOVa7LhXtYDxsz17FsFvgDTpBbEj73Gvf6AKejYOd4cKpq AeM5CA0xNXQwFDrjxSWVE2jW3d3LoVV9K/KOKBTIZtrfdpU72Zhsh2YHM3NM/THlet99jTq4t2h yrGUamXLlnjiQy9Be/73WhquKlMVSy4rz6Tz2jbbTi8mbotYpxj9rLM06iKjR1nr32c1PVFLA8F zLjHZoojqznPjoSNuM10inIcibiiLSOe9o/0774= X-Google-Smtp-Source: AGHT+IFCxX/g2A/7UErR1Ctfp4+n1YO9mdfo2ye+OgWrutkfud6PysFrWTAAq7t7HDR0wSd84uEjqQ== X-Received: by 2002:a05:622a:1e8a:b0:46d:faa2:b6d1 with SMTP id d75a77b69052e-46fd0a9dc93mr322592151cf.16.1738498855753; Sun, 02 Feb 2025 04:20:55 -0800 (PST) Received: from tamird-mac.local ([2600:4041:5be7:7c00:f07f:fb14:288d:6f1e]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46fdf1728efsm37700331cf.62.2025.02.02.04.20.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Feb 2025 04:20:54 -0800 (PST) From: Tamir Duberstein Date: Sun, 02 Feb 2025 07:20:46 -0500 Subject: [PATCH v6 1/4] rust: move BStr,CStr Display impls behind method Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250202-cstr-core-v6-1-8469cd6d29fd@gmail.com> References: <20250202-cstr-core-v6-0-8469cd6d29fd@gmail.com> In-Reply-To: <20250202-cstr-core-v6-0-8469cd6d29fd@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 Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev There are two reasons for doing this: - Moving the Display impl behind a display method matches the style used in the standard library for printing non-Unicode data. - The standard library's core::ffi::CStr doesn't implement Display; moving Display to a helper struct is a necessary step toward using it. Signed-off-by: Tamir Duberstein --- rust/kernel/kunit.rs | 9 ++-- rust/kernel/str.rs | 120 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 89 insertions(+), 40 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..53450c956a6a 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -31,29 +31,76 @@ pub const fn from_bytes(bytes: &[u8]) -> &Self { // SAFETY: `BStr` is transparent to `[u8]`. unsafe { &*(bytes as *const [u8] as *const BStr) } } -} -impl fmt::Display for BStr { - /// Formats printable ASCII characters, escaping the rest. + /// Returns an object that implements [`Display`] for safely printing a [`BStr`] that may + /// contain non-Unicode data. If you would like an implementation which escapes the [`BStr`] + /// please use [`Debug`] instead. + /// + /// [`Display`]: fmt::Display + /// [`Debug`]: fmt::Debug + /// + /// # Examples /// /// ``` - /// # use kernel::{fmt, b_str, str::{BStr, CString}}; + /// # use kernel::{fmt, b_str, str::CString}; /// let ascii = b_str!("Hello, BStr!"); - /// let s = CString::try_from_fmt(fmt!("{}", ascii))?; + /// let s = CString::try_from_fmt(fmt!("{}", ascii.display()))?; /// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes()); /// /// let non_ascii = b_str!("