|
|
Subscribe / Log in / New account

Rust heads into the kernel?

Rust heads into the kernel?

Posted May 1, 2021 0:01 UTC (Sat) by tialaramex (subscriber, #21167)
In reply to: Rust heads into the kernel? by micka
Parent article: Rust heads into the kernel?

I guess I still don't understand. How is the code you pointed to _returning_ a new object? The function is clearly defined without a return type, and indeed doesn't return anything [technically it returns the empty tuple], it just alters self to achieve its purpose.

In some languages the add-assign operator has a result type such that if you write a = (b+= 1) then both a, and b become b + 1. But in Rust that type is always the empty tuple, I just checked and as expected Rust allows me to do this, but not to any sort of integer variable a since (b+= 1) is an empty tuple and thus incompatible.


to post comments

Rust heads into the kernel?

Posted May 1, 2021 11:14 UTC (Sat) by mathstuf (subscriber, #69389) [Link] (1 responses)

I think there was some confusion. I don't think `AddAssign` can be used where allocation is a failure on types which may need to allocate. I guess one could define it for `Result<String, AllocationError>` where if it fails, it turns into the Err variant, but that doesn't sound great. I think String just loses AddAssign in a fallible allocation world. Which I think is fine, but I am also fine with explicit error checking everywhere it is needed. I feel like the kernel would prefer calculating a single buffer size then allocating it all at once rather than building up a string piecemeal, but I don't know.

And yes, it appears as though assignment does not return a reference to the object as it does in C and C++. One could write a small function which worked that way, but then you lose the `=` spelling of assignment. With lifetime tracking, getting it to work in practice is probably not that easy anyways outside of Copy types.

Rust heads into the kernel?

Posted May 2, 2021 5:47 UTC (Sun) by tialaramex (subscriber, #21167) [Link]

OK great, I think we're both on the same page now, *phew*. I learned some things about Rust and how to interpret Trait definitions, which was valuable.

I'm excited to see kernel Rust once it does have its own alloc implementation.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds