Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by umanwizard on Sunday May 05, 2024 @12:30PM
(#40266073)
You can actually implement the C++ behavior, if you want:
unsafe fn super_unwrap<T>(x: Option<T>) -> T {
match x {
Some(val) => val,
None => unreachable_unchecked!(),
}
}
But defaults matter, and Rust certainly doesn’t make this kind of thing ergonomic (which is a correct decision on the Rust designers’ part).
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by kaashif on Sunday May 05, 2024 @02:35PM
(#40267096)
Yeah, absolutely. My point is that Option itself doesn't give you this API and to make an unsafe version, you have to explicitly write it.
Including UB in easy to misuse places is totally unnecessary and a footgun which really does cause issues in real code.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by umanwizard on Sunday May 05, 2024 @05:49PM
(#40268798)
Yep, I agree completely. Just wanted to point out for completeness that rust can theoretically do the same thing as c++.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by tialaramex on Sunday May 05, 2024 @08:21PM
(#40269893)
You don't have to write this, it already exists as the (unsafe of course) method Option::unwrap_unchecked
Because all Rust's methods can be called as free functions, you can literally write Option::unwrap_unchecked for the same behaviour, or you can some_option.unwrap_unchecked() (in both cases you will need to be in unsafe context for this to be allowed and should write a SAFETY comment explaining why you're sure it's correct)
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by umanwizard on Monday May 06, 2024 @08:40AM
(#40273934)
I see. I didn't know that method existed despite spending ~4.5 years writing Rust.
"Make everything constexpr" isn't a real solution to UB, in the same way that "make all functions pure" isn't a solution for managing side effects.
Not adding UB to your APIs, on the other hand, is a real solution.