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

#2518 in Procedural macros

Download history 256/week @ 2025-01-22

256 downloads per month
Used in 2 crates (via more-convert-derive)

MIT license

21KB
448 lines

more-convert: more convert utilities

more-convert on crates.io more-convert on docs.rs

This crate provides utilities for convert

Usage

more-convert provides a derive macro

  • EnumRepr automatically implements TryFrom and Into for enums
    • Ideal for managing Type, etc.
    • Example: test code
  • Convert automatically implements From or Into for named structs

Example

EnumRepr

  • enum_attributes
    • serde: automatically implements serde::Serialize and serde::Deserialize
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