rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Trevor Gross <tmgross@umich.edu>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: gregkh@linuxfoundation.org, rust-for-linux@vger.kernel.org,
	andrew@lunn.ch,  miguel.ojeda.sandonis@gmail.com,
	benno.lossin@proton.me, wedsonaf@gmail.com
Subject: Re: [RFC PATCH v3 1/3] rust: core abstractions for network PHY drivers
Date: Fri, 29 Sep 2023 05:11:21 -0400	[thread overview]
Message-ID: <CALNs47td-3UM-E59R35GAjaHMPT8c-H7Qy=PZ+-wgsXpw5EJAA@mail.gmail.com> (raw)
In-Reply-To: <20230929.173856.1751823335892887678.fujita.tomonori@gmail.com>

On Fri, Sep 29, 2023 at 4:39 AM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
> [...]
> >> +    /// Gets the state of the PHY.
> >> +    pub fn state(&mut self) -> DeviceState {
> >> +        let phydev = self.0.get();
> >> +        // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
> >> +        let state = unsafe { (*phydev).state };
> >> +        match state {
> >> +            bindings::phy_state::PHY_DOWN => DeviceState::Down,
> >> +            bindings::phy_state::PHY_READY => DeviceState::Ready,
> >> +            bindings::phy_state::PHY_HALTED => DeviceState::Halted,
> >> +            bindings::phy_state::PHY_ERROR => DeviceState::Error,
> >> +            bindings::phy_state::PHY_UP => DeviceState::Up,
> >> +            bindings::phy_state::PHY_RUNNING => DeviceState::Running,
> >> +            bindings::phy_state::PHY_NOLINK => DeviceState::NoLink,
> >> +            bindings::phy_state::PHY_CABLETEST => DeviceState::CableTest,
> >
> > Again, this feels like a maintance nightmare.
>
> As I wrote above, it's guaranteed that C and Rust code never
> diverge. However, as you said, suley it's troublesome to maintain this.
>
> We could directly use enum generated from C's enum at compile
> time. Then we can remove this conversion. The names are not pretty
> from Rust's view though.
>
> Any concerns from Rust side?
> [...]
>
>  /// Corresponds to the kernel's `enum phy_state`.
> -#[derive(PartialEq)]
> -pub enum DeviceState {
> -    /// PHY device and driver are not ready for anything.
> -    Down,
> -    /// PHY is ready to send and receive packets.
> -    Ready,
> -    /// PHY is up, but no polling or interrupts are done.
> -    Halted,
> -    /// PHY is up, but is in an error state.
> -    Error,
> -    /// PHY and attached device are ready to do work.
> -    Up,
> -    /// PHY is currently running.
> -    Running,
> -    /// PHY is up, but not currently plugged in.
> -    NoLink,
> -    /// PHY is performing a cable test.
> -    CableTest,
> -}
> +///
> +///  PHY device and driver are not ready for anything.
> +///  PHY_DOWN
> +///  PHY is ready to send and receive packets.
> +///  PHY_READY
> +///  PHY is up, but no polling or interrupts are done.
> +///  PHY_HALTED
> +///  PHY is up, but is in an error state.
> +///  PHY_ERROR
> +///  PHY and attached device are ready to do work.
> +///  PHY_UP
> +///  PHY is currently running.
> +///  PHY_RUNNING
> +///  PHY is up, but not currently plugged in.
> +///  PHY_NOLINK
> +///  PHY is performing a cable test.
> +///  PHY_CABLETEST
> +pub use bindings::phy_state as DeviceState;

I think there are three reasons we may prefer to recreate an enum from
C in Rust:

1. Size, C enums are typically an int but rust enums are u8 until they
   need to be bigger
2. Name consistency, rust is almost always CamelCase for enum variant
   names but the ones from bindings don't
3. Docs, if the enum is user-facing then there are benefits to documenting
   the variants rather than the entire enum (cleaner doc output, ide hints)

None of those are super strong on their own but together seems like
good enough reason to leave it as you have it. I think the most
important thing is making sure it is maintainable, incompatible
changes on the C side should fail rust compilation - which is the goal
of this conversation.

  reply	other threads:[~2023-09-29  9:11 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-28 22:55 [RFC PATCH v3 0/3] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-09-28 22:55 ` [RFC PATCH v3 1/3] rust: core " FUJITA Tomonori
2023-09-29  6:03   ` Greg KH
2023-09-29  8:38     ` FUJITA Tomonori
2023-09-29  9:11       ` Trevor Gross [this message]
2023-10-02 14:08         ` Andrew Lunn
2023-10-02 16:24           ` Miguel Ojeda
2023-10-02 19:08             ` Andrew Lunn
2023-10-09 12:25               ` Miguel Ojeda
2023-10-09 15:50                 ` Andrew Lunn
2023-10-09 16:16                   ` Miguel Ojeda
2023-10-09 16:29                     ` Andrew Lunn
2023-10-10 17:31                       ` Miguel Ojeda
2023-10-02 16:37           ` Trevor Gross
2023-09-29 11:39       ` Miguel Ojeda
2023-09-29 12:23       ` Andrew Lunn
2023-10-01 13:08       ` FUJITA Tomonori
2023-09-29  8:50     ` Trevor Gross
2023-09-29 18:42   ` Miguel Ojeda
2023-10-10 19:19   ` Wedson Almeida Filho
2023-10-10 20:28     ` Andrew Lunn
2023-10-10 21:00       ` Wedson Almeida Filho
2023-10-10 23:34         ` Andrew Lunn
2023-10-11  1:56           ` Wedson Almeida Filho
2023-10-11  5:17             ` FUJITA Tomonori
2023-10-10 22:50     ` Trevor Gross
2023-10-10 22:53       ` Miguel Ojeda
2023-10-10 23:06         ` FUJITA Tomonori
2023-10-10 23:12         ` Trevor Gross
2023-10-11 23:57           ` FUJITA Tomonori
2023-10-12  3:09             ` Trevor Gross
2023-10-12  3:16               ` FUJITA Tomonori
2023-10-12  4:20                 ` Trevor Gross
2023-10-12 15:05                   ` Andrew Lunn
2023-10-11  6:56     ` FUJITA Tomonori
2023-10-11 14:28       ` Wedson Almeida Filho
2023-10-11 14:59         ` FUJITA Tomonori
2023-10-11 23:28         ` FUJITA Tomonori
2023-10-11 17:35       ` Trevor Gross
2023-09-28 22:55 ` [RFC PATCH v3 2/3] MAINTAINERS: add Rust PHY abstractions to the ETHERNET PHY LIBRARY FUJITA Tomonori
2023-09-28 22:55 ` [RFC PATCH v3 3/3] net: phy: add Rust Asix PHY driver FUJITA Tomonori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALNs47td-3UM-E59R35GAjaHMPT8c-H7Qy=PZ+-wgsXpw5EJAA@mail.gmail.com' \
    --to=tmgross@umich.edu \
    --cc=andrew@lunn.ch \
    --cc=benno.lossin@proton.me \
    --cc=fujita.tomonori@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).