Originally posted by uid313
View Post
Announcement
Collapse
No announcement yet.
Linux 6.9 To Upgrade Rust Toolchain - Making More Features Stable
Collapse
X
-
Originally posted by uid313 View Postpublic traits cannot have any async functions
traits can only have functions but not fields (why? is this because I should define getter/setter functions instead?)
I don't like the double colon :: syntax because it is ugly, and not needed, I think just a normal dot should be used instead.
the compiler is slow
if the input parameter is a reference, i.e. &foo, then when I match I must do match &foo, I can't do match foo, why?
- Likes 2
Comment
-
Originally posted by Quackdoc View Post
Is the compiler slow? I find large rust projects to compile fast enough for me, and I run it on a crappy Intel N3050
- Likes 5
Comment
-
Originally posted by uid313 View Post[LIST][*]traits can only have functions but not fields (why? is this because I should define getter/setter functions instead?)
In this case, it's that, at best, it'd make things much more complex to specify when also supporting dynamic dispatch, for a convenience improvement that hasn't been demonstrated to be worthwhile enough to merit it.
In C++, you can do that because:- they cram the dynamic dispatch stuff into your class whether or not a given location is using it, resulting in everyone having to be concerned with whether a struct or class is POD (Plain Old Data) or not because C++ has just enough of that GCed language "just make it work"-ness that you have to worry about it slipping in hidden struct fields to power the dynamic features you didn't realize you were requesting.
- They're willing to accept inefficiencies in how they implement inheritance that the Rust team don't want.
In GCed languages, you're already paying for a ton of Run-Time Type Information, so they just hang it off that infrastructure.
In Rust:- A statically dispatched struct is just data, and its methods are just syntactic sugar for bare functions taking it as the first argument. Something like my_struct.foo() compiles to roughly the same thing as something like my_struct_foo(my_stuct_ptr) would in C.
- A trait object (dynamic dispatch) is a fat pointer that's basically an (object_instance, trait_vtable) tuple. The object_instance pointer is literally just the address of an opaque blob of data, identical to the data blob used in static dispatch, while the trait_vtable is the same memory address for every instance that resolves to that impl Trait for Struct. This ensures you only pay for dynamic dispatch when you ask for it. (i.e. In C++, dynamic dispatch is a property of the class/struct. In Rust, dynamic dispatch is a property of the reference to it.)
- The Rust philosophy is "make costs explicit", so Rust isn't exactly eager to implement trait fields via something like Python's @property decorator which hides function calls behind the member access (i.e. period) operator.
Last edited by ssokolow; 11 March 2024, 12:51 AM.
- Likes 5
Comment
-
Michael ptr_metadata isnt stable (
​​https://github.com/rust-lang/rust/issues/81513), maybe something else allowed them to stop depending on that feature?
EDIT: Says further down: "Stop using the unstable 'ptr_metadata' feature by employing the now ​​​​​stable 'byte_sub' method to implement 'Arc::from_raw()'.". So that explains it.Last edited by Vorpal; 11 March 2024, 04:53 AM.
- Likes 1
Comment
-
Originally posted by samcday View Post- Public traits *can* have async functions, there's just caveats that are still being worked on: https://blog.rust-lang.org/2023/12/2...in-traits.html ... Think of async support in traits kinda like const generic support - it's something that takes *years* to fully implement and stabilize because it needs to be done very, very carefully.
- Why would traits need fields? In that case you use a struct. Analogy: Java interfaces (which are sorta like traits) can't have fields. Golang interfaces cannot have fields.
- The normal dot means something different in Rust syntax. Sounds like you're confusing two very different things.
- Libraries that use async are absolutely *not* tied to a specific runtime. If they are, they're not designed properly. It might be that they pull in tokio to use supporting functions to better compose and control async flow.
- All of the questions/concerns you've raised are already addressed in the Rust book and Rust async books: https://doc.rust-lang.org/book/ + https://rust-lang.github.io/async-book/
The dot is used to reach the functions on the struct, but to reach the associated functions I have to use the double-colon syntax. I would like to use the dot for both like in C#, Java and Python.
- Likes 1
Comment
-
Originally posted by uid313 View Post
I want fields in traits because I have a dozen structs representing different message types which all need to have a "id" field. So I wanted all the structs to implement a trait to get the id field. I can have a get_id() function in the trait, but I cannot create a default implementation for it. .
It might be necessary to work with two traits here, one for the ID and one for the rest. The 2nd trait for the rest would require your type to implement the ID trait.
- Likes 1
Comment
-
Originally posted by dremon_nl View Post
Problem with async runtime is library-specific, has nothing to do with the language. There are many runtime-independent crates. I am the author of one of them so I know.
But the runtime situation is probably not a very big issue for kernel development. I don't expect them to do much async, and if so I assume they will use their own custom kernel runtime, and on top of that I also assume they will not pull in random libraries from crates.io but most likely write their own.
- Likes 3
Comment
-
Originally posted by Quackdoc View Post
Is the compiler slow? I find large rust projects to compile fast enough for me, and I run it on a crappy Intel N3050
C++ is also comparatively slow (maybe even moreso due to templating being a crazy beast)
- Likes 1
Comment
Comment