From patchwork Mon Aug 5 15:19:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13753735 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 C1E40C3DA4A for ; Mon, 5 Aug 2024 15:22:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 494158D0009; Mon, 5 Aug 2024 11:22:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 443798D0001; Mon, 5 Aug 2024 11:22:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E5A38D0009; Mon, 5 Aug 2024 11:22:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0E39B8D0001 for ; Mon, 5 Aug 2024 11:22:41 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C3BCD1A0297 for ; Mon, 5 Aug 2024 15:22:40 +0000 (UTC) X-FDA: 82418558880.10.7026818 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id 31EF98001F for ; Mon, 5 Aug 2024 15:22:38 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uc9cVv7E; spf=pass (imf02.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722871351; 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=TNMHTiQPDsOotoqPWiY+akbD6XzPr/Jtnfk/mP8IDw4=; b=diz9w+wUDfkHuZofbkfIBnpesNAdDeT8whumsOgotuqPWq0aSlc8iFYocEo5dXeRUjKecg MXSNMZowLPlQUf15jh60WQFDiGAY/NlNZFe8+xK8HrIQOSTSQhLvEr6xWgVoMeqBSle/fj HXvwbfIg6sMNWQKbNgDjhl8WcTSGJLM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uc9cVv7E; spf=pass (imf02.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722871351; a=rsa-sha256; cv=none; b=0EuUs9OrOTZyTFmtuUmXhZvps47fWHnwiuAIb+N7AHmoFisdu24MX+XxVdYgz+zQDTHLy9 X28EbvIhq4KlQdpPPKfurSOpRm3LLYAX0MLXpCgsV2nDw+NrFdg3nnSEUrr5V6erXNEgdc v2AsiRvoQSzF90M1Zj2kSfHOux4NKNk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 56A8660C8F; Mon, 5 Aug 2024 15:22:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE08AC32782; Mon, 5 Aug 2024 15:22:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722871358; bh=6dLN6QohX0ktcU4cWe7TRCkIGusnQOXkZAPpBQ1FLWU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uc9cVv7EEGEFQxjC4dwhg3zVwyiZ6SxOToMFUCRbQVqM16pUaXc+fZlyaIuu7Frtu NDJPcUyyZsri9m6uxCaouTJDUyTn+TWuVOk9yGLqOPI8Hv8+Fz9y0bT5pvkaO4Q92u N6maC4dN3Y+PW6mixAbH4phOt+o6zomDCYBcUaUyQc0VZI+BuzVDShLPiaoTLNIxPA ADzFXCXhJEV/3uBxq0mK01Cm43A4x58uvAMs0f0RqRGngixezRZG+Ib22n4Q1Wfstm 4L0sh3jjULPRSEozDeAdICVRsE9LtDVeLSGocoEd0pjyL+E/2FdsA88HTkU86M33oG oXaa/CY92g80g== 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, acurrid@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 v4 26/28] rust: str: test: replace `alloc::format` Date: Mon, 5 Aug 2024 17:19:45 +0200 Message-ID: <20240805152004.5039-27-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240805152004.5039-1-dakr@kernel.org> References: <20240805152004.5039-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: tekt46je1r16xz5zw4y6f5sn68rker13 X-Rspamd-Queue-Id: 31EF98001F X-Rspamd-Server: rspam11 X-HE-Tag: 1722871358-573118 X-HE-Meta: U2FsdGVkX19Hji1OTD5VRwwANC7RR9nPXglX0SFI5wnhiwCA5/SG+X3ZZgMubIMcp0/JMbOQCO/zEbqQ7rNFrsed2YamkS7KN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+jT82BQUre+9mU8vFGr05Pwc2S038WcA19HrdMXDtEsy9GYvzbD2F7oAkEoonvEw7uqXkUp53gvT0zao2Gtn80/jHnYClXW4h3SgXXNW7CoNp6zSKqve7AJSr00+JrndBOl4ZBXcTGfST9ng== 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`. Signed-off-by: Danilo Krummrich --- 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 0b6ffbade521..8f234224cbf5 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\