#conversion #string #trivial #type #derived #enums #struct

namewise

Derived trivial name-wise conversions for Rust types

19 stable releases

2.6.8 Feb 11, 2023
2.6.2 Feb 10, 2023
2.5.0 Aug 16, 2022
1.2.1 Aug 6, 2022
0.1.1 May 20, 2022

#1091 in Rust patterns

Download history 175/week @ 2024-07-27 11/week @ 2024-09-21

165 downloads per month
Used in kafkaesque

MIT license

7KB
59 lines

Derive trivial transformations between fields that are mostly unpacking and converting similarly-nemd fields from a source into a target. Example usage:

use namewise;
use std::collections::HashSet;

struct Source {
    a: &'static str,
    text: String,
    numeric: i16,
    truth: bool,
    truths: Vec<bool>,
}

#[derive(namewise::From)]
#[namewise_from(from_type = "Source")]
struct Destination {
    a: String,
    text: String,
    #[namewise_from(from_name = "numeric")]
    number: i64,
    #[namewise_from(collect)]
    truths: HashSet<bool>,
}

This should be equivalent to:

use std::collections::HashSet;

struct Source {
    a: &'static str,
    text: String,
    numeric: i16,
    truth: bool,
    truths: Vec<bool>,
}

struct Destination {
    a: String,
    text: String,
    number: i64,
    truths: HashSet<bool>,
}

impl From<Source> for Destination {
    fn from(value: Source) -> Destination {
        Destination {
            a: value.a.into(),
            text: value.text.into(),
            number: value.numeric.into(),
            truths: value.truths.into_iter().collect(),
        }
    }
}

Dependencies

~1–1.5MB
~32K SLoC