Re: [PATCH v2 3/5] rust: scatterlist: Add type-state abstraction for sg_table
From: Alice Ryhl
Date: Fri Aug 22 2025 - 07:54:09 EST
On Fri, Aug 22, 2025 at 01:48:47PM +0200, Danilo Krummrich wrote:
> On Fri Aug 22, 2025 at 1:44 PM CEST, Alice Ryhl wrote:
> >> +impl<P> Owned<P>
> >> +where
> >> + for<'a> P: page::AsPageIter<Iter<'a> = VmallocPageIter<'a>> + 'static,
> >> +{
> >> + fn new(
> >> + dev: &Device<Bound>,
> >> + mut pages: P,
> >> + dir: dma::DataDirection,
> >> + flags: alloc::Flags,
> >> + ) -> Result<impl PinInit<Self, Error> + '_> {
> >> + let page_iter = pages.page_iter();
> >> + let size = page_iter.size();
> >
> > Variable naming here is confusing. There's another variable called size
> > in an inner scope, and then afterwards in RawSGTable you use *this* size
> > variable again.
>
> I can change the size in the assignment block of max_segment to max_size, or do
> you have other suggestions?
How about just this?
let max_segment = {
// SAFETY: `dev.as_raw()` is a valid pointer to a `struct device`.
let max_segment = unsafe { bindings::dma_max_mapping_size(dev.as_raw()) };
if max_segment == 0 {
u32::MAX
} else {
u32::try_from(max_segment).unwrap_or(u32::MAX)
}
};
Alice