Type-checked keypaths in Rust

#108 · ✸ 77 · 💬 20 · one year ago · www.cmyr.net · pcr910303 · 📷
Like a number of other folks, I have recently been exploring some slightly less well-worn corners of the Rust type system. Our goal is simple enough: we want a way to generate code, at compile time, that can verify that a particular path exists, starting at a base type and ending up at some other type. Rust does not allow you to make 'inherent impl' for a type in an external crate; in this case, the KeyPath type is declared in the keypath crate, which means it is external to wherever this code is being generated. The basic idea is that each participating type implements a trait that has an associated type, the mirror. The mirror type itself is a way of inspecting the types of the fields on the main struct; the mirror has the same fields as the parent type, but those fields themselves return the mirror of the type of the parent's corresponding field. Mirrors have specially-named methods that return the mirror of the collection's member type. This is simple enough; we know the root type already, and we know the type of the mirrored object when we generate the mirror; so we can just add a method to each mirror type that looks like,.
Type-checked keypaths in Rust



Send Feedback | WebAssembly Version (beta)