2 comments

[ 5.1 ms ] story [ 12.9 ms ] thread
I 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/fluc...