5 ms·
Type-Level Programming in Rust
- jswrenn 6y agoA few months later, Will released Tyrade, an impressive macro-based DSL for type-level programming in Rust: https://github.com/willcrichton/tyrade https://github.com/willcrichton/tyrade The README uses some of the same motivating examples as the blog post. Type-level programming absolutely has its place. Rust's `typenum` and `generic_array` crates enabled projects like `nalgebra` long before const generics. I recently used type-level programming to prototype an extension to Rust's type system — that's now an RFC (#2981)!
- mateus_amin 6y agoThat is really cool. Would "Use types as data primitives" be a good way to describe Tyrade?
- couchand 6y agoI'm immediately turned off by the entirely gratuitous use of unsafe here. I clicked through to the linked Reddit discussion and I'm not convinced the author has really thought it through. Writing, "pedagogically, that's irrelevant to the post, so I didn't highlight it" misunderstands the pedagogy here. However, there's some really neat stuff later, so it's wise to grit your teeth and get past it. In case anyone else had the same reaction, here's an entirely-safe version of the first snippet that you can pretend replaced the original: // Each state is a unique type struct Receiving; struct Sending; // The state machine is parameterized by the state struct Channel<State> { chan: ..., _state: PhantomData<State> } // Methods for the state are uniquely associated with only the state impl Channel<Receiving> { // recv consumes ownership, ensuring old state is invalidated fn recv(mut self) -> (Channel<Sending>, String) { let Channel { chan, .. } = self; let msg = chan.recv(); // The state type changes after executing a transition let next = Channel { chan, _state: PhantomData }; (next, msg) } } impl Channel<Sending> { fn send(mut self, msg: String) -> Channel<Receiving> { let Channel { chan, .. } = self; chan.send(msg); Channel { chan, _state: PhantomData } } } #[test] fn channel_test() { let c: Channel<Sending> = Channel::new(); let c: Channel<Receiving> = c.send("hi"); let (c, msg) = c.recv(); // and so on }
- guitarbill 6y agoThank you for going to the trouble of posting the code. I kind of agree with your point here, especially because the safe version is hardly more arduous.
- jzoch 6y agoIm not disagreeing with you but fyi will crichton definitely knows what he is talking about. He teaches Programming Language courses at Stanford and his brother is the all-time top contributor to Rust. So, pedagogy + rust is sort of his thing. In general, this series of posts has been more about types than about rust so I don't blame him for ignoring unsafe. It isnt relevant (its also not a best practice!)
- monadic2 6y ago> his brother is the all-time top contributor to Rust. Say no more! Any brother of such renowned talent is sure to know what they're talking about.
- couchand 6y agoTeachers are just like everyone else: they're fallible and half of them are below average.
- db48x 6y agoWell, teachers are a subset of humanity and thus in principle they could all be above the overall average for humanity. It's even intended that there be a selection bias, so hopefully most of them are at least above average at teaching if nothing else. Of course half of them are worse than the average teacher, but that's not very useful information.
- wcrichton 6y agoHi, author of the code here. Why do I use mem::transmute? To highlight the fact that when using typestate, it's useful to understand operations that change the the value of an object as distinct from those that change the type of an object. It's also a matter of efficiency -- you don't have to generate a new struct every time you change state. You can avoid the overhead without transmute, but it's still unsafe and not easily explained: https://github.com/Munksgaard/session-types/commit/3a310d205cec567bed719d526d4a03c49825d583#diff-b4aea3e418ccdb71239b96952d9cddb6R200 https://github.com/Munksgaard/session-types/commit/3a310d205...
- deleted 6y ago[deleted]
- Arnavion 6y agoSee also this comment where I showed a way to doing such a thing using constants for the type parameters instead of types, using the currently-unstable "const generics" feature. https://news.ycombinator.com/item?id=22747405 https://news.ycombinator.com/item?id=22747405
- dathinab 6y agoJust please don't overdo type-level programming, especially not if it involves trait bounds and wildcard impl. Finding the right balance between using type-level programming to ensure invariants and not using it is a important skill.
- ragnese 6y agoEspecially so with a language like Rust, IMO. Its type system is powerful enough that you kind of can do it, but it gets awkward pretty quickly. In some ways that's a dangerous combo!
- seanmceligot 6y agoThe SecureVec is nice. I've been waiting to see this become more common. Reminds me of this talk at ICFP 2010 on compile time Authorization checks. http://jamiemorgenstern.com/papers/ml10sectyp.pdf http://jamiemorgenstern.com/papers/ml10sectyp.pdf
- kevincox 6y agoOne minor note is that I don't think PhantomData is required here. _state: PhantomData<State> Since State is zero-sized it is free to store it in the struct. state: State IIUC PhantomData is only required when you need to indicate that you are logically storing something of that type but don't want to actually store it. Since storing is free in this case you may as well just store it.
- lilyball 6y agoThis sounds reasonable, but personally I prefer the PhantomData as it makes it immediately obvious to the reader that this is a zero-sized type.
- charlieflowers 6y agoFYI if the author is around. All the code blocks render as one continuous line for me (no line breaks) Tried it in both Firefox and Chrome.
- zanellato19 6y agoIt was the same for me and then rendered correctly after a refresh. Weird