#options #implicitly-unwrapped #auto-unwrap

no-std dangerous_option

This crate provides DangerousOption - a type similar to ! in Swift language. It's basically an Option which panics if dereferenced while containing None. no_std compatible.

2 unstable releases

Uses old Rust 2015

0.2.0 Sep 11, 2017
0.1.0 May 8, 2017

#472 in No standard library

Download history 13/week @ 2024-07-20 20/week @ 2024-07-27 18/week @ 2024-08-03 21/week @ 2024-08-10 11/week @ 2024-08-17 16/week @ 2024-08-24 21/week @ 2024-08-31 14/week @ 2024-09-07 15/week @ 2024-09-14 43/week @ 2024-09-21 61/week @ 2024-09-28 4/week @ 2024-10-05 13/week @ 2024-10-12 8/week @ 2024-10-19 10/week @ 2024-10-26 34/week @ 2024-11-02

65 downloads per month
Used in 3 crates (via rdedup-lib)

MITNFA license

9KB
106 lines

Dangerous Option

About

This crate provides DangerousOption - a type similar to ! in Swift language. It's basically an Option which panics if dereferenced while containing None. This is useful in cases one needs to initialize things a little bit later or when accesses are made via functions called from trusted library.

While such thing might look like a step back (there's a reason we don't have NULL pointers in Rust), there is still one advantage over classic approach of NULL-pointer exceptions (including manually unwrapping): the cause of the bug is usually not in the place where dereferencing happened but in the place where assignment happened. Since this type has only three, very unique methods for creating invalid value, those can be easily searched for and tracked.

This crate is no_std.

License

MITNFA


lib.rs:

This crate provides DangerousOption - a type similar to ! in Swift language. It's basically an Option which panics if dereferenced while containing None. This is useful in case one needs to initialize things a little bit later or when accesses are made via functions called from trusted library.

While such thing might look like a step back (there's a reason we don't have NULL pointers in Rust), there is still one advantage over classic approach of NULL-pointer exceptions (including manually unwrapping): the cause of the bug is usually not in the place where dereferencing happened but in the place where assignment happened. Since this type has only three, very unique methods for creating invalid value, those can be easily searched for and tracked.

It has also intentionally long name to prevent over-use. Also the methods creating dangerous state have longer names then those creating valid state.

Note: you should prefer dereferencing the DangerousOption and passing dereferenced value instead of passing the reference to DangerousOption itself. That way you'll decrease chance of making mistake.

Finally, it also provides an exception handler which allows customizing panic message, logging, etc. There is a default handler which just panics, but in contexts where there is a more concrete, known cause of invalid operation, overriding the message is encouraged.

This crate is no_std.

No runtime deps