From: Wedson Almeida Filho <wedsonaf@gmail.com>
To: Benno Lossin <benno.lossin@proton.me>
Cc: FUJITA Tomonori <fujita.tomonori@gmail.com>,
rust-for-linux@vger.kernel.org, andrew@lunn.ch,
tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com
Subject: Re: [RFC PATCH v2 1/3] rust: core abstractions for network PHY drivers
Date: Sun, 24 Sep 2023 10:51:34 -0300 [thread overview]
Message-ID: <CANeycqq+az08SR3ucSyyT_82Bcxy8SZTcP+6OEzSY2+GHMjokA@mail.gmail.com> (raw)
In-Reply-To: <652520d9-c409-6bb4-821b-5bb8bf99bc0d@proton.me>
On Sun, 24 Sept 2023 at 10:40, Benno Lossin <benno.lossin@proton.me> wrote:
>
> On 24.09.23 14:56, Wedson Almeida Filho wrote:
> > On Sun, 24 Sept 2023 at 03:49, FUJITA Tomonori
> >> + let phydev = Opaque::get(&self.0);
> >> + // SAFETY: This object is initialized by the `from_raw` constructor, so it's valid.
> >> + unsafe {
> >> + (*phydev).speed = speed;
> >> + }
> >> + }
> >> +
> >> + /// Sets duplex mode.
> >> + pub fn set_duplex(&mut self, mode: DuplexMode) {
> >> + let phydev = Opaque::get(&self.0);
> >> + // SAFETY: This object is initialized by the `from_raw` constructor, so it's valid.
> >> + unsafe {
> >> + match mode {
> >> + DuplexMode::Full => (*phydev).duplex = bindings::DUPLEX_FULL as i32,
> >> + DuplexMode::Half => (*phydev).duplex = bindings::DUPLEX_HALF as i32,
> >> + DuplexMode::Unknown => (*phydev).duplex = bindings::DUPLEX_UNKNOWN as i32,
> >> + }
> >> + }
> >
> > To avoid repeating `(*phydev).duplex =` and to reduce the amount of
> > code inside the unsafe block, I suggest you do something like this
> > here:
> >
> > let v = match mode {
> > DuplexMode::Full => bindings::DUPLEX_FULL,
> > DuplexMode::Half => bindings::DUPLEX_HALF,
> > DuplexMode::Unknown => bindings::DUPLEX_UNKNOWN,
> > };
> >
> > // SAFETY: This object is initialized by the `from_raw` constructor,
> > so it's valid.
> > unsafe { (*phydev).duplex = v};
> >
> >
> > In fact, for this enum, you wan to use #[repr(i32)] and assign values
> > from `bindings` so that you can avoid the match as convert using just
> > `mode as i32`. See for example:
> >
> > https://github.com/wedsonaf/linux/blob/rtarfs/rust/kernel/fs.rs#L71
> >
>
> I do not like `as`-style casts, they are error prone and no warning/error
> is generated if they are used incorrectly. They also make review harder,
> because I now have to look if the enum indeed has that correct repr.
> I think we should talk about this in one of our meetings.
Yes, let's discuss this with the rest of the team in a meeting.
I think the main advantage is that we avoid the big match statement
and additional code that it entails. Perhaps we could have a `TryFrom`
implementation that uses `as` and that's what callers use. Then we
only have to review/verify the `as` usage once when the enum is
defined.
Another option is to keep the big match statement if we can show that
the compiler can optimise it out.
>
> >> + phy_id: <T>::PHY_ID,
> >> + phy_id_mask: <T>::PHY_ID_MASK,
> >> + soft_reset: if <T>::HAS_SOFT_RESET {
> >> + Some(Self::soft_reset_callback)
> >> + } else {
> >> + None
> >> + },
> >> + get_features: if <T>::HAS_GET_FEATURES {
> >> + Some(Self::get_features_callback)
> >> + } else {
> >> + None
> >> + },
> >> + match_phy_device: if <T>::HAS_MATCH_PHY_DEVICE {
> >> + Some(Self::match_phy_device_callback)
> >> + } else {
> >> + None
> >> + },
> >> + suspend: if <T>::HAS_SUSPEND {
> >> + Some(Self::suspend_callback)
> >> + } else {
> >> + None
> >> + },
> >> + resume: if <T>::HAS_RESUME {
> >> + Some(Self::resume_callback)
> >> + } else {
> >> + None
> >> + },
> >> + config_aneg: if <T>::HAS_CONFIG_ANEG {
> >> + Some(Self::config_aneg_callback)
> >> + } else {
> >> + None
> >> + },
> >> + read_status: if <T>::HAS_READ_STATUS {
> >> + Some(Self::read_status_callback)
> >> + } else {
> >> + None
> >> + },
> >> + read_mmd: if <T>::HAS_READ_MMD {
> >> + Some(Self::read_mmd_callback)
> >> + } else {
> >> + None
> >> + },
> >> + write_mmd: if <T>::HAS_WRITE_MMD {
> >> + Some(Self::write_mmd_callback)
> >> + } else {
> >> + None
> >> + },
> >> + link_change_notify: if <T>::HAS_LINK_CHANGE_NOTIFY {
> >> + Some(Self::link_change_notify_callback)
> >> + } else {
> >> + None
> >> + },
> >> + // SAFETY: The rest is zeroed out to initialize `struct phy_driver`,
> >> + // sets `Option<&F>` to be `None`.
> >> + ..unsafe { core::mem::MaybeUninit::<bindings::phy_driver>::zeroed().assume_init() }
> >
> > I think you could just use bindings::phy_driver::default() here.
>
> That is not possible, since it is not a const function.
Ah, yeah, this is in const context. Nevermind.
next prev parent reply other threads:[~2023-09-24 13:51 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-24 6:48 [RFC PATCH v2 0/3] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-09-24 6:49 ` [RFC PATCH v2 1/3] rust: core " FUJITA Tomonori
2023-09-24 12:56 ` Wedson Almeida Filho
2023-09-24 13:39 ` Benno Lossin
2023-09-24 13:51 ` Wedson Almeida Filho [this message]
2023-09-25 11:30 ` FUJITA Tomonori
2023-09-25 13:14 ` Andrew Lunn
2023-09-24 15:42 ` Andrew Lunn
2023-09-25 1:13 ` FUJITA Tomonori
2023-09-25 13:25 ` Andrew Lunn
2023-09-25 6:47 ` Alice Ryhl
2023-09-26 1:19 ` FUJITA Tomonori
2023-09-26 2:50 ` Andrew Lunn
2023-09-24 13:19 ` Benno Lossin
2023-09-25 10:24 ` FUJITA Tomonori
2023-09-25 15:41 ` Miguel Ojeda
2023-09-26 13:46 ` FUJITA Tomonori
2023-09-27 10:49 ` Miguel Ojeda
2023-09-27 11:19 ` FUJITA Tomonori
2023-10-09 12:28 ` Miguel Ojeda
2023-09-24 17:00 ` Andrew Lunn
2023-09-24 18:03 ` Miguel Ojeda
2023-09-25 13:28 ` Andrew Lunn
2023-09-25 13:43 ` Andrew Lunn
2023-09-25 15:42 ` Miguel Ojeda
2023-09-25 16:53 ` Andrew Lunn
2023-09-25 17:26 ` Miguel Ojeda
2023-09-25 18:32 ` Andrew Lunn
2023-09-25 19:15 ` Miguel Ojeda
2023-09-26 6:05 ` Trevor Gross
2023-09-26 12:11 ` Andrew Lunn
2023-09-27 3:26 ` FUJITA Tomonori
2023-09-26 6:54 ` Trevor Gross
2023-09-27 3:39 ` FUJITA Tomonori
2023-09-27 12:21 ` Andrew Lunn
2023-09-24 6:49 ` [RFC PATCH v2 2/3] MAINTAINERS: add Rust PHY abstractions file to the ETHERNET PHY LIBRARY FUJITA Tomonori
2023-09-24 6:49 ` [RFC PATCH v2 3/3] net: phy: add Rust Asix PHY driver FUJITA Tomonori
2023-09-24 8:05 ` Miguel Ojeda
2023-09-24 9:38 ` FUJITA Tomonori
2023-09-24 10:10 ` Miguel Ojeda
2023-09-24 11:00 ` FUJITA Tomonori
2023-09-24 13:33 ` Benno Lossin
2023-09-25 2:31 ` FUJITA Tomonori
2023-09-26 6:20 ` Trevor Gross
2023-09-26 7:07 ` FUJITA Tomonori
[not found] ` <CALNs47uYnQC+AXbJuk8d5506D25SDhZ-ZKuhimFkZnYOhhdfCg@mail.gmail.com>
2023-09-26 12:36 ` Andrew Lunn
2023-09-27 1:18 ` 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=CANeycqq+az08SR3ucSyyT_82Bcxy8SZTcP+6OEzSY2+GHMjokA@mail.gmail.com \
--to=wedsonaf@gmail.com \
--cc=andrew@lunn.ch \
--cc=benno.lossin@proton.me \
--cc=fujita.tomonori@gmail.com \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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).