Re: [PATCH v4 12/15] rust: block: add `GenDisk` private data support

From: Alice Ryhl
Date: Wed Aug 13 2025 - 09:51:25 EST


On Wed, Aug 13, 2025 at 3:47 PM Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
>
> "Alice Ryhl" <aliceryhl@xxxxxxxxxx> writes:
>
> > On Tue, Aug 12, 2025 at 10:44:30AM +0200, Andreas Hindborg wrote:
> >> Allow users of the rust block device driver API to install private data in
> >> the `GenDisk` structure.
> >>
> >> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
> >
> > Overall LGTM.
> > Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> >
> >> self,
> >> name: fmt::Arguments<'_>,
> >> tagset: Arc<TagSet<T>>,
> >> + queue_data: T::QueueData,
> >> ) -> Result<GenDisk<T>> {
> >> + let data = queue_data.into_foreign();
> >> + let recover_data = ScopeGuard::new(|| {
> >> + drop(
> >> + // SAFETY: T::QueueData was created by the call to `into_foreign()` above
> >> + unsafe { T::QueueData::from_foreign(data) },
> >> + );
> >
> > This is usually formatted as:
> >
> > // SAFETY: T::QueueData was created by the call to `into_foreign()` above
> > drop(unsafe { T::QueueData::from_foreign(data) });
>
> I don't really have a preference, my optimization function was to
> minimize distance to the unsafe block. Are there any rust guidelines on this?

I would say that the unsafe keyword just has to be on the next line
from the safety comment. Optimizing further than that leads to wonky
formatting. A similar example that I also think is going too far:

let var =
// SAFETY: bla bla
unsafe { value };

Alice