3 ms·
Oh, I thought that the comment about not looking as idiomatic code was referring to the kernel::new_device_... sample in a parent/parent HN comment. I was more
by ggrrhh_ta 5y ago
Oh, I thought that the comment about not looking as idiomatic code was referring to the kernel::new_device_... sample in a parent/parent HN comment. I was more interested in knowing what was so unidiomatic about that.
- estebank 5y agoThe posted code isn't terrible, but it is a bit denser and harder to read than I'd like. Some small changes (like importing the macros to avoid a full path) makes a bit easier to read // SAFETY: This device doesn't support DMA. let base = unsafe { IoMem::try_new(res)? }; let mut data = new_device_data!( RegistrationWithIrqChip::new(), PL061Resources { base, parent_irq: irq }, // SAFETY: We call `spinlock_init` below. unsafe { SpinLock::new(PL061Data::default()) }, "PL061::Registrations" )?; // SAFETY: General part of the data is pinned when `data` is. let gen = unsafe { data.as_mut().map_unchecked_mut(|d| &mut **d) }; spinlock_init!(gen, "PL061::General"); let data: Ref<DeviceData> = data.into(); I imagine code like this would be the body of a small function that does all this unsafe preparations and gives back a Result<Ref<DeviceData>, Error> once it's done, which would change the end to be Ok(data.into())
- ggrrhh_ta 5y agoThanks, it actually looks better and more understandable.