Last active
May 31, 2024 12:16
-
-
Save Rudxain/bd3439996593a59b1f8eaf3f73933771 to your computer and use it in GitHub Desktop.
fns that should be in Rust `core` library (wait, they are! https://doc.rust-lang.org/std/option/enum.Option.html#method.filter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
/// if `cond` is `true`, then `identity(x)` | |
/// | |
/// if `cond` is `false` then `drop(x)` | |
pub fn cond_ident<T>(cond: bool, x: T) -> Option<T> { | |
if cond { | |
Some(x) | |
} else { | |
None | |
} | |
} | |
/// if `pred` returns `true`, then `identity(x)` | |
/// | |
/// if `pred` returns `false` then `drop(x)` | |
pub fn pred_ident<F: FnOnce(&T) -> bool, T>(pred: F, x: T) -> Option<T> { | |
if pred(&x) { | |
Some(x) | |
} else { | |
None | |
} | |
} // should it be `impl FnOnce(&T) -> bool`? | |
// API would be "less flexible", | |
// but `Option::is_some_and` doesn't seem to care |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment