Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp26169rdb; Wed, 14 Feb 2024 11:29:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUopyfouTcbZFFC3B0ipXTcErlWCfZpC4J1u9o63vmRdn2V2SSVzFZodNHVe/UHA9oYswQsd7q6gUV22BmP0lXR4VbFG0agg1TpcDQB3w== X-Google-Smtp-Source: AGHT+IFTwbWhHc4KOzZkK2x+W4WgtXf0njv/LKqXDbW9FvNZOk/bOiDdKN5FWJKBfkJBfIxGwRBa X-Received: by 2002:a17:906:11d8:b0:a3b:e9d9:bf6a with SMTP id o24-20020a17090611d800b00a3be9d9bf6amr2122166eja.40.1707938988949; Wed, 14 Feb 2024 11:29:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707938988; cv=pass; d=google.com; s=arc-20160816; b=r7Yw92fqdZcoJYFY0zwcMMV9erwG3K0x/Y3wl7YoqUnXoko96UqnaZzCAR9MCvJPaW QgUAVpK3dzQa8ie0M2iLp4rX1/RF+p4Swlj9MdKx7rPbqVqSeUvB/oFSgqrK3FeiWd1i uIInKOKopM1eJN58kERDkHLAskbU7XrN0aD075z1atdJ01hVey5zuBQmWKNusI+6wXYk qnTTtf+tMGkJFihHfqAbAWm2JWS9gA8VlwaSb6N3wEIqG+VhmnOYERjgbjUkQKZ/FiYS sb25MLkzH6c+3TU0P8/T0e8asNTE3OVPQ7QaKHwSrgdpiY+7B3MlPxBnidVGmIYVwynt HvTA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=bxKxd0YOf3Ety49fAv0PO6SpvuAL0dCmkIegxDtI/W8=; fh=/rcJxvrAS4QYgyQzCAGCHwJC7Kqz8J6l9XI63CHBGNM=; b=y6G/oxD6FDyFIxeImCrgCA2kY9K0RIt6Xy1a5mGOA+Jl4dyX1N3LgflfvX1c2lBUhN cUzysJwh947dmy2z+bNtnxDTG1L3EJEb8luiTliDrWaTAMK5ZL6bJyb2rajn4zPwEcg6 OkVRfOdY1jSYfwjnongTcnzACMwvErOiY2YohG5VQwr/FvplBJmuCk87QmY3T5y4n/pJ HnwsTsyKomdAH/sKAycDZ8QEH9K7W9rFMm/1haZ7MMsMPnZqIjSlVzssotdGvW/h7CmJ E4Npe0sD7BPYiIMZQwC5mYiiurdk7dX4PtjySQ+zDPttM0FRlRJpfKGC6yOwAWpBWXo2 Wa2g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=G3eWZzYN; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-65881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65881-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com X-Forwarded-Encrypted: i=2; AJvYcCXxb61aBsvPnXX91S2izdXS31PY1Lz14yl5Nit4YZId1baqd9lveEB436etpqKuwDv4KlnY7WiiARW6oc7+RFRPBefU6uaLHphgvF7maw== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id k12-20020a1709065fcc00b00a3d317ee138si1140361ejv.61.2024.02.14.11.29.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 11:29:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=G3eWZzYN; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-65881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65881-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 7D7D71F26086 for ; Wed, 14 Feb 2024 19:29:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7681813B7A5; Wed, 14 Feb 2024 19:29:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G3eWZzYN" Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6F6412D76E; Wed, 14 Feb 2024 19:29:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707938978; cv=none; b=bDLOmUZXs/4iL4QZys9CALo5oI6nStJIpNegc17Eoyir216y3ty7nnYsIYmX8yPvCryzeM6hQJzQth1oxKeeZDE1TTtqibZmF0RmI53waIH69c3cWkJzUZwNTwa/wJFjc0BXvxNMDrRspFCS+whP5OUxXt/FNd/0DwLtJ1rBTjE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707938978; c=relaxed/simple; bh=Nu/AiYO0cZHPXxcq7qHdK3J+BhmArbnGSgTitnJB4/w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XmNSJyiQCkQ6X1HVSWqiD/x9/VWWOAJctpzm0UrrZyFEzHZK2mJFaBz5SZaqdOyl8Bb/AMf9Y8tJErayiAq2K9pxGj0Ho2OV7NN7oqOhx+5PwL2AHKBhgdTl6c7Cjh2T+LRcvA39h6rc2tL30r7Hp9q3fdhfKmigMmpaEX1mWyU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G3eWZzYN; arc=none smtp.client-ip=209.85.219.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-68ce2fe86e3so698266d6.3; Wed, 14 Feb 2024 11:29:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707938976; x=1708543776; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=bxKxd0YOf3Ety49fAv0PO6SpvuAL0dCmkIegxDtI/W8=; b=G3eWZzYNaS2sIh2QjBPVkSq8KmF4bCMudktCfS7HT5/N1+OcU/cT+I5YLwpEysKmZ+ Tf1DVPxGCMCl5j2xcxkejcLK72qBt/VO/07yvk1ntX90GFJ5hUQvRlxt2ZoJu0JIgR4R YtgxQkpmFwPoZHWMU16BAuxdIOK5ykQa7g6mJ/3kfNgd6uSw3L9Y1G6E6kiA7lz8acNl hgzW+r4IwuBMXaMGSsTU+pST0QCCfzZU12AL9QIgEni/byxwDxdvV46+l7oOwf5+Ggau SvBvJM3aIDMGrANthisLFiXNOLtfaR30I70oPR0jya6FcL/4ePugCQE5/hMQgOlykilv NNdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707938976; x=1708543776; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bxKxd0YOf3Ety49fAv0PO6SpvuAL0dCmkIegxDtI/W8=; b=WpCc29GUjfJFmspWqUZG2THSqkyJ3AgqTrZH6WJV29Gfflqw1uW2QrKh9CtqwL4/bC ewKM1ScrSTtsAm847OVANcFxvaAnCmkXuJGsUifnMvK0AvIR3Ueb4hdRf1x5NfxkC/bA nRkvMuvHLTMdS8GCLKf6uuXVe4uyYvBc5b3Nw9ut/xpkZmAUqfY7WNmb5gjzJbm5xCVl I0l3RvcgBJYWAAhxP/6rCr9RvqHGvI+TtLUi21AQauBEUPi9QRbd9lyZ0SS6aw9AjRbp DhIQuJvgbrQdUEsDrl/RnRYWPUFTguHKB6l3LURnqOLHikg0LdSZzk5Fwh0/6KzupjWV qHvQ== X-Forwarded-Encrypted: i=1; AJvYcCVZihp45Slsu+GbVCPpvvFYgCnNesDRAXdwhJUu3u2YQqOn41fx8cGX48MqNz4X0R1+XhcAINt9O1BodsT7rlbheXZgqGuCyk/diY4ZOWJ+6J3MH7TxEJaUspqe0okl22hhbyfZHp0LjBoGTus= X-Gm-Message-State: AOJu0YyOf6E8fw3gH4On7U648YuzOrGtbXeElqz8imI2/Ndk4T4aO8OA S+u8/oa2byF4FTLlc0VQ2EBlUdJ817d7Ant15w9euVGYjqT2bmmJ X-Received: by 2002:a0c:cb0c:0:b0:68f:10bf:6ec9 with SMTP id o12-20020a0ccb0c000000b0068f10bf6ec9mr863632qvk.13.1707938975741; Wed, 14 Feb 2024 11:29:35 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVo4GFgA+hc09NEfLHstdTrSzSAKE5leRPjHoIEvGVv8+hQwzV6TnbBhi71fBYjns5hCpYrVOJxMvT9z4+L0XXLR+yQZaHk2XXcTU8Bh1gq68SzaKLNvZ27fWc4i7MztR0ZnppI+l5apDBp2HLU5pQ/w0AEFlBwMryQmjrstyHdJiF+p78xdPHQIWVyQ7v8Xb++xVjC+gNc8FKZV/+NkWwAfsBtUSr9IEAK5fheZ7mP839UUbTW1ZoMLigyV03Uube9XrJoIlqbD3+dmTdWQ6RI6rAgiXVQ7KoC+66OJMav5cDxM3jAgEhGDlKV8S0JEolPQ2hgX1uXmBXm1vLvh+o9anZdneF1ck26Vw+wGw/ycihKy6c8M95p/hO+ Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id d8-20020a0cf6c8000000b0068cbc630dc8sm2555319qvo.49.2024.02.14.11.29.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 11:29:35 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfauth.nyi.internal (Postfix) with ESMTP id 8D2441200066; Wed, 14 Feb 2024 14:29:34 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 14 Feb 2024 14:29:34 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudejgdduvdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeejtefftdfgueehgffhkedttddtgeetvdeukeetueeggffhhefffeeuledu ieefveenucffohhmrghinhepiihulhhiphgthhgrthdrtghomhenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgr uhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsoh hquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 14 Feb 2024 14:29:34 -0500 (EST) Date: Wed, 14 Feb 2024 11:27:49 -0800 From: Boqun Feng To: Danilo Krummrich Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] rust: str: add {make,to}_{upper,lower}case() to CString Message-ID: References: <20240214172505.5044-1-dakr@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240214172505.5044-1-dakr@redhat.com> On Wed, Feb 14, 2024 at 06:24:10PM +0100, Danilo Krummrich wrote: > Add functions to convert a CString to upper- / lowercase, either > in-place or by creating a copy of the original CString. > > Naming followes the one from the Rust stdlib, where functions starting > with 'to' create a copy and functions starting with 'make' perform an > in-place conversion. > > This is required by the Nova project (GSP only Rust successor of > Nouveau) to convert stringified enum values (representing different GPU > chipsets) to strings in order to generate the corresponding firmware > paths. See also [1]. > > [1] https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/String.20manipulation.20in.20kernel.20Rust > > Signed-off-by: Danilo Krummrich > --- > Changes in V3: > - add an `impl DerefMut for CString`, such that these functions can be defined > for `CStr` as `&mut self` and still be called on a `CString` > Changes in V2: > - expand commit message mentioning the use case > - expand function doc comments to match the ones from Rust's stdlib > - rename to_* to make_* and add the actual to_* implementations > --- > rust/kernel/str.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 80 insertions(+), 1 deletion(-) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index 7d848b83add4..02d6e510b852 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -5,7 +5,7 @@ > use alloc::alloc::AllocError; > use alloc::vec::Vec; > use core::fmt::{self, Write}; > -use core::ops::{self, Deref, Index}; > +use core::ops::{self, Deref, DerefMut, Index}; > > use crate::{ > bindings, > @@ -143,6 +143,19 @@ pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, CStrConvertError > unsafe { core::mem::transmute(bytes) } > } > > + /// Creates a mutable [`CStr`] from a `[u8]` without performing any > + /// additional checks. > + /// > + /// # Safety > + /// > + /// `bytes` *must* end with a `NUL` byte, and should only have a single > + /// `NUL` byte (or the string will be truncated). > + #[inline] > + pub const unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr { > + // SAFETY: Properties of `bytes` guaranteed by the safety precondition. > + unsafe { &mut *(bytes as *mut [u8] as *mut CStr) } First `.cast::<[u8]>().cast::()` is preferred than `as`. Besides, I think the dereference (or reborrow) is only safe if `CStr` is `#[repr(transparent)]. I.e. #[repr(transparent)] pub struct CStr([u8]); with that you can implement the function as (you can still use `cast()` implementation, but I sometimes find `transmute` is more simple). pub const unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr { // SAFETY: `CStr` is transparent to `[u8]`, so the transmute is // safe to do, and per the function safety requirement, `bytes` // is a valid `CStr`. unsafe { core::mem::transmute(bytes) } } but this is just my thought, better wait for others' feedback as well. Regards, Boqun