Expand description
§Impl From Macro
The implementation of trait From.
See more macros: docs, repository.
§Examples:
§Struct
#[derive(From, Debug, PartialEq)]
#[from(&str, expr = value.to_string().into())]
pub struct Email(String);
assert_eq!(
Email::from("user@example.com".to_string()),
"user@example.com".into()
);§Enum
#[derive(From, Debug, PartialEq)]
pub enum Value {
Integer(i32),
#[from(skip)]
Integer2(i32),
Float(f64),
#[from((), expr = Self::Null)]
Null,
#[from(&str, with = String::from)]
String(String),
}
assert_eq!(Value::Integer(42), 42.into());
assert_eq!(Value::Float(3.14), 3.14.into());
assert_eq!(Value::Null, ().into());
assert_eq!(Value::String("test".to_string()), "test".into());§License & Feedback:
This library distributed under the MIT license.
You can contact me via GitHub or send a message to my E-Mail. This library is actively evolving, and your suggestions and feedback are always welcome!
Derive Macros§
- From
- The implementation of std::convert::From trait