From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 C416F1F5842 for ; Sun, 27 Jul 2025 10:23:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753611816; cv=none; b=oDg+RjIgjhDSNm3r7taAcXn8OIDcu9qACknTzwhz3/C0UhDX8EoOovh16iS/TL3aj5djaJkym3PrgT4jhqFRrKJossLfAUHMKD+fE2LOkY+UoypxApFWZGdUSvRLE0BxFUi8jH1VP4YoiWJhrJsgXnG2LODBbnv4iGKmdgihg9s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753611816; c=relaxed/simple; bh=wKZ+wdSVT2zLismPrTnYeNtClZj0rpeLkvM39+Aizn0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tV5q7us13JF6GaZkie9H34lDkJ8w7A1mNPvzhuYagnvNRnh17X+sp4EJdfD5i5DvLoIity6rVtOvwkU+/5+PilZjsqi3XicEweOS7uetkUAoXtiYzMCQxAKBOL1sdd1gJS7zvmjYdrb7+Te2WzJkTxkCNBfJ7KaeGOwAOSmZkJo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=o/4zY23I; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="o/4zY23I" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-45611579300so23819305e9.0 for ; Sun, 27 Jul 2025 03:23:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753611813; x=1754216613; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=t4iTebxkYeknnu3eio38CNOGaV1IEcupvHvkpN3fyAc=; b=o/4zY23IOf05RsQzHpr6pBNhFgfvnPFk9+uawAzHjOlPUzu6yhGHbLg+/niV8LGwos hrr2rAvEreo9NrF03mE+JB1mEjYRg/GO5PUsigRWlwq4fbEbI28NN18p9A6oBUxNfIjG VVdoUcJNNwgCDjikooma4ZIs/r9wN1UA3B71/PSUAopq+w3Bbq9bVgVSL0hmx/act2B5 782+0vye8tT9MlRth0uuBsFac18hohbkxTsmUe3FIcPaxiBqAX+yr9m4kmfE6jkl32r5 DjqE7uNJ23RXtMAHiiQwi6pwM9jtrv0yQms6ZuOnIFgOvry7hjzqzBi2q8DL6cjTPJgV X01w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753611813; x=1754216613; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=t4iTebxkYeknnu3eio38CNOGaV1IEcupvHvkpN3fyAc=; b=kOzQKS2fjWimwWGzJtk0eEast9pxLgnp5LirHO2YWVdwT7I/GLLMgylvJ3TJDKMDFS zr1lCiF7h/seekIfP089oXrtetWJTZOiG3pS03C/7OBpBVpsPy02el1oZaG6w/rRsnOc hTYOZySXv3gfp5xdUi1n1sr8qOnohlCipXFjqlNQ+TMYErkWpnWe1CwmU1xZk9p/hScp tWqOvqXkQJef8T+WFIK69VvKAefYjAYAU38GskvAAv/EJIqhjotqnx8vHOIRj8BvKuSe flsw7gi5aZGmSdakhDh6Ht80l4xyFlfsWHhDLaQO7fGoiD4X3tetXPkL7quLXLyREhg1 9KMw== X-Forwarded-Encrypted: i=1; AJvYcCXn28iuOehM3qPi0skPaZnrgmrOZeMJhViwSvSgXbn9CKcKbpYxWOr8s/Boj9U/+ZM6HqyFGCHZ5+3FhgML7A==@vger.kernel.org X-Gm-Message-State: AOJu0Yyi5JK2wDswJN30rOWrLYO+fHgE+MRKrP6RUxtSl0P/LcCaHV7E zpM3qX0tGucASbf6lMbFrGxgHbL3YJ5PAZl/OvXhynSagNiSeOgaE9FeowN0GEjR3m3NPCisQyu SkvEv6yCz9CE2OwG5gA== X-Google-Smtp-Source: AGHT+IFcALmF/+LSBw+Is6iiYMTn1WPw/8ZrhL7tFBQSfUm+sfoyiXw3npeFqmKQRVU7yPgTjyXYfaMGGkptdUQ= X-Received: from wmbem10.prod.google.com ([2002:a05:600c:820a:b0:458:6553:5a99]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:450e:b0:456:1c4a:82b2 with SMTP id 5b1f17b1804b1-4587630f5a6mr81488465e9.10.1753611813207; Sun, 27 Jul 2025 03:23:33 -0700 (PDT) Date: Sun, 27 Jul 2025 10:23:32 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250726-as_bytes-v3-1-eb7514faab28@nvidia.com> Message-ID: Subject: Re: [PATCH v3] rust: transmute: add `as_bytes` method for `AsBytes` trait From: Alice Ryhl To: Alexandre Courbot Cc: Abdiel Janulgue , Danilo Krummrich , Daniel Almeida , Robin Murphy , Andreas Hindborg , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Trevor Gross , "Christian S. Lima" , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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: > > > > Every type that implements `AsBytes` should be able to provide its byte > > representation. Introduce the `as_bytes` method that returns the > > implementer as a stream of bytes, and provide a default implementation > > that should be suitable for any type that satisfies `AsBytes`'s safety > > requirements. > > > > Reviewed-by: Danilo Krummrich > > Signed-off-by: Alexandre Courbot > > --- > > This is the sister patch of [1], providing an `as_bytes` method for > > `AsBytes`. > > > > It is going to be used in Nova, but should also be universally useful - > > if anything, it felt a bit strange that `AsBytes` did not provide this > > method so far. > > > > [1] https://lore.kernel.org/rust-for-linux/20250624042802.105623-1-chri= stiansantoslima21@gmail.com/ > > --- > > Changes in v3: > > - Use `ptr::from_ref` instead of `as *const T`. > > - Link to v2: https://lore.kernel.org/r/20250725-as_bytes-v2-1-c6584c21= 1a6c@nvidia.com > > > > Changes in v2: > > - Use `size_of_val` to provide a default implementation for both `Sized= ` > > and non-`Sized` types, and remove `AsBytesSized`. (thanks Alice!) > > - Link to v1: https://lore.kernel.org/r/20250725-as_bytes-v1-1-6f06a374= 4f69@nvidia.com > > --- > > rust/kernel/transmute.rs | 11 ++++++++++- > > 1 file changed, 10 insertions(+), 1 deletion(-) > > > > diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs > > index 1c7d43771a37b90150de86699f114a2ffb84db91..69c46c19a89191d8a2abc58= 01564cacda232218c 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. Alice