#unwrap #expect #result #unchecked #options

no-std unchecked_unwrap

Adds an unchecked version of unwrap() and expect() to Option and Result

10 releases (stable)

4.0.0 Mar 22, 2022
3.0.0 Jan 29, 2021
2.0.2 Jun 21, 2020
1.0.1 Feb 25, 2019
0.9.0 Dec 7, 2018

#2764 in Rust patterns

Download history 1116/week @ 2024-01-05 1194/week @ 2024-01-12 1060/week @ 2024-01-19 1408/week @ 2024-01-26 1273/week @ 2024-02-02 1303/week @ 2024-02-09 1440/week @ 2024-02-16 1774/week @ 2024-02-23 2023/week @ 2024-03-01 1810/week @ 2024-03-08 2113/week @ 2024-03-15 2197/week @ 2024-03-22 1950/week @ 2024-03-29 1644/week @ 2024-04-05 1776/week @ 2024-04-12 1844/week @ 2024-04-19

7,577 downloads per month
Used in 6 crates (5 directly)

MIT/Apache

12KB
60 lines

unchecked_unwrap

Crates.io Libraries.io Commits since License LoC

Release: Build Docs

Master: Build Docs

WARNING ⚠️

Since Rust v1.58 this crate is useless, see:

Table of contents

Description

Adds an unchecked version of unwrap() and expect() to Option and Result for the Rust programming language. Supports no_std.

Branches

  • release - For releases only.
  • master - For active development, PR's and testing.

Usage

use unchecked_unwrap::UncheckedUnwrap;

let x = Some("air");
assert_eq!(unsafe { x.unchecked_unwrap() }, "air");

let x: Result<u32, &str> = Ok(2);
assert_eq!(unsafe { x.unchecked_unwrap() }, 2);

let x = Some("value");
assert_eq!(unsafe { x.unchecked_expect("the world is ending") }, "value");

let x: Result<u32, &str> = Ok(2);
assert_eq!(unsafe { x.unchecked_expect("the sky is falling down") }, 2);
checked unchecked
fn test_checked(value: Option<&str>) -> &str {
    value.unwrap()
}
fn test_unchecked(value: Option<&str>) -> &str {
    unsafe { value.unchecked_unwrap() }
}
push    rax
test    rdi, rdi
je      .LBB2_1       // panic handler
mov     rdx, rsi
mov     rax, rdi
pop     rcx
ret
mov     rdx, rsi
mov     rax, rdi
ret

Crate features

  • debug_checks - On by default. Enables the normal checking behavior with panics when cfg(debug-assertions) is enabled.

Documentation

Documentation is available online in the badge links above.

Tests

Is as simple as cargo test and cargo test --release.

Benchmarks

Is as simple as cargo bench. Currently the nightly version of Rust is needed for benchmarking.

A sample result from the CI running on Github Actions:

test checked::expect_option   ... bench:         798 ns/iter (+/- 90)
test checked::expect_result   ... bench:         724 ns/iter (+/- 109)
test checked::unwrap_option   ... bench:         802 ns/iter (+/- 52)
test checked::unwrap_result   ... bench:         743 ns/iter (+/- 176)
test unchecked::expect_option ... bench:         407 ns/iter (+/- 93)
test unchecked::expect_result ... bench:         374 ns/iter (+/- 48)
test unchecked::unwrap_option ... bench:         345 ns/iter (+/- 53)
test unchecked::unwrap_result ... bench:         407 ns/iter (+/- 22)

Alternatives

Both alternatives and this crate are quite the same except that this crate provides additional features that can be toggled with cargo features. See crate features for details.

Changelog

See the CHANGELOG file for details

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps

Features