Type-Level Programming in Rust
willcrichton.net
Type-Level Programming in Rust
1–10 of 29 posts
Re: Type-Level Programming in Rust
#2The 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)!
Re: Type-Level Programming in Rust
#3However, 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 {
chan: ...,
_state: PhantomData
}
// Methods for the state are uniquely associated with only the state
impl Channel {
// recv consumes ownership, ensuring old state is invalidated
fn recv(mut self) -> (Channel, 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 {
fn send(mut self, msg: String) -> Channel {
let Channel { chan, .. } = self;
chan.send(msg);
Channel { chan, _state: PhantomData }
}
}
#[test]
fn channel_test() {
let c: Channel = Channel::new();
let c: Channel = c.send("hi");
let (c, msg) = c.recv();
// and so on
}Re: Type-Level Programming in Rust
#4I'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…
Re: Type-Level Programming in Rust
#5Re: Type-Level Programming in Rust
#6Re: Type-Level Programming in Rust
#7Finding the right balance between using type-level programming to ensure invariants and not using it is a important skill.
Re: Type-Level Programming in Rust
#8I'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…
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!)
Re: Type-Level Programming in Rust
#9Re: Type-Level Programming in Rust
#10I'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…
Im 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!)
Say no more! Any brother of such renowned talent is sure to know what they're talking about.