rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: FUJITA Tomonori <tomo@exabit.dev>
Cc: aliceryhl@google.com, fujita.tomonori@gmail.com,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH 3/5] rust: add support for get_stats64 in struct net_device_ops
Date: Mon, 5 Jun 2023 07:26:59 -0700	[thread overview]
Message-ID: <ZH3wsx9KBFFfL9yT@Boquns-Mac-mini.local> (raw)
In-Reply-To: <010101888bd95727-14bd2f14-91f2-4bc2-bca9-e0912f256d21-000000@us-west-2.amazonses.com>

On Mon, Jun 05, 2023 at 01:57:36PM +0000, FUJITA Tomonori wrote:
> Hi,
> 
> On Sun,  4 Jun 2023 12:48:29 +0000
> Alice Ryhl <aliceryhl@google.com> wrote:
> 
> > FUJITA Tomonori writes:
> >> +/// Corresponds to the kernel's `struct rtnl_link_stats64`.
> >> +pub struct RtnlLinkStats64(*mut bindings::rtnl_link_stats64);
> >> +
> >> +impl RtnlLinkStats64 {
> >> +    /// Updates TX stats.
> >> +    pub fn set_tx_stats(&mut self, packets: u64, bytes: u64, errors: u64, dropped: u64) {
> >> +        // SAFETY: The safety requirement ensures that the pointer is valid.
> >> +        unsafe {
> >> +            (*self.0).tx_packets = packets;
> >> +            (*self.0).tx_bytes = bytes;
> >> +            (*self.0).tx_errors = errors;
> >> +            (*self.0).tx_dropped = dropped;
> >> +        }
> >> +    }
> >> +}
> > 
> > It seems like this type is always used behind a reference. Your current
> > approach has the disadvantage that you're introducing an extra layer of
> > indirection. You can avoid that like this:
> > 
> > ```
> > /// Corresponds to the kernel's `struct rtnl_link_stats64`.
> > #[repr(transparent)]
> > pub struct RtnlLinkStats64(Opaque<bindings::rtnl_link_stats64>);
> > 
> > impl RtnlLinkStats64 {
> >     /// # Safety
> >     ///
> >     /// For the duration of the lifetime 'a, the pointer must be valid for writing and nobody else
> >     /// may read or write to the `rtnl_link_stats64` object.
> >     pub unsafe fn from_raw<'a>(ptr: *mut bindings::rtnl_link_stats64) -> &'a mut Self {
> >     	unsafe { &mut *(ptr as *mut Self) }
> >     }
> 
> I was wondering which I should use here, a pointer or Opaque. Can you
> give guidelines on which to use?
> 

Always avoid raw pointers in data structures, unless there is no
dereference on the pointers (i.e. use the pointer types as plain data).

The reason is as Alice mentioned above.

Regards,
Boqun

> The condition in the above commment is met, Opaque should be used?

  reply	other threads:[~2023-06-05 14:27 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20230604021736.3392963-1-tomo@exabit.dev>
2023-06-04  2:17 ` [PATCH 1/5] rust: core abstractions for network device drivers FUJITA Tomonori
2023-06-04 12:47   ` Alice Ryhl
2023-06-05 13:36     ` FUJITA Tomonori
2023-06-08  7:38       ` Alice Ryhl
2023-06-09 12:39         ` FUJITA Tomonori
2023-06-09 13:15     ` Alice Ryhl
2023-06-09 13:23       ` FUJITA Tomonori
2023-06-06 16:20   ` Andrew Lunn
2023-06-07  5:14     ` FUJITA Tomonori
2023-06-08  8:10     ` Alice Ryhl
2023-06-09 12:52       ` FUJITA Tomonori
2023-06-04  2:17 ` [PATCH 2/5] rust: add support for ethernet operations FUJITA Tomonori
2023-06-04 12:48   ` Alice Ryhl
2023-06-05 14:02     ` FUJITA Tomonori
2023-06-05 23:18       ` FUJITA Tomonori
2023-06-04  2:17 ` [PATCH 3/5] rust: add support for get_stats64 in struct net_device_ops FUJITA Tomonori
2023-06-04 12:48   ` Alice Ryhl
2023-06-05 13:57     ` FUJITA Tomonori
2023-06-05 14:26       ` Boqun Feng [this message]
2023-06-08  7:56       ` Alice Ryhl
2023-06-04  2:17 ` [PATCH 4/5] rust: add methods for configure net_device FUJITA Tomonori
2023-06-04 12:48   ` Alice Ryhl
2023-06-05 13:39     ` FUJITA Tomonori
2023-06-04  2:17 ` [PATCH 5/5] samples: rust: add dummy network driver sample FUJITA Tomonori
2023-06-04 12:48   ` Alice Ryhl
2023-06-05 14:35     ` FUJITA Tomonori
2023-06-06 16:34   ` Andrew Lunn
2023-06-07  5:20     ` FUJITA Tomonori
2023-06-08  8:22     ` Alice Ryhl
2023-06-09 12:33       ` FUJITA Tomonori
2023-06-09 13:11         ` Alice Ryhl
2023-06-09 13:54           ` FUJITA Tomonori
2023-06-09 13:53         ` Andrew Lunn
2023-06-09 14:41           ` FUJITA Tomonori
2023-06-13  4:53 [PATCH 0/5] Rust abstractions for network device drivers FUJITA Tomonori
2023-06-13  4:53 ` [PATCH 3/5] rust: add support for get_stats64 in struct net_device_ops 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=ZH3wsx9KBFFfL9yT@Boquns-Mac-mini.local \
    --to=boqun.feng@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=fujita.tomonori@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tomo@exabit.dev \
    /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).