This crate provides an error handling abstraction which, among other things, allows to build chains of errors and iterate these chains. One example where this could be useful (which looks reasonable to me, but could be a totally terrible idea!) would be a failure crate. However, what if we want to dispatch a call based on both a type of the interface we want to dispatch on and the concrete type? What does it mean and why would we even want that? Is it a Failure? Finally, the code would call that function, providing a pointer to the data as the first parameter (which becomes &self in the function). Then, it would lookup the virtual table to get the address of the function to call (each function is assigned a unique slot in the table, so this is a simple indexing operation). The second pointer points to a virtual table where each “slot” is the address of the function.Įvery time Rust compiler generates code to invoke a function on a trait object, the generated code would destructure the trait object into two pointers, the data pointer and virtual table pointer. The first pointer points to the data itself. Again, I am going to skip the details, but the idea is that a trait object is represented internally by two pointers. The way Rust dynamic dispatch works is via a special reference type, a trait object which provides a way to dispatch a call based on a concrete type. Instead, I would like to do an experiment of making dynamic dispatch even more dynamic! Like in Java 1. I’m going to skip the basics about the traits and just link to another blog post with a good explanation about static and dynamic dispatch in Rust: Traits and Trait Objects in Rust. In Rust, traits are a powerful tool to use polymorphism, both static and dynamic.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |