Re: [PATCH v10 01/13] rust: hrtimer: introduce hrtimer support
From: Andreas Hindborg
Date: Fri Mar 07 2025 - 09:18:55 EST
"Benno Lossin" <benno.lossin@xxxxxxxxx> writes:
> On Fri Mar 7, 2025 at 2:10 PM CET, Andreas Hindborg wrote:
>> "Benno Lossin" <benno.lossin@xxxxxxxxx> writes:
>>> On Fri Mar 7, 2025 at 11:11 AM CET, Andreas Hindborg wrote:
>>>> +/// Use to implement the [`HasHrTimer<T>`] trait.
>>>> +///
>>>> +/// See [`module`] documentation for an example.
>>>> +///
>>>> +/// [`module`]: crate::time::hrtimer
>>>> +#[macro_export]
>>>> +macro_rules! impl_has_hr_timer {
>>>> + (
>>>> + impl$({$($generics:tt)*})?
>>>> + HasHrTimer<$timer_type:ty>
>>>> + for $self:ty
>>>> + { self.$field:ident }
>>>> + $($rest:tt)*
>>>> + ) => {
>>>> + // SAFETY: This implementation of `raw_get_timer` only compiles if the
>>>> + // field has the right type.
>>>> + unsafe impl$(<$($generics)*>)? $crate::time::hrtimer::HasHrTimer<$timer_type> for $self {
>>>> +
>>>> + #[inline]
>>>> + unsafe fn raw_get_timer(this: *const Self) ->
>>>> + *const $crate::time::hrtimer::HrTimer<$timer_type>
>>>> + {
>>>> + // SAFETY: The caller promises that the pointer is not dangling.
>>>> + unsafe {
>>>> + ::core::ptr::addr_of!((*this).$field)
>>>> + }
>>>> + }
>>>> +
>>>> + #[inline]
>>>> + unsafe fn timer_container_of(ptr: *mut $crate::time::hrtimer::HrTimer<$timer_type>) ->
>>>> + *mut Self
>>>
>>> This formatting looks a bit weird, (macro formatting is annoying, I
>>> know).
>>
>> How would you change it?
>
> Just use `rustfmt` (copy the macro arm, replace all `$` with `_` in
> names and remove repetitions, `rustfmt` and then revert. You're lucky,
> since you only have one repetition that doesn't cause the line to go
> over the 100 column threshold):
>
> // SAFETY: This implementation of `raw_get_timer` only compiles if the
> // field has the right type.
> unsafe impl$(<$($generics)*>)? $crate::time::hrtimer::HasHrTimer<$timer_type> for $self {
>
> #[inline]
> unsafe fn raw_get_timer(
> this: *const Self,
> ) -> *const $crate::time::hrtimer::HrTimer<$timer_type> {
> // SAFETY: The caller promises that the pointer is not dangling.
> unsafe { ::core::ptr::addr_of!((*this).$field) }
> }
>
> #[inline]
> unsafe fn timer_container_of(
> ptr: *mut $crate::time::hrtimer::HrTimer<$timer_type>,
> ) -> *mut Self {
> // SAFETY: As per the safety requirement of this function, `ptr`
> // is pointing inside a `$timer_type`.
> unsafe { ::kernel::container_of!(ptr, $timer_type, $field).cast_mut() }
> }
> }
That is better.
Best regards,
Andreas Hindborg