Re:Diesel: A Safe, Extensible ORM and Query Builder for Rust
by Manishearth on Saturday February 06, 2016 @01:20PM
(#11048932)
This type of bound can be expressed with a trait and associated type already in Rust, fwiw.
We were working in Scala, and I had a go at solving this using a higher-kinded type for the ID and other changing fields:
https://github.com/tim-group/higher-kinded-lifecycle/blob/ma...
The idea is that if you say that the ID is of type 'T of String, where T is some subtype of Option', then you can bind T to None for entities that haven't been saved, Some for ones which have, and Option where you have a mixture. You then have one type definition, with which you can write stage-agnostic code, but static guarantees about whether there's an ID.
You can go further and use type constraints to enforce the order of lifecycle stages: if your object can only be saved to the database once it's been given a timestamp, say, you can make sure that the ID type can only be Some if the timestamp type is also Some.
So maybe when Rust gets HKTs it'll be time for a new release!