Skip to content

Instantly share code, notes, and snippets.

@Eveeifyeve
Last active September 6, 2024 12:15
Show Gist options
  • Save Eveeifyeve/ddc992ee08da8d82477d7cfd62df7db1 to your computer and use it in GitHub Desktop.
Save Eveeifyeve/ddc992ee08da8d82477d7cfd62df7db1 to your computer and use it in GitHub Desktop.
Rust impl_trait Optional
trait Optional<T>: Into<Option<T>> {}
impl<T> Optional<T> for T where T: Into<Option<T>> {}
impl<T> Optional<T> for Option<T> {}
fn test<T: Optional<bool>>(test_type: T) -> bool {
let test_type = match test_type.into() {
Some(test) => test,
None => false,
};
test_type
}
pub fn main() {
println!("{}", test(Some(true)));
println!("{}", test(None));
println!("{}", test(false));
println!("{}", test(true));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment