r/rust•
ripread

Help finding ugly Generics examples?

Hey all,

What are some of the ugliest things you've seen done with Rusts Generics system?

I remember there were a bunch of posts back before HKTs and HOFs. Do ya'll have any saved?

Posted Oct 31, 2019, 2:24 AM UTC

54
CAD1997•

My go-to type system abuse example is Frunk, mainly because they have such an approachable series of blog posts on how it does its magic:

HList (read me first): https://beachape.com/blog/2016/10/23/rust-hlists-heterogenously-typed-list
Generic (read me third): https://beachape.com/blog/2017/02/04/rust-generic-not-generics
LabelledGeneric (read me fourth): https://beachape.com/blog/2017/03/04/labelledgeneric-in-rust-what-why-how
HList again (read me second): https://beachape.com/blog/2017/03/12/gentle-intro-to-type-level-recursion-in-Rust-from-zero-to-frunk-hlist-sculpting
Transform (read me last): https://beachape.com/blog/2017/04/12/boilerplate-free-struct-transforms-in-rust

....And I just nerd snipped myself into rereading this series. It's an impressive masterclass on advanced type system (ab)use.

10
YatoRust•

uom, typenum, and frunk all have pretty extreme "abuses" of generics, but I still love them. It's really interesting how expressive you can be with Rust's type system.

varkor's post on idiomatic monads was also rather interesting. https://varkor.github.io/blog/2019/03/28/idiomatic-monads-in-rust.html

6
vadixidavrust-cv•

It is also important to note that a good portion of the Rust community regularly uses typenum as well due to the current lack of const generics. It is being used actively and is a dependency to several linear algebra libraries among other things. Nonetheless, I think everyone knows that it's a temporary solution, since it makes compile times go through the roof.

3
Programmurr•

Take a look at actix-web. It uses actix-net framework, which is very heavily based on generics. It's not ugly as much as it is hard to satisfy requirements for.

1