From patchwork Wed Sep 11 22:52:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13801196 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8DC58EE57DC for ; Wed, 11 Sep 2024 22:57:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 183A36B00B1; Wed, 11 Sep 2024 18:57:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 133BC6B00B3; Wed, 11 Sep 2024 18:57:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECD0C6B00B4; Wed, 11 Sep 2024 18:57:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C7BE76B00B1 for ; Wed, 11 Sep 2024 18:57:06 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 76338141094 for ; Wed, 11 Sep 2024 22:57:06 +0000 (UTC) X-FDA: 82553969652.10.1296D85 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf17.hostedemail.com (Postfix) with ESMTP id C0B1540017 for ; Wed, 11 Sep 2024 22:57:04 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=F2tMltIA; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726095321; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xeZMQRweATNzsUU2VsVY3UXM5T0TGPEUM4pc7l5tHz4=; b=ipuYJXSBbx7VjCg+KZiDLa1H8RK6orRZFbJGH96QZJyfnALmIMxtqhdSC/+MvWOWdoW35h ZOrde+UMcc5Qsh4bh+t0SHXfFze1soR9dZ5JWreO4soopVW6LdL095PeLt64Aa5/vtJkNn ZVnIBIi1295LNGoIe6Yq6NCaemlW3/s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726095321; a=rsa-sha256; cv=none; b=53a7vGxaIRAH6ZbLrjd0qELXDMzTeY9t7Ay3qGOUrESBWkY66H3Dv527LPpuMLcoHF+EYd CGbeJovtSWmT0iOsBs9iqNYt9Vf0X4w1j4JmptbIyMHPG8b/EW03o3t1G1JKuHeIQgLqFs uvPO3i9hjef03cfCSzU4ujvaLeX7uuc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=F2tMltIA; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 468F55C0655; Wed, 11 Sep 2024 22:57:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6DA91C4CEC5; Wed, 11 Sep 2024 22:56:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726095423; bh=fJujYBGRQtYZqIjs4LKUGmLmSm16nkPMSrJSclIruqE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F2tMltIAnv/i/tJGJe7bNbkIVpUV0WYSuNcZNfxRBs0WykhZrbPUzjYzdq1yY34uH 1Tx1f0x7bXrf25FFCClhtCtxPjxtJodKtTeMP74jggPIzmW/Z/1VJE6bDVMXBtUCvg XkIo9Xxs8cYlTzpQVFzlR6GruyGgXDdHHxUw8xMdtQ8zupFvp5V7el7Qx/kcfhcV6F S8uHLX7f2feRP+Gm60bPlSTQHq8hx+Uef+zFzcViVZkn5GWlUJqbW3040EiuFZo9sg rXwez/Mly+0HlkBG7ZYvORIQfbENvQGSBM11EbHHo2c4mMzroKLodWB7u1U76CPay2 nU3m0kqSg8I5w== From: Danilo Krummrich To: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org Cc: daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Danilo Krummrich Subject: [PATCH v7 23/26] rust: str: test: replace `alloc::format` Date: Thu, 12 Sep 2024 00:52:59 +0200 Message-ID: <20240911225449.152928-24-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240911225449.152928-1-dakr@kernel.org> References: <20240911225449.152928-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C0B1540017 X-Stat-Signature: xo7mfrhwgrf8wfxkzjkuqeoa1tzcw57k X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1726095424-389439 X-HE-Meta: U2FsdGVkX1+pa7GiWlF/w9ES5KTHVoo4wdMOpP+Sx73lH3GIelF7H0SviWEJA9WVKxZxrsbH0q/HaEWKfwk+6GdzhmA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z1PKM8H7ZNwyJpq70ywkb4bgKOVOK4X2Bt1L4AXPvrSUidW6TNpXg7qPYYSOZXE2+qMmg/i+xrL6tvIsq3qk0Wax7jSE6pOZRTnuQgE/CCnLw9O+AEAaH5EmcLhHJNywmjF4piXTkBSoYph6MQ6YDlHwSGypQE/IlaGfrCcoKwLcfPRdwsA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The current implementation of tests in str.rs use `format!` to format strings for comparison, which, internally, creates a new `String`. In order to prepare for getting rid of Rust's alloc crate, we have to cut this dependency. Instead, implement `format!` for `CString`. Note that for userspace tests, `Kmalloc`, which is backing `CString`'s memory, is just a type alias to `Cmalloc`. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Signed-off-by: Danilo Krummrich Reviewed-by: Gary Guo --- rust/kernel/str.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 209ad1bb1a49..726329530338 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -523,7 +523,28 @@ macro_rules! c_str { #[cfg(test)] mod tests { use super::*; - use alloc::format; + + struct String(CString); + + impl String { + fn from_fmt(args: fmt::Arguments<'_>) -> Self { + String(CString::try_from_fmt(args).unwrap()) + } + } + + impl Deref for String { + type Target = str; + + fn deref(&self) -> &str { + self.0.to_str().unwrap() + } + } + + macro_rules! format { + ($($f:tt)*) => ({ + &*String::from_fmt(kernel::fmt!($($f)*)) + }) + } const ALL_ASCII_CHARS: &'static str = "\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\