Re: [net-next PATCH v10 7/7] rust: net::phy sync with match_phy_device C changes
From: FUJITA Tomonori
Date: Mon May 19 2025 - 08:02:10 EST
On Sat, 17 May 2025 21:02:51 +0200
"Benno Lossin" <lossin@xxxxxxxxxx> wrote:
>>> I think that's wrong, nothing stops me from implementing `Driver` for an
>>> empty enum and that can't be instantiated. The reason that one wants to
>>> have this in C is because the same `match` function is used for
>>> different drivers (or maybe devices? I'm not too familiar with the
>>> terminology). In Rust, you must implement the match function for a
>>> single PHY_DEVICE_ID only, so maybe we don't need to change the
>>> signature at all?
>>
>> I'm not sure I understand the last sentence. The Rust PHY abstraction
>> allows one module to support multiple drivers. So we can could the
>> similar trick that the second patch in this patchset does.
>>
>> fn match_device_id(dev: &mut phy::Device, drv: &phy::DriverVTable) -> bool {
>> // do comparison workking for three drivers
>> }
>
> I wouldn't do it like this in Rust, instead this would be a "rustier"
> function signature:
>
> fn match_device_id<T: Driver>(dev: &mut phy::Device) -> bool {
> // do the comparison with T::PHY_DEVICE_ID
> dev.id() == T::PHY_DEVICE_ID
> }
>
> And then in the impls for Phy{A,B,C,D} do this:
>
> impl Driver for PhyA {
> fn match_phy_device(dev: &mut phy::Device) -> bool {
> match_device_id::<Self>(dev)
> }
> }
Ah, yes, this works well.
>> The other use case, as mentioned above, is when using the generic helper
>> function inside match_phy_device() callback. For example, the 4th
>> patch in this patchset adds genphy_match_phy_device():
>>
>> int genphy_match_phy_device(struct phy_device *phydev,
>> const struct phy_driver *phydrv)
>>
>> We could add a wrapper for this function as phy::Device's method like
>>
>> impl Device {
>> ...
>> pub fn genphy_match_phy_device(&self, drv: &phy::DriverVTable) -> i32
>
> Not sure why this returns an `i32`, but we probably could have such a
Maybe a bool would be more appropriate here because the C's comment
says:
Return: 1 if the PHY device matches the driver, 0 otherwise.
> function as well (though I wouldn't use the vtable for that).
What would you use instead?