one-of

Crates.ioone-of
lib.rsone-of
version0.2.3
sourcesrc
created_at2020-11-22 03:36:50.041245
updated_at2021-03-04 03:44:31.572309
descriptionMacro to represent a type that can be converted either `From` or `TryInto` the given types
homepagehttps://github.com/figsoda/one-of
repositoryhttps://github.com/figsoda/one-of
max_upload_size
id314904
size37,821
(figsoda)

documentation

https://docs.rs/one-of

README

one-of

version license ci

Macro to represent a type that can be converted either From or TryInto the given types

This crate only works on the nightly version of Rust

Documentation

Usage

use one_of::{case, one_of};

// either `u32` or `char`
let x: one_of!(u32, char) = 42.into();
assert_eq!(Some(42u32), x.into());
assert_eq!(Option::<char>::None, x.into());

// some type of integer
let x: one_of!(i8, i16, i32, i64, u8, u16, u32, u64) = 42.into();
assert_eq!(Option::<i8>::None, x.into());
assert_eq!(Option::<i16>::None, x.into());
assert_eq!(Some(42i32), x.into());
assert_eq!(Option::<i64>::None, x.into());
assert_eq!(Option::<u8>::None, x.into());
assert_eq!(Option::<u16>::None, x.into());
assert_eq!(Option::<u32>::None, x.into());
assert_eq!(Option::<u64>::None, x.into());

// case macro is the `match` keyword for `one_of` types
case!(<one_of!(bool, &str, i64)>::from("Hello, world!"),
    // bool
    _ => {
        panic!("not bool");
    };

    // &str
    s if s.starts_with("Hello, ") => {
        assert_eq!(&s[7 ..], "world!");
    }
    _ => {
        panic!("not other strings");
    };

    // i64
    _ => {
        panic!("not i64");
    };
);

Changelog

See CHANGELOG.md

Commit count: 53

cargo fmt