4 releases (breaking)
new 0.4.0 | Jan 30, 2025 |
---|---|
0.3.0 | Jan 28, 2025 |
0.2.0 | Jan 26, 2025 |
0.1.0 | Jan 25, 2025 |
#2778 in Development tools
256 downloads per month
Used in more-convert
25KB
462 lines
more-convert: more convert utilities
This crate provides utilities for convert
Usage
more-convert
provides a derive macro
EnumRepr
automatically implementsTryFrom
andInto
for enums- Ideal for managing Type, etc.
- Example: test code
Convert
automatically implementsFrom
orInto
for 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
- enum_attributes
- serde: automatically implements
serde::Serialize
andserde::Deserialize
- serde: automatically implements
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
- field_attributes
- ignore: skip the field
- rename: rename the field
- map
- map: map of expr
- map_field: map of field
- map_struct: map of struct
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
Dependencies
~220–660KB
~16K SLoC