Wrap and unwrap your types the stylish way with derive macros for From/TryFrom in both directions
Giftwrap
exposes two derive macros, Wrap
and Unwrap
that derive impl From<inner_type> for your_type
and impl From<your_type> for inner_type
(or TryFrom<your_type>
in the case of enums) respectively.
It works for any struct or enum variant that holds only a single type, and don't worry variants with multiple types or with types you want to convert yourself can be easily ignored with #[noWrap]
and #[noUnwrap]
.
Consider the following error.rs
```rust
pub type Result
macrorules! implfrom { ($wrapper:expr, $inner:ty) => { impl From<$inner> for Error { fn from(e: $inner) -> Error { $wrapper(e) } } }; }
pub enum Error { Io(std::io::Error), RppalGpio(rppal::gpio::Error), Reqwest(reqwest::Error), Qr(qrcodegen::DataTooLong), Other(String), }
implfrom!(Error::Io, std::io::Error); implfrom!(Error::RppalGpio, rppal::gpio::Error); implfrom!(Error::Reqwest, reqwest::Error); implfrom!(Error::Qr, qrcodegen::DataTooLong); ``` This might seem simple enough but adding new error types is not as easy as it could be.
However with Giftwrap
it's as simple as it gets:
```rust
pub type Result
use giftwrap::Wrap;
pub enum Error {
Io(std::io::Error),
RppalGpio(rppal::gpio::Error),
Reqwest(reqwest::Error),
Qr(qrcodegen::DataTooLong),
#[noWrap]
Other(String),
}
``
Now you could add a new error variant wrapping a type from any library and
Giftwrap` handles the rest for you
Giftwrap
does not yet support:
- [ ] Generics / Lifetimes