#derive #proc-macro #generic #bounds #depend #replace #drop-in

macro iderive

Drop-in replacement for derive that doesn't directly depend on generic bounds

3 releases (stable)

1.1.0 Apr 3, 2024
1.0.0 Jun 17, 2022
0.1.0 Jun 28, 2021

#1160 in Rust patterns

Download history 13/week @ 2024-02-23 9/week @ 2024-03-01 126/week @ 2024-03-29 40/week @ 2024-04-05 1/week @ 2024-04-12

167 downloads per month

BSL-1.0 license

14KB
232 lines

iderive: Inner Derive

iderive is a drop-in replacement for derive that doesn't directly depend on generic bounds. It only checks the types of a struct's fields when deriving a trait.

Example

#[derive(Clone, Copy)]
struct TaggedIndex<T: ?Sized> {
    index: usize,
    _tag: PhantomData<T>,
}

let a = TaggedIndex::<String> { index: 0, _tag: PhantomData };
let b = a;
let c = a; // Error: Value used after move

This won't work because derive requires that T implements Copy for TaggedIndex to be able to derive it.

In contrast, iderive only checks the struct's fields to determine if a trait can be derived. Because usize and PhantomData<T> implements Copy regardless of the type of T, iderive(Copy) will implement Copy for TaggedIndex:

#[iderive(Clone, Copy)]
struct TaggedIndex<T: ?Sized> {
    index: usize,
    _tag: PhantomData<T>,
}

let a = TaggedIndex::<String> { index: 0, _tag: PhantomData };
let b = a;
let c = a; // Works!

Supported traits

iderive is currently implemented for Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord and Hash.

Dependencies

~0.4–0.8MB
~20K SLoC