From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EBF11C8604; Sun, 27 Jul 2025 12:39:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753619984; cv=none; b=nqJmtakez7rphHp2DZLhqWG8BBNfaqinFF33G+xypd3m3qlvtFQrPf+sa261hM7t7i1f5FYohiGvX8/Kt+bKya5KsEWx0PfBQpATv41thlZ+SMN/HvFMOpCH8YnvHeDYyHdZ+5a39BNm+EwKujAzWYjvEDnI3nazeq67QQgreyI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753619984; c=relaxed/simple; bh=fIXQgL9YEiRqNQxiYSID7ZjBcy7GeSQhzu+5YaUrg7k=; h=Mime-Version:Content-Type:Date:Message-Id:Subject:From:To:Cc: References:In-Reply-To; b=oprDB4CGhlZqVcAdWLOfYc10ea6M/SOHI6nTn18AnMu5s6ubL5e4zNVCt5ycRhAfhmX2nW+TLpUdQLzIi+ye/ZBj+rl1JRZ/NZuS7y0YEckyH2HbsmV9HLO/kfv1OexdT5YpbY7OfSXTzKZEnDOXhWKEHlk3barYOxbYPExxTxM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rv5+0JLf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rv5+0JLf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F47AC4CEEB; Sun, 27 Jul 2025 12:39:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753619984; bh=fIXQgL9YEiRqNQxiYSID7ZjBcy7GeSQhzu+5YaUrg7k=; h=Date:Subject:From:To:Cc:References:In-Reply-To:From; b=rv5+0JLf03BsZyyLJ4SqD+8fQmTbff6WjrWDLgui+5tTMtEUh1+ZAO4E6B5O5QEl7 InAR8jpKa/1vowj+PtEBGZTTYRBnhNiUubAAlbmlsBqQrOvXxwlNHVLVMxZqsfpeg3 /pZKe3ssPKCGJH9cZNnHtbmkSxIFrD79/vtK99lUAL/J/qYfXl9RlgR4i1sTNaLhfN u1wV97EdaiAYKgoE/11lgpl+xH9OvwWW2PcNfIGLtlrePtA4QqjUpFfijIhjCm68fp 1eyNY2v7HcDAE608nli7ceE3hUkTObOGpeK8LFhWorPZk83xtUT2l7rk90JgC71tGf DamYTvpVaB9hg== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sun, 27 Jul 2025 14:39:39 +0200 Message-Id: Subject: Re: [PATCH v3] rust: transmute: add `as_bytes` method for `AsBytes` trait From: "Benno Lossin" To: "Alice Ryhl" , "Alexandre Courbot" Cc: "Abdiel Janulgue" , "Danilo Krummrich" , "Daniel Almeida" , "Robin Murphy" , "Andreas Hindborg" , "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Trevor Gross" , "Christian S. Lima" , , X-Mailer: aerc 0.20.1 References: <20250726-as_bytes-v3-1-eb7514faab28@nvidia.com> In-Reply-To: On Sun Jul 27, 2025 at 12:23 PM CEST, Alice Ryhl wrote: > On Sun, Jul 27, 2025 at 08:52:00AM +0200, Alice Ryhl wrote: >> On Sat, Jul 26, 2025 at 4:47=E2=80=AFAM Alexandre Courbot wrote: >> > diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs >> > index 1c7d43771a37b90150de86699f114a2ffb84db91..69c46c19a89191d8a2abc5= 801564cacda232218c 100644 >> > --- a/rust/kernel/transmute.rs >> > +++ b/rust/kernel/transmute.rs >> > @@ -47,7 +47,16 @@ macro_rules! impl_frombytes { >> > /// >> > /// Values of this type may not contain any uninitialized bytes. This= type must not have interior >> > /// mutability. >> > -pub unsafe trait AsBytes {} >> > +pub unsafe trait AsBytes { >> > + /// Returns `self` as a slice of bytes. >> > + fn as_bytes(&self) -> &[u8] { >> > + let data =3D core::ptr::from_ref(self).cast::(); >> > + let len =3D size_of_val(self); >> > + >> > + // SAFETY: `data` is non-null and valid for `len * sizeof::()` bytes. >> > + unsafe { core::slice::from_raw_parts(data, len) } >> > + } >> > +} >>=20 >> Let's also have an as_bytes_mut() method. I would require the type to >> also implement FromBytes as it lets you replace the value with another >> set of bytes. > > s/I would/It would/ > > FromBytes is needed only for as_bytes_mut(), not for the existing > method. I agree with your suggestion, but it can be an independent patch and doesn't need to go in via this one, right? --- Cheers, Benno