13 releases
Uses new Rust 2024
| 0.5.1 | Sep 2, 2025 |
|---|---|
| 0.5.0 | Dec 29, 2023 |
| 0.4.6 | Feb 23, 2023 |
| 0.4.5 | Aug 7, 2022 |
| 0.2.1 | Jan 4, 2020 |
#203 in Rust patterns
21,293 downloads per month
Used in 10 crates
(via rustpython-vm)
13KB
99 lines
OptionLike and ResultLike
Install: https://crates.io/crates/result-like
Define your own Option-like and Result-like enum types. Avoid reimplementing the entire APIs of option and result for your own enums.
Option example
use result_like::OptionLike;
// Simple case with single argument name to use Some and None
#[derive(OptionLike)]
enum MyOption<T> {
Some(T),
None,
}
let v = MyOption::Some(1);
// every option utilities are possible including unwrap, map, and, or etc.
assert_eq!(v.unwrap(), 1);
// convertable to option
let opt = v.into_option();
assert_eq!(opt, Some(1));
// enum with custom names instead of Some and None
#[derive(OptionLike)]
enum Number {
Value(i64),
Nan,
}
let v = Number::Value(10);
assert_ne!(v, Number::Nan);
Result example in same way
use result_like::ResultLike;
// typical
#[derive(ResultLike)]
enum MyResult<T, E> {
Ok(T),
Err(E),
}
// value-only
#[derive(ResultLike)]
enum Trial {
Success(String),
Failure(String),
}
Dependencies
~190–610KB
~15K SLoC