3 ms·
Functional domain modeling in Rust – exploration
- somethingsome 2y agoAfter watching this talk https://youtu.be/2JB1_e5wZmU?si=WXoYxGnSOoMYwCFU https://youtu.be/2JB1_e5wZmU?si=WXoYxGnSOoMYwCFU I was wondering how it could be implemented in Rust
- whytevuhuni 2y agoI watched the talk, I agree that was really useful. I did something similar, but for string references [1], rather than owned strings. It was more for type-safety rather than any sort of domain modeling; you can cast a `&str` to a branded `&CandidateId`, which has a `.to_str()` method but otherwise will not go where a generic string is expected. The memory representation is the same, so it's mostly just a compile-time trick that disappears at runtime, same as newtypes for owned strings. I stole that idea (and the confidence for that unsafe transmute code) from a really popular crate, but I can't for the life of me remember which one that was. My implementation ended up... something I don't really like. I hope Rust will eventually make this kind of stuff easier (the nightly-only coerce-unsized would make that happen). [1] https://github.com/andreivasiliu/fluctlight/blob/master/fluctlight-router/src/matrix_types.rs https://github.com/andreivasiliu/fluctlight/blob/master/fluc...