Re: [PATCH v12 3/5] rust: add bitmap API.

From: Alice Ryhl
Date: Thu Jun 19 2025 - 06:51:09 EST


On Thu, Jun 19, 2025 at 11:49 AM Burak Emir <bqe@xxxxxxxxxx> wrote:
>
> On Mon, Jun 16, 2025 at 12:49 PM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> >
> > On Wed, Jun 11, 2025 at 9:48 PM Burak Emir <bqe@xxxxxxxxxx> wrote:
> > > +impl core::ops::DerefMut for Bitmap {
> > > + fn deref_mut(&mut self) -> &mut CBitmap {
> > > + let ptr = if self.nbits <= bindings::BITS_PER_LONG as _ {
> > > + // SAFETY: Bitmap is represented inline.
> > > + unsafe { core::ptr::addr_of_mut!(self.repr.bitmap) }
> > > + } else {
> > > + // SAFETY: Bitmap is represented as array of `unsigned long`.
> > > + unsafe { self.repr.ptr.as_mut() }
> >
> > Nit: You want NonNull::as_mut_ptr() here.
>
> Can you explain? That seems to be an unstable method that exists so
> one can get pointer to slice buffer.
>
> The repr.ptr case is NonNull<usize>, not a slice - though "morally",
> it is actually an owned C array.
> Are you suggesting we could/should represent it as a Rust one?
> However, we'd like it to use the C API to free etc.

Sorry I meant NonNull::as_ptr() which returns a mutable pointer:
https://doc.rust-lang.org/stable/std/ptr/struct.NonNull.html#method.as_ptr

Alice