Get the latest tech news
“Bypassing” specialization in Rust
Pointers I've spent nearly a year developing and refining my own FAT driver in Rust. For much of the last six months, I had to put the project on hold due to school commitments.
During that journey, I (almost) learned how FAT and filesystems in general work behind-the-scenes and in my attempts to navigate the constraints imposed by the Rust programming language, I encountered what I thought was an immovable obstacle: specialization Both constructors call roughly the same code (in fact I use a macro, akin, that essentially duplicates a single impl block that I don't have to deal with duplicate code) but when they construct the FileSystem struct, the RO FileSystem constructor passes None to a struct field that takes an Option with a function pointer, while in the second case we pass a Some value containing a pointer to a function that is only accessible from this scope. My solution to my issue is not only pretty specific to my use case, but it also introduces additional overhead to the struct, both when it comes to performance (every time the load_nth_sector function is called and the sector buffer is modified, Rust must match that very field, even though its value doesn't change at all from the FileSystem's construction until it is dropped.
Or read this on Hacker News