Expand description

This crate is a declarative macro library to help you implement the From or TryFrom trait for your type.

Implementing From trait

The from macro helps you to implement From trait.

from!((src: SourceType) -> TargetType {
    /* ... */
});

The above code results in the following code.

impl From<SourceType> for TargetType {
    fn from(src: SourceType) -> Self {
        /* ... */
    }
}

You can also use the as struct or as enum keywords to convert between struct types or enum types. See the details at from.

Implementing TryFrom trait

The try_from macro helps you to implement TryFrom trait.

try_from!((src: SourceType) -> <TargetType, ErrorType> {
    /* ... */
});

The above code results in the following code.

impl TryFrom<SourceType> for TargetType {
    type Error = ErrorType;
    fn try_from(src: SourceType) -> Result<Self, Self::Error> {
        /* ... */
    }
}

You can also use the as struct or as enum keywords to convert between struct types or enum types. See the details at try_from.

Macros

  • Helper to impl From<$src_type> for $dst_type.
  • Helper to impl TryFrom<$src_type> for $dst_type.