Expand description
§more-convert: more convert utilities
This crate provides utilities for convert
§Usage
more-convert provides a derive macro
EnumReprautomatically implementsTryFromandIntofor enums- Ideal for managing Type, etc.
- Example: test code
Convertautomatically implementsFromorIntofor named structs- Leave the very cumbersome From and Into implementations to us!
- Example From: from’s test code
- Example Into: into’s test code
§Example
§EnumRepr
use more_convert::EnumRepr;
#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[repr(u16)]
pub enum Test {
First,
Three = 3,
Four,
}
assert_eq!(0u16, Test::First.into());
assert_eq!(3u16, Test::Three.into());
assert_eq!(0u16.try_into(), Ok(Test::First));
assert_eq!(3u16.try_into(), Ok(Test::Three));§Convert
use more_convert::Convert;
#[derive(Convert)]
#[convert(from(B))]
pub struct A {
pub a: u16,
pub b: u16,
}
pub struct B {
pub a: u8,
pub b: u8,
}
let b = B {
a: 1u8,
b: 2u8,
};
let a: A = b.into();
assert_eq!(a.a, 1u16);
assert_eq!(a.b, 2u16);more Into examples are here
more From examples are here
§License
Licensed under