unwrap-safe 0.1.0

Safe unwrap replacements that log instead of panic — drop-in macros for Option and Result
Documentation
  • Coverage
  • 100%
    6 out of 6 items documented6 out of 6 items with examples
  • Size
  • Source code size: 13.82 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.3 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 22s Average build duration of successful builds.
  • all releases: 22s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • CortanaRepo/ted-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • CortanaRepo

Safe unwrap replacements that log instead of panic.

.unwrap() and .expect() are useful during development but cause production outages when invariants break. This crate provides drop-in macro replacements that degrade gracefully: return a default value and emit a structured log instead of panicking.

Quick Reference

Before After For
opt.unwrap() unwrap_or_warn!(opt, default, "ctx") Option<T>
res.unwrap() result_or_warn!(res, default, "ctx") Result<T, E>
s.parse::<T>().unwrap() parse_or_warn!(s, T, default, "ctx") String parsing
opt.expect("msg") expect_or_error!(opt, default, "msg") Option soft-expect
res.expect("msg") expect_result_or_error!(res, default, "msg") Result soft-expect

All macros include file!() and line!() in the log output.

Example

use unwrap_safe::{unwrap_or_warn, result_or_warn, parse_or_warn};

// Option: return default on None
let name: Option<String> = None;
let display = unwrap_or_warn!(name, String::from("anonymous"), "user display name");
assert_eq!(display, "anonymous");

// Result: return default on Err
let val: Result<i32, String> = Err("timeout".into());
let count = result_or_warn!(val, 0, "request count");
assert_eq!(count, 0);

// Parse: return default on bad input
let port = parse_or_warn!("not_a_port", u16, 8080, "PORT env var");
assert_eq!(port, 8080);