r/rust • • Aug 29 '24

One Of The Rust Linux Kernel Maintainers Steps Down - Cites "Nontechnical Nonsense"

https://www.phoronix.com/news/Rust-Linux-Maintainer-Step-Down
586 Upvotes

374 comments sorted by

View all comments

Show parent comments

3

u/looneysquash Aug 29 '24

(Still haven't been able to watch the whole thing, so might not what what I'm talking about)

Result<Either<ARef<INode<T>>, inode::New<T>>> is a pretty complicated type.

I'm not sure how to fix it. But like, for a return type, you'd say struct Foo, not struct { type1 field1, type2 field2, etc... }. This feels more like the latter, where too many details are present.

You could use a type alias, but I'm not sure if that's a good idea here or not, because a type alias would make it less obvious how to use match / destructuring with this.

9

u/Svenskunganka Aug 29 '24

I disagree that it is a complicated type, and I think most Rust users could deduce from a glance that it is a Result that contains either a reference-counted inode, a new inode or an error. It is a bit wordy yes, but it is explicit and won't catch you by surprise with "oh I forgot that I need to check if it is a new inode. If it is I need to initialize it and if I don't initialize it I need to clean it up". It is there, encoded in the types and cannot be misused. In the C code, this is something that you'd just have to know and keep in mind, and if you work with 20 other APIs that has similar behaviours, chances are you're going to forget some details about this that leads to at best someone catching it in review or at worst production bugs.

6

u/looneysquash Aug 29 '24

I disagree that it is a complicated type

Why do you think it's not complicated?

There are 5 different types mentioned, plus the type parameter T.

If someone wrote:

result(either(a_ref(inode(t), inode_new(t))));

and you were reviewing the code, would you say that it's simple, or would you say that it's too complicated, and ask them to refactor it into mulitple statements?

I would suggest breaking it up, if I was the reviewer.

To be fair, if someone wrote:

struct SomeName {
  int result;
  int either;
  int a_ref;
  int inode;
  int t;
  int inode_new;
  int tt;
};

I wouldn't call that a complicated struct. So I suppose it's the nesting that makes it complicated in my view.

I think most Rust users could deduce from a glance that it is a Result that contains either a reference-counted inode, a new inode or an error.

I'm not so sure about the "at a glance" part of your statement.

It is there, encoded in the types and cannot be misused

I agree, and I consider that to be a good thing.

That would be just as true if it was a type alias, let's say IgetLockedResult<T>. What are your thoughts on that?

Humans can only hold so many indepdent things in their head at one time. structs are, amoung other things, a memory aid. They're a way to grouping things together into a single thing to think about. I think type aliases can serve a similar purpose.

To be fair, I did say I was unsure of if it was a good idea to use one here or not.

In the C code, this is something that you'd just have to know and keep in mind, and if you work with 20 other APIs that has similar behaviours, chances are you're going to forget some details about this that leads to at best someone catching it in review or at worst production bugs.

Yes, 100%! At least in isolation.

But if you're trying to maintain parity between the Rust and C APIs, you might think twice about adding these types of improvements. Making the APIs too different can cause problems switching between them. But maybe not in this case, maybe that's for the more complicated one they alluded to ;)

To be clear, I don't work on the kernel, so I'm a little uncertain, but I do lean towards saying that that Rust API is appropriate and is what they should be doing. But everything is a trade off.

4

u/Svenskunganka Aug 29 '24 edited Aug 30 '24

Perhaps I was a bit unclear in my comment, but what I'm refering to is what details the return type encodes; the intricacies of the C API it wraps. For that goal, it does so well and can be understood by a Rust user. Of course, there are ways to make it shorter with e.g a type alias but the complexity of the type remains the same. Even with an enum as the other commenter posted:

enum Foo<T> {
    Existing(ARef<INode<T>>),
    New(inode::New<T>),
}

The return type changes to Result<Foo<T>> but the complexity of the type remains almost the same. I say almost because the enum is a bit better since it is either an Existing or a New, while for Either it is Left or Right and the former more clearly explains the difference.

I also don't want to put too much weight on the character length on the return type because 1) it might have been demoed like that in the slides so that the non-Rust users in the audience can digest that the Rust implementation has indeed encoded all of the subtle details of the C API and 2) because the author had just posted the patch that same morning and such details would probably have been iterated and improved upon with the rest of the RFL team in review.

What I'm ultimately referring to is that the type itself encodes the details of the C API in an excellent way that can be understood and used "safely" by a Rust user with arguably less complexity than what the C API requires a C programmer to keep contextualized in their head - and to that I say well done! ๐Ÿ‘

4

u/sunshowers6 nextest ยท rust Aug 29 '24

I would probably have a concrete enum Foo<T> with Existing and New variants and just return that. (In general I think Either should be sparingly used.)

1

u/[deleted] Sep 18 '24

You can hide parts of it with enums, but the "fixing" needs to happen on the C side because the C function produces all that information. Rust bindings can't return Result<INode<T>> if C API doesn't produce an owned INode and instead returns "either a reference to an exisiting INode or creates one if it doesn't exist".

1

u/el_muchacho Aug 29 '24

The problem is, it's not even anywhere near the real behavior it is supposed to mimic. The real, final Rust type would be a lot more complex. That's what the russian guy is trying to say.