rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: FUJITA Tomonori <fujita.tomonori@gmail.com>,
	a.hindborg@kernel.org, alex.gaynor@gmail.com,  ojeda@kernel.org,
	anna-maria@linutronix.de, bjorn3_gh@protonmail.com,
	 boqun.feng@gmail.com, dakr@kernel.org, frederic@kernel.org,
	gary@garyguo.net,  jstultz@google.com,
	linux-kernel@vger.kernel.org, lossin@kernel.org,
	 lyude@redhat.com, rust-for-linux@vger.kernel.org,
	sboyd@kernel.org,  tglx@linutronix.de, tmgross@umich.edu
Subject: Re: [PATCH v1 1/2] rust: time: Rename Delta's methods as_micros_ceil and as_millis
Date: Wed, 18 Jun 2025 09:52:30 +0000	[thread overview]
Message-ID: <aFKMXqak-mHraxU_@google.com> (raw)
In-Reply-To: <CANiq72nP+dKugMv3KXdJJsFE0oD01+zYgWbjz3e04kmhj_5MbQ@mail.gmail.com>

On Wed, Jun 18, 2025 at 11:29:26AM +0200, Miguel Ojeda wrote:
> On Wed, Jun 18, 2025 at 10:05 AM Alice Ryhl <aliceryhl@google.com> wrote:
> >
> > Why are we renaming them? The stdlib always uses as_* or to_* for copy
> > types. In my mind, into_* means that you want to emphasize that you are
> > performing a transformation that consumes self and transfers ownership
> > of some resource in the process.
> >
> > See the api guidelines:
> > https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv
> 
> We may be going in circles here... I think the confusion is all on
> what to do for "owned -> owned" `Copy` non-expensive conversions.
> 
> I think Tomo sent a patch to change the `as_` and `is_` methods to
> take `&self` to be consistent with the guidelines, since they say
> there is no "owned -> owned" case for `as_`. Then you mentioned that
> `self` is OK and Andreas agreed, and I guess Tomo ended up with
> `into_` since `to_` is only for the expensive case, even though it is
> not meant for `Copy` types.
> 
> In other words, either we say in the kernel we are OK with `as_` for
> "owned -> owned" too, or we take `&self`.
> 
> Did I get that right, everyone?

Yeah I think using as_* naming for cases other than borrowed->borrowed
is relatively common for Copy types. Looking at the stdlib, the rules
I'm seeing are more or less these:

First, if the method is expensive the naming is to_* or a verb without a
prefix. We only use into_* for expensive operations if the call also
transfers ownership of some resource. Example: CStr::to_bytes()

Otherwise, if the method is something that looks inside the type (i.e.
it peels away a layer of abstraction), then we using as_* naming. Or we
might use a noun with no prefix if we want it to feel like a field
access. Example: Duration::as_millis()

On the other hand, if the method transforms the value while staying at
the same layer of abstraction, then we may call the method to_* (or just
use a verb without a prefix). Example: f64::to_radians()

Alice

  reply	other threads:[~2025-06-18  9:52 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-17 14:41 [PATCH v1 0/2] rust: time: Add fsleep() FUJITA Tomonori
2025-06-17 14:41 ` [PATCH v1 1/2] rust: time: Rename Delta's methods as_micros_ceil and as_millis FUJITA Tomonori
2025-06-18  8:05   ` Alice Ryhl
2025-06-18  9:29     ` Miguel Ojeda
2025-06-18  9:52       ` Alice Ryhl [this message]
2025-06-18 11:03         ` Miguel Ojeda
2025-06-18 13:17           ` Alice Ryhl
2025-06-18 15:47             ` Miguel Ojeda
2025-06-19  7:08               ` FUJITA Tomonori
2025-06-19  7:23                 ` Miguel Ojeda
2025-06-19  9:28                   ` Andreas Hindborg
2025-06-19 11:44                     ` Miguel Ojeda
2025-06-19 12:51                       ` Andreas Hindborg
2025-06-19 19:03                         ` Miguel Ojeda
2025-06-24 12:15                 ` Alice Ryhl
2025-06-24 13:49                   ` FUJITA Tomonori
2025-06-24 13:54                     ` Alice Ryhl
2025-06-24 14:14                       ` FUJITA Tomonori
2025-06-24 14:45                         ` Alice Ryhl
2025-06-24 16:39                           ` FUJITA Tomonori
2025-06-19  9:12   ` Andreas Hindborg
2025-06-19 11:25     ` FUJITA Tomonori
2025-06-17 14:41 ` [PATCH v1 2/2] rust: time: Add wrapper for fsleep() function FUJITA Tomonori
2025-06-30 12:07   ` Andreas Hindborg

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=aFKMXqak-mHraxU_@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=anna-maria@linutronix.de \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=frederic@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    --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).