[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220728112114.0000003a@garyguo.net>
Date: Thu, 28 Jul 2022 11:21:14 +0100
From: Gary Guo <gary@...yguo.net>
To: Olliver Schinagl <oliver+list@...inagl.nl>
Cc: Miguel Ojeda <miguel.ojeda.sandonis@...il.com>,
Miguel Ojeda <ojeda@...nel.org>,
Linus Torvalds <torvalds@...ux-foundation.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
rust-for-linux <rust-for-linux@...r.kernel.org>,
Linux Kbuild mailing list <linux-kbuild@...r.kernel.org>,
Linux Doc Mailing List <linux-doc@...r.kernel.org>,
linux-kernel <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 00/13] [RFC] Rust support
Hi Olliver,
On Wed, 27 Jul 2022 10:05:31 +0200
Olliver Schinagl <oliver+list@...inagl.nl> wrote:
> Consitency is absolutly important! Zero argument there. My argument
> is, the consistency should be within the kernel tree, not 'but the
> rest of the world is using style X/Y/Z, lets be consistent with that.
> In an utopia, maybe, but the real world doesn't work that way, sadly.
> So in an attempt to standardize (rustfmt) they just "invented" a new
> standard. Which btw is common, we see this happening every so often,
> right?
Difference languages have different characteristics and I don't think
it's necessarily good (and often isn't) to blindly apply coding style
of one language onto another. So I don't see rustfmt as "inventing yet
another standard" really, because there aren't many conflicting coding
style standards in Rust world; almost everyone just settled on using
rustfmt, mostly using the default, maybe with a few small
project-specific configuration tweaks.
A small example for C and Rust differences:
Rust requires braces around branches of if expression, and C doesn't.
So in kernel coding style you often have:
if (condition) do_something();
Or
if (condition)
do_something();
But in Rust it will be:
if condition {
do_something();
}
That's just an example of one control flow constructions. There are
differences between Rust match and C switch, etc. Rust's official
coding style takes properties of Rust into consideration, so in many
regards it's a more suitable coding style for Rust code in kernel, then
applying kernel's C coding standard directly on kernel's Rust code.
Your earlier email in the thread also mentions about indentation, and I
have a few things to point out as well.
First, Rust code typically requires more levels of indentation than C
code. For example, many functions might be methods and they are inside
an impl block, which creates one extra level of indentation.
Statements inside match arms' block are two levels more indented than
the match statement itself, as opposed to C's switch (as kernel coding
style doesn't indent the case labels). As a result, 8 spaces for 1 level
can be a bit excessive for Rust code, and thus the 4 space indentation
used in rustfmt default.
Secondly, I don't think the argument about tabs being customisable
holds; in kernel coding style tabs are strictly 8 characters. For line
continuation it's not uncommon to use a series of tabs followed by a
few spaces, e.g.
int function_name(int first_argument,
< tab >< tab >..int second_argument)
changing tab into 4 spaces will break the layout. (and I'll not go into
well-known reasons about non-4-space-tab messing up code in terminal
etc).
> Copy/pasting is known to cause bugs. There's actually research from
> NASA on that. Code-reuse (libraries/functions) are not bad. But
> (worst kind of example) copy paste from stack-overflow, or
> copy/pasting stuff without actually looking at the content and
> forgetting to rename something, causes bugs. Why is this relevant?
> The whole 'lets be consistent with the rust codebase of the wrold'
> argument. E.g. if everybody uses the same style (which is idealistic
> and great) then copy/pasting becomes consistent. Where I say, try to
> be careful when copy/pasting code.
When we vendor in code as a whole (e.g. like we currently do for
alloc crate), it is proper code reuse. With different coding style the
vendored code either diverges from upstream (which makes upstreaming
much more difficult) or diverge from rest of kernel's Rust code base.
> But if that is the case, why not try to follow the kernels existing
> code-style as close as possible with the rust-fmt configuration? I
> know code-style has been discussed a few times over the decades; but
> not many changes have been done, surely, if there's some codestyle
> changes that are best for the kernel, they would have been 'advised'?
> '4 space indents are better then 8-size tabs, on new code, try to use
> them' for example :p
You do realize that you are creating a new coding style by doing this,
right? It feels like creating problems rather than solving problems.
My personal feeling is that it's easier for me to adapt to different
coding style when switching between languages, but it's rather awkward
for me when trying to use different coding styles with the same
language. I find myself no problem switching between 2 spaces when
coding JavaScript to 4 spaces when coding Rust to 8 spaces(tab) when
coding C, but it's rather painful to switch between C projects with
different coding styles. I certainly don't want to switch between Rust
projects with vastly different coding styles.
> But why? Why should we not be consistent with the kernels' code-base
> (while yes, that is not rust, but C, but we can follow the same
> style?)
Difference languages have different characteristics, and one size
doesn't fit them all :)
> Sadly, I've seen so much vendor code (yeah, I know) which doesn't
> even have consistency in their own files ...
That's very true. So when all other Rust code currently follow
(roughly) the same coding style and this situation doesn't currently
exist, let's not make it worse...
Best,
Gary
Powered by blists - more mailing lists