From patchwork Fri Oct 4 15:41:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13822637 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 70541CF8849 for ; Fri, 4 Oct 2024 15:44:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F039E8D0009; Fri, 4 Oct 2024 11:44:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8C9D8D0003; Fri, 4 Oct 2024 11:44:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE0D68D0009; Fri, 4 Oct 2024 11:44:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A726E8D0003 for ; Fri, 4 Oct 2024 11:44:31 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6D8524197E for ; Fri, 4 Oct 2024 15:44:31 +0000 (UTC) X-FDA: 82636341942.13.2619571 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id C75E040023 for ; Fri, 4 Oct 2024 15:44:29 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fnNeEXxR; spf=pass (imf01.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=1728056538; 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=ASRYhw9Dg3IAmGN8c5WgCpAoZPW6j+dg/YVKcwHcBmk=; b=xHWvJFbBjAyAGWTcFGjFiWTqJkqmCjhYgPOVett1bcuwp/CHn8ANiZAvZ1qt7VfwXNVdJt ebQej2XqB1YTgcgsQltXYU9AV/GJkNyJjmt0t7fkTGEkTHaD+aV1odQedqn3b9MM6oSNLL VDcJ1bvKdoiD+SYn9y0qItwKbYRcEyM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728056538; a=rsa-sha256; cv=none; b=aCCGSuBrNVMVVxJ2DO/SMht3gOOiv7jfyZ13ZBbp3iUoPzZl6d9daMkPL1IvLKQDg3zVrH 6chscM4apax7V5ohSeZXp3pH9eJhVO5G7SozhOZ+W+3BPG2DGwwucfOSf+8stOvcjd8DPn vI0MXPLNaypAh1WICa1y630E7MvDcm0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fnNeEXxR; spf=pass (imf01.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 D7FA25C5F07; Fri, 4 Oct 2024 15:44:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8C23C4CEC7; Fri, 4 Oct 2024 15:44:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056668; bh=+flMXZQfIhSSa3rmdG7sm/b2ESf50jZzxj0uBuhzVdQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fnNeEXxR7mvP92yIfBbZLKQgeMPUx20LLtjjEtRpZbG5b21h9I1Q266YhULU7we5G nIKnt7Ig2nVe5gPYYo/WacZZO+8P/BkpLtAFIfmf5m6S5lMNkXsZJ4yTPSmqgK7scw /kUUR7mXbJJ8XXO3LP2dNem62eLLl2KpL1XH3Xle8t6v18Drlsi691bL1jeRJWyQR7 4cxmM1JClD1M0QJ2FXgsJ/ZrZEQRzaQYn9D+5MrpBEY9ABXsinpdgYKX9rn2EjyRUz pNO82/M6amgupFvVov/hqqXNHS/+rEi1aMJOiy/7d+sQYdu0oHywrQ7+1ynfWF9luu TmZWjk4vJOhrA== 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 v9 26/29] rust: str: test: replace `alloc::format` Date: Fri, 4 Oct 2024 17:41:30 +0200 Message-ID: <20241004154149.93856-27-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241004154149.93856-1-dakr@kernel.org> References: <20241004154149.93856-1-dakr@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 5u4puxeec4qq4qpdfs4rfsaisqd51qs1 X-Rspamd-Queue-Id: C75E040023 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728056669-279359 X-HE-Meta: U2FsdGVkX1/NDppRz4cZRCKVczD98TQD4NjMYwB1VPgdtB/n5XU9aq71SY7ahIxksl/6w1AWZp7y2cJBZjIBNq1dktJ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2sqWSkXODuRyha0MZmZWk4qJUKectGAXHEzeNyQJrKE1g4BP5IegfGLBGIE1FcNL4YgNedtt7DgeoaSTh57XTeeHdbvK2ts6xuaYoR/nLGi6RjWXV7y3WZEXxMOYB4jYSEdAN4gwD9PKI8JKtGsm/DqEWxtrMX1XvLuPTJtRfnhzkW20O5DGmwHIkT7RdQs2k1SxrfZw606qrMS5y4t3Pc= 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 Reviewed-by: Gary Guo 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 6053bc7a98d1..aff6baa521d4 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -524,7 +524,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\