From: Boqun Feng <boqun.feng@gmail.com>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: "Gary Guo" <gary@garyguo.net>, "Asahi Lina" <lina@asahilina.net>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Sven Van Asbroeck" <thesven73@gmail.com>,
"Fox Chen" <foxhlchen@gmail.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
asahi@lists.linux.dev
Subject: Re: [PATCH 5/5] rust: error: Add from_kernel_result!() macro
Date: Sun, 26 Feb 2023 14:13:10 -0800 [thread overview]
Message-ID: <Y/vZdv6IVmrcP7wR@boqun-archlinux> (raw)
In-Reply-To: <CANiq72kTiHF76T0AycM43qj4rUgQpHzBqXujdvk+H2qoDz22AQ@mail.gmail.com>
On Sun, Feb 26, 2023 at 09:59:25PM +0100, Miguel Ojeda wrote:
> On Sun, Feb 26, 2023 at 7:17 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> >
> > My preference to function instead of macro here is because I want to
> > avoid the extra level of abstraction and make things explict, so that
> > users and reviewers can understand the API behavior solely based on
> > Rust's types, functions and closures: they are simpler than macros, at
> > least to me ;-)
>
> There is one extra problem with the macro: `rustfmt` does not format
> the contents if called with braces (as we currently do).
Interesting, sounds like a missing feature in `rustfmt` or maybe we
don't use the correct config ;-)
>
> So when I was cleaning some things up for v8, one of the things I did
> was run manually `rustfmt` on the blocks by removing the macro
> invocation, in commit 77a1a8c952e1 ("rust: kernel: apply `rustfmt` to
> `from_kernel_result!` blocks").
>
> Having said that, it does format it when called with parenthesis
> wrapping the block, so we could do that if we end up with the macro.
>
> > First, I think the macro version here is just a poor-man's try block, in
> > other words, I'd expect explicit use of try blocks intead of
> > `from_kernel_result` when the feature is ready. If that's the case, we
> > need to change the use sites anyway.
>
> Yeah, if we eventually get a better language feature that fits well,
> then we should use it.
>
> > Do both implementation share the same behavior?
>
> Yeah, a `return` will return to the outer caller in the case of a
> `try` block, while it returns to the closure (macro) in the other
> case. Or do you mean something else?
>
"Yeah" means they have different behaviors, right? ;-)
Thanks for confirming and I think you get it, but just in case for
others reading this: if we use the macro way to implement
`from_kernel_result` as in this patch:
macro_rules! from_kernel_result {
($($tt:tt)*) => {{
$crate::error::from_kernel_result_helper((|| {
$($tt)*
})())
}};
}
and later we re-implement with try blocks:
macro_rules! from_kernel_result {
($($tt:tt)*) => {{
$crate::error::from_kernel_result_helper(try {
$($tt)*
})
}};
}
the `from_kernel_result` semantics will get changed on the `return`
statement inside the macro blocks.
And this is another reason why we want to avoid use macros here. Code
example as below:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=58ea8b95cdfd6b053561052853b0ac00
`foo_v1` and `foo_v3` has the exact same function body, but behave
differently.
> In that case, I think one could use use a labeled block to `break`
> out, not sure if `try` blocks will allow an easier way.
>
> We have a case of such a `return` within the closure at `rust/rust` in
> `file.rs`:
Thanks for finding an example! Means we did use return.
For this particular API, I'd say function right now, `try` blocks if
avaiable.
Regards,
Boqun
>
> from_kernel_result! {
> let off = match whence as u32 {
> bindings::SEEK_SET => SeekFrom::Start(offset.try_into()?),
> bindings::SEEK_CUR => SeekFrom::Current(offset),
> bindings::SEEK_END => SeekFrom::End(offset),
> _ => return Err(EINVAL),
> };
> ...
> Ok(off as bindings::loff_t)
> }
>
> Cheers,
> Miguel
next prev parent reply other threads:[~2023-02-26 22:13 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-24 8:50 [PATCH 0/5] rust: error: Add missing wrappers to convert to/from kernel error codes Asahi Lina
2023-02-24 8:50 ` [PATCH 1/5] rust: error: Add Error::to_ptr() Asahi Lina
2023-02-25 22:14 ` Gary Guo
2023-02-26 14:26 ` Miguel Ojeda
2023-03-07 20:21 ` Miguel Ojeda
2023-02-24 8:50 ` [PATCH 2/5] rust: error: Add Error::from_kernel_errno() Asahi Lina
2023-02-25 22:19 ` Gary Guo
2023-02-26 13:56 ` Miguel Ojeda
2023-02-27 13:27 ` Andreas Hindborg
2023-02-24 8:50 ` [PATCH 3/5] rust: error: Add to_result() helper Asahi Lina
2023-02-27 13:26 ` Andreas Hindborg
2023-02-24 8:50 ` [PATCH 4/5] rust: error: Add a helper to convert a C ERR_PTR to a `Result` Asahi Lina
2023-02-27 13:41 ` Andreas Hindborg
2023-02-27 13:52 ` Miguel Ojeda
2023-02-24 8:50 ` [PATCH 5/5] rust: error: Add from_kernel_result!() macro Asahi Lina
2023-02-24 23:56 ` Boqun Feng
2023-02-25 2:31 ` Asahi Lina
2023-02-25 22:23 ` Gary Guo
2023-02-26 2:22 ` Boqun Feng
2023-02-26 13:36 ` Gary Guo
2023-02-26 18:16 ` Boqun Feng
2023-02-26 20:59 ` Miguel Ojeda
2023-02-26 22:13 ` Boqun Feng [this message]
2023-02-27 12:10 ` Miguel Ojeda
2023-02-27 16:11 ` Boqun Feng
2023-02-27 13:59 ` Martin Rodriguez Reboredo
2023-02-26 13:40 ` Miguel Ojeda
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=Y/vZdv6IVmrcP7wR@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=asahi@lists.linux.dev \
--cc=bjorn3_gh@protonmail.com \
--cc=foxhlchen@gmail.com \
--cc=gary@garyguo.net \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=thesven73@gmail.com \
--cc=wedsonaf@gmail.com \
/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).