degeneric-macros
Quick Start
use ;
use PhantomData;
use trait_set;
use TypedBuilder;
trait_set!
Elevator pitch
The problem
Degeneric is a utility library that solves the common problem of having too many generics. Let's say we want to construct a dependency container like this:
let container = Container ;
accepts_container;
// now to consume such a container, one needs to write the function like this:
This creates a problem of ever growing list of generics in all functions that touch the container and pollutes APIs with unnecessary generics.
Degeneric solution
Degeneric proposes solution to this problem by creating a trait and stuffing all of the generic types into the trait as associated types. Instead of the pattern above, you'll end up with this:
use Degeneric;
let c = Container ;
accepts_container;
How is this different, you ask? Instead of accepting a whole lot of generic arguments, I can now write
the function without even using angular brackets and I think that's beautiful.
What is even more beautiful is that you can add more generics without having to modify the
signature of accepts_container.
Degeneric understands lifetimes
use Cow;
use Debug;
use ;
use TypedBuilder;
let cow = Owned;
Degeneric can be used with galemu!
If you're into hiding generics, you'll be surprised that the galemu crate makes it possible to hide even lifetimes!
The way this example works is, that your Container contains an impl GCon. This object is able
to produce [galemu::Bound]<GCon::Transaction>.
The particular implementation of GTran is provided by [galemu::create_gal_wrapper_type].
One must manually implement GTran on it.
In principle, galemu lifts the lifetime of Transaction<'a> into the [galemu::BoundExt] trait.
The lifetime inference happens in Connection::transaction. At that point, it's apparent that
the connection's lifetime is passed to Transaction.
use Debug;
use Cow;
use Deref;
use Degeneric;
use ;
// begin galemu
create_gal_wrapper_type!
// end galemu
let conn = Connection ;
let cont = Container ;
commit_transaction;
Degeneric + dynamize
Degeneric supports dynamizing the generated trait. How does that work?
Here's a minimal example on how to dynamize the generated trait:
use Degeneric;
By convention, dynamize generates a DynGeneratedContainerTrait where the types are boxed.
Please refer to dynamize documentation
for more information.
Degeneric + haz
Degeneric is able to serve as a derive macro for the excellent
haz crate.
use Degeneric;
use Has;
assert_has_all_the_things;
Degeneric understands where clause
use ;
use Debug;
let c = Container ;
construct_default_value;
Generate getters only for some fields
The no_getter attribute can be used to skip generating a getter.
use degeneric_macros::{Degeneric};
#[derive(Degeneric)]
#[degeneric(trait_decl = "pub(crate) trait Something")]
struct Container<'a, T: 'a, S: 'a> {
item: &'a T,
item2: S,
#[degeneric(no_getter)]
dt: PhantomData<S>,
}
let c = Container {
item: "hello",
item2: format!("this won't have getter!"),
dt: PhantomData<S>,
};
fn accept_container<C: Something>(c: C) {
/// ERROR: dt doesn't have a getter!
assert_eq!(c.dt(), format!("this won't have getter!"));
}
accept_container(c);
Degeneric figures out mutability
Some fields may have mutable getters, some not. Degeneric recognizes immutable pointers and references and skips generating mutable getter for them.
use ;
let mut c = Container ;
accept_container;
use degeneric_macros::{Degeneric};
#[derive(Degeneric)]
#[degeneric(trait_decl = "pub(crate) trait Something")]
struct Container<'a, T> {
x: &'a T,
}
let c = Container {
x: &(),
};
fn accept_container<'a>(c: impl Something<'a>) {
// ERROR: x is a reference which can't be made mut
c.x_mut();
}
Add attributes everywhere!
For some attributes, you can just add them on the field and they'll be forwarded to all getters automatically. Here's a list of such attributes:
#[allow]#[doc]#[cfg(...)]#[cfg_attr(...)]
If you need more granularity, you can add attributes only on:
- Trait declaration:
#[degeneric(trait_decl_attr = "#[doc = \"Trait declaration\"]")] - Trait impl block:
#[degeneric(trait_impl_attr = "#[doc = \"Trait implementation\"]")] - Field immutable getter implementation:
#[degeneric(getter_impl_attr = "#[doc = \"Getter implementation\"])] - Field mutable getter declaration:
#[degeneric(mut_getter_decl_attr = "#[doc = \"Mutable Getter declaration\"])]
use degeneric_macros::Degeneric;
#[derive(Degeneric)]
#[degeneric(trait_decl = "pub(crate) trait Something")]
#[degeneric(trait_decl_impl_attr = "#[cfg(foo)]")]
/// This is documentation for the `Something` trait
struct Container<T> {
x: T,
}
// this will error because the Something trait exists only in the foo configuration
#[cfg(not(foo))]
fn accept_container(c: impl Something) {}
Crates degeneric plays nice with
- galemu - hide lifetimes!
- trait-set - shorten and DRY up trait bounds
- typed-builder - generate a builder for your trait
- easy-ext - extend your trait with more methods
CloneExt
Apart from solving the dependency injection problem, degeneric also helps with cloning. There might be a situation where you're holding a non-cloneable type inside another type. In these situations, it might be possible to clone the value by different means.
Failing example:
#[derive(Default)]
struct NonClone;
#[derive(Clone)]
struct Container {
nc: PhantomData<NonClone>,
}
In such situations, one can resort to degeneric's CloneExt derive macro. Currently, it offers a single attribute to adjust the way fields are cloned:
;
default.clone;
License: MIT