Skip to content
/ one-of Public

Rust library that introduces macros to represent and use types that can be converted either From or TryInto the given types

License

Notifications You must be signed in to change notification settings

figsoda/one-of

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Rust library that introduces macros to represent and use types that can be converted either From or TryInto the given types

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Contributors 2

  •  
  •  

Languages