rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Trevor Gross <tmgross@umich.edu>
To: Boqun Feng <boqun.feng@gmail.com>
Cc: Andrew Lunn <andrew@lunn.ch>,
	FUJITA Tomonori <fujita.tomonori@gmail.com>,
	 rust-for-linux@vger.kernel.org
Subject: Re: [RFC PATCH v1 1/4] rust: core abstractions for network PHY drivers
Date: Thu, 14 Sep 2023 00:10:40 -0400	[thread overview]
Message-ID: <CALNs47vsMBDKm0Be4VbNo+agrorgmwymqU3Am+TxyTvW34g5Ow@mail.gmail.com> (raw)
In-Reply-To: <ZQIqgEHPQCfVfjDd@boqun-archlinux>

On Wed, Sep 13, 2023 at 11:05:31PM +0200, Andrew Lunn wrote:
> > [...]
> >
> > So by lifetime, it means while it is inside this function?
> >

Boqun covered this example quite well, here are some more general
crash course notes if it helps -

Lifetime is just rust lingo for compiler-tracked metadata on a
pointer, pointer + lifetime = reference (`&`). An example

    struct Foo<'a> {
        bar: &'a u32
    }

Read that as:
- a `Foo` will exist and be valid for some amount of time (of course)
- call the time it exists for `'a` [^1]
- `bar` is a pointer to something
- `bar` has lifetime `'a`. Which means, whatever `bar` points to must
  be valid for at least `'a`, i.e, at least as long as `Foo` is valid

Same rules you have to follow in any language, having these
annotations just tells the compiler about it. Then it keeps track and
makes sure they get followed. Functions give similar information:

    /// Return the first substring of `haystack` that starts with `needle`
    fn strstr<'a, 'b>(haystack: &'a str, needle: &'b str) -> &'a str;

The return value and `haystack` have the same lifetime, so you know
you can't `destroy` haystack as long as the return value is in use.
`needle` has a different lifetime so it's clear that it is unrelated.
In the original function

>> +    /// # Safety
>> +    ///
>> +    /// For the duration of the lifetime 'a, the pointer must be valid for writing and nobody else
>> +    /// may read or write to the `phy_device` object.
>> +    pub unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self

A compiler-tracked lifetime is conjured from nothing, so the caller
gets to decide what that is (usually implicitly but can be specified).
This could be anything, hence "unsafe" and instructions on how to make
it correct. Once created, the compiler will track the above rules.

The other part of the picture is that rust strictly enforces one
`&mut` reference XOR any number `&` (const) references to a piece of
data at a time [^2]. The exception to this is of course things that
safely allow access behind a non-exclusive reference -

> In theory, the core will protect the phy_device structure
> using the phydev->lock mutex, although we might be missing a few [...]

- such as mutexes :). [^3] The original comment could probably add
something like "with the exception of mutex-protected fields" or a
mention about the callbacks.

Bit awkward at first, but it winds up being very nice to kick the
mental pointer-validity-chasing load to the compiler.

[^1]: could be called `'foo` `'buffer` or anything else,
      `'a' `'b` ... are common for simple things
[^2]: at a lower level `&mut` is basically C's `restrict`. Easier
      to use when the compiler checks it.
[^3]: these types use `UnsafeCell` to tell the compiler
      they allow this and not to emit `restrict`. `Opaque`
      in bindings also does this.

  reply	other threads:[~2023-09-14  4:10 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-13 13:36 [RFC PATCH v1 0/4] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-09-13 13:36 ` [RFC PATCH v1 1/4] rust: core " FUJITA Tomonori
2023-09-13 20:11   ` Andrew Lunn
2023-09-13 20:49     ` Boqun Feng
2023-09-13 21:05       ` Andrew Lunn
2023-09-13 21:32         ` Boqun Feng
2023-09-14  4:10           ` Trevor Gross [this message]
2023-09-13 22:14     ` Miguel Ojeda
2023-09-14  0:30       ` Andrew Lunn
2023-09-14 11:03         ` Miguel Ojeda
2023-09-14 12:24           ` Andrew Lunn
2023-09-17  9:44           ` FUJITA Tomonori
2023-09-17 10:17     ` FUJITA Tomonori
2023-09-17 15:06       ` Andrew Lunn
2023-09-17 18:42         ` Trevor Gross
2023-09-17 19:08           ` Andrew Lunn
2023-09-18  0:49             ` Trevor Gross
2023-09-18  1:18               ` Andrew Lunn
2023-09-18  2:22                 ` Trevor Gross
2023-09-18  3:44                   ` FUJITA Tomonori
2023-09-18 13:13                     ` Andrew Lunn
2023-09-18  6:01         ` FUJITA Tomonori
2023-09-14  5:47   ` Trevor Gross
2023-09-14 10:17     ` Jarkko Sakkinen
2023-09-14 19:46       ` Trevor Gross
2023-09-14 12:39     ` Andrew Lunn
2023-09-14 19:42       ` Trevor Gross
2023-09-14 19:53         ` Trevor Gross
2023-09-18  9:56   ` Finn Behrens
2023-09-18 13:22     ` Andrew Lunn
2023-09-18 10:22   ` Benno Lossin
2023-09-18 13:09     ` FUJITA Tomonori
2023-09-18 15:20       ` Benno Lossin
2023-09-19 10:26         ` FUJITA Tomonori
2023-09-20 13:24           ` Benno Lossin
2023-09-13 13:36 ` [RFC PATCH v1 2/4] rust: phy: add module device table support FUJITA Tomonori
2023-09-14  6:26   ` Trevor Gross
2023-09-14  7:23     ` Trevor Gross
2023-09-17  6:30       ` FUJITA Tomonori
2023-09-17 15:13         ` Andrew Lunn
2023-09-13 13:36 ` [RFC PATCH v1 3/4] MAINTAINERS: add Rust PHY abstractions file to the ETHERNET PHY LIBRARY FUJITA Tomonori
2023-09-13 18:57   ` Andrew Lunn
2023-09-17 12:32     ` FUJITA Tomonori
2023-09-19 12:06       ` Miguel Ojeda
2023-09-19 16:33         ` Andrew Lunn
2023-09-22 23:17           ` Trevor Gross
2023-09-23  0:05           ` Miguel Ojeda
2023-09-23  1:36             ` Andrew Lunn
2023-09-23 10:19               ` Miguel Ojeda
2023-09-23 14:42                 ` Andrew Lunn
2023-10-09 12:26                   ` Miguel Ojeda
2023-09-13 13:36 ` [RFC PATCH v1 4/4] sample: rust: add Asix PHY driver FUJITA Tomonori
2023-09-13 14:11   ` Andrew Lunn
2023-09-13 16:53     ` Greg KH
2023-09-13 18:50       ` Andrew Lunn
2023-09-13 18:59         ` Greg KH
2023-09-13 20:20           ` Andrew Lunn
2023-09-13 20:38             ` Greg KH
2023-09-13 16:56   ` Greg KH
2023-09-13 18:43     ` Andrew Lunn
2023-09-13 19:15   ` Andrew Lunn
2023-09-17 11:28     ` FUJITA Tomonori
2023-09-17 15:46       ` Andrew Lunn
2023-09-18  8:35         ` FUJITA Tomonori
2023-09-18 13:37           ` Andrew Lunn
2023-09-24  9:12             ` FUJITA Tomonori
2023-09-24  9:59               ` Miguel Ojeda
2023-09-24 15:31               ` Andrew Lunn
2023-09-24 17:31                 ` Miguel Ojeda
2023-09-24 17:44                   ` Andrew Lunn
2023-09-24 18:44                     ` Miguel Ojeda
2023-09-18 22:23 ` [RFC PATCH v1 0/4] Rust abstractions for network PHY drivers Trevor Gross
2023-09-18 22:48   ` Andrew Lunn
2023-09-18 23:46     ` Trevor Gross
2023-09-19  6:24       ` FUJITA Tomonori
2023-09-19  7:41         ` Trevor Gross
2023-09-19 16:12         ` Andrew Lunn
2023-09-19  6:16   ` FUJITA Tomonori
2023-09-19  8:05     ` Trevor Gross

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=CALNs47vsMBDKm0Be4VbNo+agrorgmwymqU3Am+TxyTvW34g5Ow@mail.gmail.com \
    --to=tmgross@umich.edu \
    --cc=andrew@lunn.ch \
    --cc=boqun.feng@gmail.com \
    --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).