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: rust-for-linux@vger.kernel.org, andrew@lunn.ch, benno.lossin@proton.me
Subject: Re: [PATCH v3 1/2] rust: net::phy unified read/write API for C22 and C45 registers
Date: Thu, 6 Jun 2024 19:43:55 -0400	[thread overview]
Message-ID: <CALNs47urAkARWg+e4eAqXDpOJFUjoe73WZxYHjL-_DvvzWtOZg@mail.gmail.com> (raw)
In-Reply-To: <20240607.080236.199306626197602234.fujita.tomonori@gmail.com>

On Thu, Jun 6, 2024 at 7:03 PM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> On Thu, 6 Jun 2024 03:41:18 -0500
> Trevor Gross <tmgross@umich.edu> wrote:
>
> > On Sun, Jun 2, 2024 at 6:18 PM FUJITA Tomonori
> > <fujita.tomonori@gmail.com> wrote:
> [..]
> >> +use super::Device;
> >> +use crate::build_assert;
> >> +use crate::error::*;
> >> +use crate::uapi;
> >> +
> >> +/// Accesses PHY registers.
> >> +///
> >> +/// This trait is used to implement the unified interface to access
> >> +/// C22 and C45 PHY registers.
> >> +pub trait Register {
> >
> > Since we don't use this anywhere could it be sealed?
>
> Yeah. Only reg module implements it. How it can be done?
>
> diff --git a/rust/kernel/net/phy/reg.rs b/rust/kernel/net/phy/reg.rs
> index 7628eb09e902..0dfae4677574 100644
> --- a/rust/kernel/net/phy/reg.rs
> +++ b/rust/kernel/net/phy/reg.rs
> @@ -12,11 +12,18 @@
>  use crate::error::*;
>  use crate::uapi;
>
> +mod private {
> +    pub trait Sealed {}
> +
> +    impl Sealed for super::C22 {}
> +    impl Sealed for super::C45 {}
> +}
> +
>  /// Accesses PHY registers.
>  ///
>  /// This trait is used to implement the unified interface to access
>  /// C22 and C45 PHY registers.
> -pub trait Register {
> +pub trait Register: private::Sealed {
>      /// Reads a PHY register.
>      fn read(&self, dev: &mut Device) -> Result<u16>;
>
>
> > This might not even need to be public since it should probably only be
> > used as a method on `Device` (i.e. `my_device.read(C22::BMCR)` rather
> > than importing the trait and using `C22::BCMR::read(my_device)`).
>
> If I drop public, I got the following error:
>
> error[E0603]: trait `Register` is private
>    --> rust/kernel/net/phy.rs:181:25
>     |
> 181 |     pub fn read<R: reg::Register>(&mut self, reg: R) -> Result<u16> {
>     |                         ^^^^^^^^ private trait
>     |
> note: the trait `Register` is defined here
>    --> rust/kernel/net/phy/reg.rs:19:1
>     |
> 19  | trait Register {
>     | ^^^^^^^^^^^^^^:
>
> (snip)

Oh, I did mean `pub(crate)`.

If that doesn't work, usually the sealed pattern looks like this:

    /* lib.rs */
    mod private {
        /// Marker that a trait cannot be implemented outside of this crate
        trait Sealed {}
    }

    /* reg.rs or other modules */
    use crate::private::Sealed;

    // Traits that aren't meant to be implemented outside of the crate
    // get `Sealed` as a bound
    pub trait Register: Sealed { /* ... */ }

    // Anything with a `Register` impl now also needs `Sealed`
    impl Sealed for C22 {}
    impl Register for C22 { /* ... */ }

    // And then nobody from a different crate can implement `Register` on
    // some random type, because they have no way to access/implement
    // private `Sealed`

I also asked about this because I'm a bit surprised it hasn't come up yet [1].

But if `pub(crate)` works then that is easier, of course.

- Trevor

[1]: https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/Sealed.20traits/near/443180412

  reply	other threads:[~2024-06-06 23:44 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-02 23:17 [PATCH v3 0/2] net::phy add unified API for C22 and C45 FUJITA Tomonori
2024-06-02 23:17 ` [PATCH v3 1/2] rust: net::phy unified read/write API for C22 and C45 registers FUJITA Tomonori
2024-06-03 14:02   ` Andrew Lunn
2024-06-03 14:03     ` Miguel Ojeda
2024-06-06  8:41   ` Trevor Gross
2024-06-06 23:02     ` FUJITA Tomonori
2024-06-06 23:43       ` Trevor Gross [this message]
2024-06-06 23:59         ` FUJITA Tomonori
2024-06-02 23:17 ` [PATCH v3 2/2] rust: net::phy unified genphy_read_status function " FUJITA Tomonori
2024-06-03 14:09   ` Andrew Lunn
2024-06-03 14:39     ` FUJITA Tomonori
2024-06-03 14:45       ` Andrew Lunn
2024-06-06  8:52   ` Trevor Gross
2024-06-06 23:07     ` 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=CALNs47urAkARWg+e4eAqXDpOJFUjoe73WZxYHjL-_DvvzWtOZg@mail.gmail.com \
    --to=tmgross@umich.edu \
    --cc=andrew@lunn.ch \
    --cc=benno.lossin@proton.me \
    --cc=fujita.tomonori@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    /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).