A Rust Design Pattern for Linear Types

For ECS-related reasons, I really wanted a design pattern that could emulate linear types in Rust. After reading a bunch about functional programming, I came up with a design pattern inspired by Haskell's Cont monad which works for just about any use case where one would want a linear type. Read the Medium article here!

The Destruct trait is being developed rn, so after it's added a impl !Copy + !Destruct + !Send would give you a linear type, wouldn't it?

(Note: if it is Send, then you can send it to a thread which will then loop forever, so a value is lost.)

Yeah! FIoC for linear types will pretty much become obsolete when linear types are fully added to the language

How does this prevent more elaborate single-threaded methods to forget types, like reference cycles? Do types like Rc have to bound on Destruct now?

I think you mean !Forget. Or perhaps !Forget + !Destruct.

What would unwinding do when confronted with a !Destruct type, if such a thing exists? Abort?

I thought the intent was for everything to implement Destruct (and only some things implement const Destruct). (Edit: ah, and a hypothetical !Forget + !Destruct type would have total control over how it’s discarded or destroyed in safe code.)

Rc (and mem::forget) would need to bound on Forget, which would probably be accomplished by making Forget an implicit default on generics, like Sized is. And Destruct too, I guess, if !Destruct types are ever permitted.

And both would probably be auto traits. Sigh. An explosion of default auto traits is unfortunate.

You might be interested in extend_mut, which forces its user to return a certain value (else abort the process). I tried for a while to find unsoundness in it, but couldn’t find any… in its intended interface.

Amusingly, there was a certain error path that tried and failed to abort the process, resulting in unsoundness (which has since been fixed).

Yeah, it starts to feel a lot like an effects system shoehorned in after the fact. Because that's basically what it is... and I'm not sure there's a better option.