From patchwork Fri Aug 16 00:11:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13765304 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 DC497C52D7D for ; Fri, 16 Aug 2024 00:14:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A9FE8D0027; Thu, 15 Aug 2024 20:14:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 656698D0002; Thu, 15 Aug 2024 20:14:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F8BE8D0027; Thu, 15 Aug 2024 20:14:33 -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 2D25C8D0002 for ; Thu, 15 Aug 2024 20:14:33 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DA391818EE for ; Fri, 16 Aug 2024 00:14:32 +0000 (UTC) X-FDA: 82456187184.25.19D59E9 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf27.hostedemail.com (Postfix) with ESMTP id DA8934000A for ; Fri, 16 Aug 2024 00:14:30 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ExCVsShO; spf=pass (imf27.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1723767234; a=rsa-sha256; cv=none; b=sALcTHygaacdbM1S0Zs5UoouYHZewpXqXGQEo/vmGsOH2N8PgMc9w3FlhpRpS2rzBpaWtU 8NCIT1bKpUb7WIa46Etv8UBFpwISETQEN4A68hUZiF1Lj+WRh+Sl0MrkrDmUhxYjqGe954 Vw68iiDDC3IibUs9H85C8W10kxmUo9A= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ExCVsShO; spf=pass (imf27.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1723767234; 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=0D7FqZUWgIaqAHnwMJev1VjFQsifHHTjQfERRAF0RCs=; b=h2sCiuPsDeKuQ3C6mO58TQQQ9xO5UlggJzsrYowP6weKTWvmUDapYoYIFzKlurPkuxtaYX A22r/9/UqpjpuweNgHWe+l5vllyieXkb7LoL21FYsJt9Cf0XkQ6hArW58NgRbx2xUA/mMP Mo/kMshglCm2s0xJ5vHl5K4DEnBPI+0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 0A938CE1DAC; Fri, 16 Aug 2024 00:14:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 631C6C32786; Fri, 16 Aug 2024 00:14:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723767267; bh=byWvg3/QTModmmEnrau3RUl54V9uowntF04isjcbzmY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ExCVsShOu+MJYyYdh3Bouvz6RbIOq9x/nfwsjA+xfaTBafsQ/7F1iqdP1l+TX/Yjg 65XXWKpcYDzhdg1TpyB2x8z+mDg9uGx0raYVemR9FrzpnNnUYDQx23BL9hGzvaUkTA xfdhGCS//QauVuv56XjPoHXcjgfzT6pkP2WvovLYzPo8yr1x0Xx+E9LcXJa8edt91d UtWk4DypRH1l6gRpe22mvohkuJ6xdwEEsCVLsTGE04coDgZgWtyHHLzAIZFxu60pGL EA7QkBvas09Pq57lxKs53sOQYzML5eG0QRVC2pE5u6wuRvw4Iknw3DUEvAtsfHcERA 7mqZ2lz96lK9g== 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 v6 23/26] rust: str: test: replace `alloc::format` Date: Fri, 16 Aug 2024 02:11:05 +0200 Message-ID: <20240816001216.26575-24-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240816001216.26575-1-dakr@kernel.org> References: <20240816001216.26575-1-dakr@kernel.org> MIME-Version: 1.0 X-Stat-Signature: w5ua6qwkiaswgigwiijyze6pfg9unt3u X-Rspamd-Queue-Id: DA8934000A X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1723767270-461377 X-HE-Meta: U2FsdGVkX19HHBLjdofOm9Hdqqs4YnMb2/+0EfJzLOcZ0qE1j3DXN7Us+kXA8nZCgglQr9OcsrrnhLOuzMoIjSdbPvscatUIT4dXTTNnijO3k2cq3B8mNaz9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7j2NxuomjUrqnPbmf/XmrIydwxBHdmhs/Mhd5pOIpEH0OfLXyeiu1GsfoovTcXPq+fTyVrdE424hNaJ8Af7Bk4iU4dXy3uzo+R6pM2SefIcWMYdvVYDappycv9dg4xA= 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 Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- 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\