From patchwork Tue Oct 1 15:00:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13818245 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 AE97CCEACDC for ; Tue, 1 Oct 2024 15:02:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40FFE68001D; Tue, 1 Oct 2024 11:02:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BFDB280068; Tue, 1 Oct 2024 11:02:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 238E568001D; Tue, 1 Oct 2024 11:02:40 -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 F3A80280068 for ; Tue, 1 Oct 2024 11:02:39 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A979F1614FB for ; Tue, 1 Oct 2024 15:02:39 +0000 (UTC) X-FDA: 82625350038.21.D0DB53C Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf26.hostedemail.com (Postfix) with ESMTP id 040D9140018 for ; Tue, 1 Oct 2024 15:02:37 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=r1xNcO7w; spf=pass (imf26.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 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=1727794831; 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=qBdAA1MHAXxVW3QJV82szD4rRVx9kb1ZWjIJ/ELeRdY=; b=uPoApdHa2CLrI3DXS1hdctQqa7XufPfkT9AgZi0nJ1UBFy4FqFrhM7tcECnHdIAQ7hEnay wNqS6VoiQV7Pg7K6F3prh+1PokM7OR7xXrQStG9xTFbij0ATJiRyWWjriGwI3J7yx9hzgO aWBhzF13DLKTVcK5IUS469jbvafdvVU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727794831; a=rsa-sha256; cv=none; b=zsGNRFqm5PfbS98RSTBkpTy0aMQYqfFS50SgAY3oJv9gCzWdmYJzLUXaoC1D5SarRyj4nU YIKEowgPNhoGwzNXfQ8tEPMEY9/oDzIfohiD+vSWogDLsSf6hpI6HrT69LPcNfZvHSc2r9 huPpYnDNWMbOza13j5htSbZJ6yFxrvg= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=r1xNcO7w; spf=pass (imf26.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 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 nyc.source.kernel.org (Postfix) with ESMTP id 14D83A400EF; Tue, 1 Oct 2024 15:02:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 226F6C4CECD; Tue, 1 Oct 2024 15:02:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727794957; bh=rMRoTLbwJY/KNbnwZeqVlUvQeXlcU9VSnqsWXV9nz8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r1xNcO7wmDj+pX9zXKSF5zGwWUM4O85kJZ5cWPAxgO9+xb910r5UXJlLXxC1BXTUT zzBp0tTw/b1CeJHxrZy8zQ/bdjKcIOnTq8eOvJIpeXw1cMsIqUvNt3L7SIRSn61PTl ULpwj3RBaA4YjI5NczO3MZgxSOju7nslA/iHjIWPgpSrQwMOvY7je1ZvCyoage2RRw DsL33UucQCuqwoMwJtQaQmGtNWSnzyiAGCJPNjlAEeuoZGEE8Di3jHuGZ+YB5HwTSW 2uu10sTfzzqUOW5Can8mKlVOL3Q76hTPPjmlVHG5LSSsr9tPb5YkMuuza15LjJbHWB hsHNBft18qx1A== 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 v8 26/29] rust: str: test: replace `alloc::format` Date: Tue, 1 Oct 2024 17:00:01 +0200 Message-ID: <20241001150008.183102-27-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241001150008.183102-1-dakr@kernel.org> References: <20241001150008.183102-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 040D9140018 X-Stat-Signature: ufiawbenk3a6rth77w914spk16y46h35 X-HE-Tag: 1727794957-301708 X-HE-Meta: U2FsdGVkX1870f0p/ACZqRS4yF74AKbG9IeA4s0Zd+IWVYqtptHTpw8YM17+Xj+TeTsbmhh9sOLhxjPXwj3YrM/FvHh65IK79z5WN3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YAgHaqyomFBUbn5mwRxuoTslUrEBnhSyXMOziTIvlgU9C6WeeG8PbqrFjGMo7HMHXEx+wLmG/24kyzwUs5plWyJBAOu+2GErs2bQW/fCIM4LCdrsNW/VJV7A+Y9ZeIk9f61nV/HlgV/eX4216eqHMmSNewnZWlrm3f/Jkz46aCLVj0WfuNxYGx1I8f7TSg 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 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\