Is it possible to define various level for safety?
A real-world app may have more than one unsafe level:
level -1: unsafe operations, which may cause double free and various hardware related bug.
level 0: safe operations that access user's password
another level 0: safe operations that send messages to a private LLM api (may waste a lot of money)
level 1: publish comment into forums which could be redraw easily.
Rust could alert users very well if they misuse some unsafe code, but IMHO, use pure safe rust code to launch a nuclear missile is more dangerous than many unsafe operations.
Is it possible to utilize the unsafe block to define more than just one unsafe sematics?
unsafe fn transmute() {}//transmute is always unsafe
unsafe(abort) fn panic() {
panic!("suppose you are calculating for 10 hours, and the program aborts")
}
unsafe(any_custom_ident) fn launch_nuclear_weapon() {
// since we cannot cover all situations, define such a keyword is fine
}
Any function which call any type of unsafe function should either:
- use an unsafe block without any modifier.
- use an unsafe block with the correct modifier, and put that modifier intofunction's signature.
fn trust_me() {
unsafe{/*I can call any function here, I said it is safe since.......*/}
}
unsafe (ptr) fn play_with_ptr() {
unsafe (ptr) {
play_with_ptr();
}
}
rule 2 is a coloring rule, each function calling unsafe X must be X. To avoid this, a pure unsafe block is used.
Discussion in the ATmosphere