[−][src]Derive Macro amplify_derive::From
#[derive(From)] { // Attributes available to this derive: #[from] }
Implements From
trait for the whole entity and/or its separate fields.
Works well with #[derive(Error)]
and, in many cases may require
Default
implementation (for details, pls see Examples below)
Examples
#[derive(From, Default)] #[from(::std::io::Error)] // Structure may contain no parameters pub struct IoErrorUnit; #[derive(From, Default)] #[from(::std::io::Error)] // When no explicit binding is given, structure must implement `Default` pub struct IoError { details: String, #[from] kind: IoErrorUnit, } #[derive(From)] pub enum Error { // You can specify multiple conversions with separate attributes #[from(::std::io::Error)] #[from(IoError)] Io, #[from] Format(::std::fmt::Error), #[from] WithFields { details: ::std::str::Utf8Error }, MultipleFields { // ...and you can also covert error type #[from(IoErrorUnit)] // rest of parameters must implement `Default` io: IoError, details: String, }, } #[derive(Clone, Copy, PartialEq, Eq, Hash, Default, Debug, From)] pub struct Wrapper(u32, i16);
If you use rust nightly and #![feature(never_type)]
for [!
], you can
even do the following:
ⓘThis example is not tested
#![feature(never_type)] #[macro_use] extern crate amplify_derive; #[derive(From)] pub enum Error { // ... other error types #[from(!)] NeverType, }