All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>
Cc: <rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v4 1/3] rust: ptr: add `KnownSize` trait to support DST size info extraction
Date: Mon, 02 Mar 2026 19:56:06 +0100	[thread overview]
Message-ID: <DGSIXGQW5ALN.226YQUWPHZXVC@kernel.org> (raw)
In-Reply-To: <20260302164239.284084-2-gary@kernel.org>

On Mon Mar 2, 2026 at 5:42 PM CET, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
>
> Add a `KnownSize` trait which is used obtain a size from a raw pointer's
> metadata. This makes it possible to obtain size information on a raw slice
> pointer. This is similar to Rust `core::mem::size_of_val_raw` which is not
> yet stable.
>
> Signed-off-by: Gary Guo <gary@garyguo.net>

Reviewed-by: Benno Lossin <lossin@kernel.org>

Found a typo below.

> ---
>  rust/kernel/lib.rs |  1 +
>  rust/kernel/ptr.rs | 27 ++++++++++++++++++++++++++-
>  2 files changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 3da92f18f4ee..510cc7fe4961 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -20,6 +20,7 @@
>  #![feature(generic_nonzero)]
>  #![feature(inline_const)]
>  #![feature(pointer_is_aligned)]
> +#![feature(slice_ptr_len)]
>  //
>  // Stable since Rust 1.80.0.
>  #![feature(slice_flatten)]
> diff --git a/rust/kernel/ptr.rs b/rust/kernel/ptr.rs
> index 5b6a382637fe..cf980a103acf 100644
> --- a/rust/kernel/ptr.rs
> +++ b/rust/kernel/ptr.rs
> @@ -2,7 +2,10 @@
>  
>  //! Types and functions to work with pointers and addresses.
>  
> -use core::mem::align_of;
> +use core::mem::{
> +    align_of,
> +    size_of, //
> +};
>  use core::num::NonZero;
>  
>  /// Type representing an alignment, which is always a power of two.
> @@ -225,3 +228,25 @@ fn align_up(self, alignment: Alignment) -> Option<Self> {
>  }
>  
>  impl_alignable_uint!(u8, u16, u32, u64, usize);
> +
> +/// Trait to represent compile-time known size information.
> +///
> +/// This is a generalization of what [`size_of`] which works for dynamically sized types.

s/what//

Cheers,
Benno

> +pub trait KnownSize {
> +    /// Get the size of an object of this type in bytes, with the metadata of the given pointer.
> +    fn size(p: *const Self) -> usize;
> +}
> +
> +impl<T> KnownSize for T {
> +    #[inline(always)]
> +    fn size(_: *const Self) -> usize {
> +        size_of::<T>()
> +    }
> +}
> +
> +impl<T> KnownSize for [T] {
> +    #[inline(always)]
> +    fn size(p: *const Self) -> usize {
> +        p.len() * size_of::<T>()
> +    }
> +}


  reply	other threads:[~2026-03-02 18:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-02 16:42 [PATCH v4 0/3] rust: add pointer projection infrastructure and convert DMA Gary Guo
2026-03-02 16:42 ` [PATCH v4 1/3] rust: ptr: add `KnownSize` trait to support DST size info extraction Gary Guo
2026-03-02 18:56   ` Benno Lossin [this message]
2026-03-02 18:57   ` Benno Lossin
2026-03-02 19:42     ` Gary Guo
2026-03-02 21:52       ` Benno Lossin
2026-03-02 16:42 ` [PATCH v4 2/3] rust: ptr: add projection infrastructure Gary Guo
2026-03-02 19:02   ` Benno Lossin
2026-03-03 21:11   ` Miguel Ojeda
2026-03-02 16:42 ` [PATCH v4 3/3] rust: dma: use pointer projection infra for `dma_{read,write}` macro Gary Guo
2026-03-02 16:42   ` [PATCH v4 3/3] rust: dma: use pointer projection infra for `dma_{read, write}` macro Gary Guo
2026-03-03 21:13 ` [PATCH v4 0/3] rust: add pointer projection infrastructure and convert DMA Miguel Ojeda
2026-03-07 22:29 ` Danilo Krummrich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DGSIXGQW5ALN.226YQUWPHZXVC@kernel.org \
    --to=lossin@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.