4 ms·
Speaking of "guaranteed not to allocate", is there a way that you could express that in a type? Seems like that might be nice to have.
by kalkin 10y ago
Speaking of "guaranteed not to allocate", is there a way that you could express that in a type? Seems like that might be nice to have.
- tatterdemalion 10y agoNot in Rust's type system. In a pure language, you could have some sort of "Heap" monad similar to the "IO" type in Haskell.
- Too 10y agoSo Rust has no way to mark side effects and global dependencies of functions? Allowing singletons in a language that is supposed to be safe sounds like a huge design flaw.
- steveklabnik 10y agoMutable statics are unsafe to access or update. You can use interior mutability with something like a Mutex to get a mutable-but-not-to-rustc value, which is safe. Systems programming languages need this kind of functionality.
- Too 10y agoPutting a mutex around a global variable doesn't change the fact that it is still a global variable. Memory access might be safe but you get spaghetti code and combinatorial state explosion due to all the potential side effects. Allowing singletons for edge cases is fine but with no proper way to enforce it except code review you really have now idea what the underlying code might potentially do.
- steveklabnik 10y agoI agree with you that using globals as sparingly as possible is good, but your original claim was about safety, so that's what I focused on.
- killercup 10y agoNot in the type system itself, but you could write a lint to forbid heap allocation. This way, you could annotate a function (with e.g. `#[forbid(allocations)]`) to get a compile error when your function (or code your function calls) tries to allocate. This might not be easy, though :)