Crates.io | derive-from-ext |
lib.rs | derive-from-ext |
version | 0.2.0 |
source | src |
created_at | 2022-04-03 18:55:45.632962 |
updated_at | 2022-04-14 17:30:29.704578 |
description | Derive macro implementing 'From' for structs |
homepage | |
repository | https://github.com/andrewlowndes/derive-from |
max_upload_size | |
id | 561422 |
size | 18,554 |
A derive macro that auto implements 'std::convert::From' for structs. The default behaviour is to create an instance of the structure by calling .into() on each of the source structure properties. The source struct properties can be mapped using different names and methods by using field attributes to override the default behaviour.
Include in your Cargo.toml file:
[dependencies]
derive-from-ext = "0.2"
use derive_from_ext::From;
struct A {
prop1: String,
}
#[derive(From, Debug)]
#[from(A)]
struct B {
prop1: String,
}
let a = A { prop1: "Test".to_string() };
let b: B = a.into();
dbg!(b); //automatically converted into type B and can use implementations on this type
If a source structure has few properties than the current structure then the property can be skipped by tagging with 'skip':
#[from(A)]
struct B [
#[from(skip)]
other_prop: String,
]
Note: this only works where a default can be assigned to the property value and is equivalent to setting the skip method to 'std::default::Default::default' as below.
#[from(A)]
struct B [
#[from(skip, default="String::from(\"New value\")")]
other_prop: String,
]
To use an alternative method to create the value for the structure property, a property attribute can be used with the path of the met:
fn lowercase(str: String) -> String {
str.to_lowercase()
}
#[from(A)]
struct B {
#[from(map="lowercase")]
other_prop: String,
}
To map from a different property on the source structure, a property attribute can be used:
#[from(A)]
struct B {
#[from(rename="prop1")]
other_prop: String,
}
To support multiple source structures, the 'from' attribute can be extended to include the other structures. The attributes can then also override source-specific options for each source structure:
#[from(A, B)]
struct C {
#[from(overrides=( A=(skip=true), B=(map="lowercase") ))]
other_prop: String,
}
If you do not require the features above and only want to convert a struct into matching struct you may be better off using derive_more instead.