#unsafe #unwrap #option #result

no-std unsafe_unwrap

Unsafely unwrap Result and Option types without checking

1 unstable release

0.1.0 Jul 12, 2017

#214 in No standard library

Download history 395/week @ 2020-03-05 199/week @ 2020-03-12 433/week @ 2020-03-19 408/week @ 2020-03-26 292/week @ 2020-04-02 294/week @ 2020-04-09 272/week @ 2020-04-16 293/week @ 2020-04-23 414/week @ 2020-04-30 494/week @ 2020-05-07 564/week @ 2020-05-14 316/week @ 2020-05-21 334/week @ 2020-05-28 293/week @ 2020-06-04 499/week @ 2020-06-11 429/week @ 2020-06-18

1,954 downloads per month
Used in 5 crates (3 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