Impl-tools
A set of helper macros
Macros
Autoimpl
#[autoimpl]
is a partial replacement for #[derive]
, supporting:
- Explicit
where
clause on generic parameters - No implicit bounds on generic parameters beyond those required by the type
- Traits like
Deref
byusing
a named field - Traits like
Debug
mayignore
named fields
#[autoimpl]
may also be used on trait definitions to impl for specified types
supporting Deref
.
Unlike alternatives, #[autoimpl]
has minimal and intuitive syntax.
use autoimpl;
use Debug;
// Generates: impl<'a, T: Animal + ?Sized> Animal for Box<T> { .. }
// Generates: impl<T, A: Animal> std::fmt::Debug for Named<A> where T: Debug { .. }
// Generates: impl<T, A: Animal> std::ops::Deref for Named<A> { .. }
// Generates: impl<T, A: Animal> std::ops::DerefMut for Named<A> { .. }
Impl Default
#[impl_default]
implements std::default::Default
:
use ;
impl_scope!
Impl Scope
impl_scope!
is a function-like macro used to define a type plus its
implementations. It supports impl Self
syntax:
use impl_scope;
use Display;
impl_scope!
Caveat: rustfmt
won't currently touch the contents. Hopefully that
can be fixed!
Extensibility
Rust's #[derive]
macro is extensible via #[proc_macro_derive]
in a proc-macro
crate. Our macros cannot be extended in the same way, but they can be extended via a new front-end:
- Create a copy of the
impl-tools
crate to create a new "front-end" (proc-macro
crate). This crate is contains only a little code over theimpl-tools-lib
crate. - To extend
#[autoimpl]
, write an impl ofImplTrait
and add it to the attribute's definition. To extendimpl_scope!
, write an impl ofScopeAttr
and add it to the macro's definition. - Depend on your new front end crate instead of
impl-tools
.
Supported Rust Versions
The MSRV is 1.56.0 for no particular reason other than that it is the first to support Edition 2021.
Alternatives
Both Educe and Derivative
have similar functionality: the ability to implement various traits with more flexibility than
libstd's #[derive]
. They also support more functionality such as tweaking the output of Debug
.
Both have less clean syntax, requiring a minimum of two attributes to do anything, with further
attributes to customise implementations (e.g. to ignore a field).
derive_more isn't exactly an "alternative", simply
supporting #[derive]
for more standard traits. Possible functionality overlap in the future
(though for now #[autoimpl]
doesn't support half the traits supported by #[derive]
).
auto_impl allows implementing a trait for reference types
(&
, &mut
, Box
, Rc
, Arc
) as well as function types.
Copyright and Licence
The COPYRIGHT file includes a list of contributors who claim copyright on this project. This list may be incomplete; new contributors may optionally add themselves to this list.
The impl-tools library is published under the terms of the Apache License, Version 2.0. You may obtain a copy of this licence from the LICENSE file or on the following webpage: https://www.apache.org/licenses/LICENSE-2.0