chain-assertions

Crates.iochain-assertions
lib.rschain-assertions
version0.1.2
created_at2025-11-13 00:50:24.549618+00
updated_at2025-11-13 13:32:21.207998+00
descriptionInsertable assertions into method chains
homepage
repositoryhttps://github.com/mezum/chain-assertions-rs
max_upload_size
id1930205
size47,685
Kitsunesaki Mezumona (mezum)

documentation

https://docs.rs/chain-assertions

README

chain-assertions

Repository Latest Version Documentations License MSRV Coverage CI

This crate provides the assertion that can be insert between method chains.

Usage

Install the crate into your Cargo.toml:

[dependencies]
chain-assertions = "0.1"

# Add `passthrough` to disable checking on debug builds.
# chain-assertions = { version = "0.1", features = ["disable"] }

# Set default-features to false in no-std environment:
# chain-assertions = { version = "0.1", default-features = false }

and then use it like following:

use chain_assertions::prelude::*;

let target = i32::from_str_radix("21", 10)
    .debug_assert_ok()
    .map(|v| v * 2);
assert_eq!(target, Ok(42));
use chain_assertions::prelude::*;

let target = i32::from_str_radix("foobar", 10)
    .debug_assert_ok()
    // ^-- panic occurred here if debug_assertion is enabled
    //     but if debug_assertion is disabled, no error happened.
    .map(|v| v * 2);
assert!(matches!(target, Err(_)), "Should be Err");

Motivation

This crate makes it easy to declare and validate intermediate assumptions in Result/Option method chains. It panics in debug builds for rapid feedback, and in release builds it minimizes runtime overhead and favors fail-safe behavior to avoid unexpected panics.

This is especially useful for applications (e.g., games) where avoiding mid-run panics is important.

License

Copyright (c) 2025 Kitsunesaki Mezumona

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.

Commit count: 0

cargo fmt