#unwrap #unsafe #result #options

no-std unsafe_unwrap

Unsafely unwrap Result and Option types without checking

1 unstable release

Uses old Rust 2015

0.1.0 Jul 12, 2017

#266 in No standard library

Download history 9714/week @ 2024-07-21 3981/week @ 2024-07-28 4716/week @ 2024-08-04 2600/week @ 2024-08-11 4718/week @ 2024-08-18 4871/week @ 2024-08-25 4082/week @ 2024-09-01 5880/week @ 2024-09-08 5179/week @ 2024-09-15 6024/week @ 2024-09-22 5454/week @ 2024-09-29 5179/week @ 2024-10-06 9106/week @ 2024-10-13 8192/week @ 2024-10-20 4581/week @ 2024-10-27 3611/week @ 2024-11-03

25,562 downloads per month
Used in 50 crates (8 directly)

MIT/Apache

8KB
87 lines

unsafe_unwrap

A Rust library that enables unchecked unwrapping on Option and Result types.

Usage

The unsafe_unwrap() method can be used anywhere unwrap() is used. It behaves similar to unwrap() in unoptimized builds and will remove checks in optimized builds.

extern crate unsafe_unwrap;
use unsafe_unwrap::UnsafeUnwrap;

let x = Some(42);
let y = unsafe { x.unsafe_unwrap() };

Benchmark

bench_normal_unwrap_1000 bench_unsafe_unwrap_1000
929 ns/iter (+/- 176) 302 ns/iter (+/- 28)

License

This project is released under either:

at your choosing.


lib.rs:

Provides a way to quickly yet unsafely unwrap types whose inner values are known to exist.

By calling unsafe_unwrap(), the compiler is told, in optimized builds, that the unwrap will never fail. In debug builds it will emit a panic.

Sometimes the optimizer can remove checked unwrapping if it can prove that a value exists. However, in times that it may not be able to do so, this works as an alternative.

This is akin to the unsafelyUnwrapped property of Optional in Swift.

Examples

use unsafe_unwrap::UnsafeUnwrap;

let x = Some(20);
let y = unsafe { x.unsafe_unwrap() };

No runtime deps