Debian Codegraph @

README.md

raw symbols SCIP
1 Educe
2 ====================
3
4 [![CI](https://github.com/magiclen/educe/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/educe/actions/workflows/ci.yml)
5
6 This crate offers procedural macros designed to facilitate the swift implementation of Rust's built-in traits.
7
8 ## Features
9
10 By default, every trait this crate supports will be enabled. You can disable all of them by turning off the default features and enable only the traits that you want to use by adding them to the `features` explicitly.
11
12 For example,
13
14 ```toml
15 [dependencies.educe]
16 version = "*"
17 features = ["Debug", "Clone", "Copy", "Hash", "Default"]
18 default-features = false
19 ```
20
21 ## Traits
22
23 #### Debug
24
25 Use `#[derive(Educe)]` and `#[educe(Debug)]` to implement the `Debug` trait for a struct, enum, or union. This allows you to modify the names of your types, variants, and fields. You can also choose to ignore specific fields or set a method to replace the `Debug` trait. Additionally, you have the option to format a struct as a tuple and vice versa.
26
27 ###### Basic Usage
28
29 ```rust
30 use educe::Educe;
31
32 #[derive(Educe)]
33 #[educe(Debug)]
34 struct Struct {
35 f1: u8
36 }
37
38 #[derive(Educe)]
39 #[educe(Debug)]
40 enum Enum {
41 V1,
42 V2 {
43 f1: u8,
44 },
45 V3(u8),
46 }
47 ```
48
49 ###### Change the Name of a Type, a Variant or a Field
50
51 The `name` parameter can rename a type, a variant or a field. If you set it to `false`, the name can be ignored or forced to show otherwise.
52
53 ```rust
54 use educe::Educe;
55
56 #[derive(Educe)]
57 #[educe(Debug(name(Struct2)))]
58 struct Struct {
59 #[educe(Debug(name(f)))]
60 f1: u8
61 }
62
63 #[derive(Educe)]
64 #[educe(Debug(name = true))]
65 enum Enum {
66 #[educe(Debug(name = false))]
67 V1,
68 #[educe(Debug(name(V)))]
69 V2 {
70 #[educe(Debug(name(f)))]
71 f1: u8,
72 },
73 #[educe(Debug(name = false))]
74 V3(u8),
75 }
76 ```
77
78 ###### Ignore Fields
79
80 The `ignore` parameter can ignore a specific field.
81
82 ```rust
83 use educe::Educe;
84
85 #[derive(Educe)]
86 #[educe(Debug)]
87 struct Struct {
88 #[educe(Debug(ignore))]
89 f1: u8
90 }
91
92 #[derive(Educe)]
93 #[educe(Debug)]
94 enum Enum {
95 V1,
96 V2 {
97 #[educe(Debug(ignore))]
98 f1: u8,
99 },
100 V3(
101 #[educe(Debug(ignore))]
102 u8
103 ),
104 }
105 ```
106
107 ###### Fake Structs and Tuples
108
109 With the `named_field` parameter, structs can be formatted as tuples and tuples can be formatted as structs.
110
111 ```rust
112 use educe::Educe;
113
114 #[derive(Educe)]
115 #[educe(Debug(named_field = false))]
116 struct Struct {
117 f1: u8
118 }
119
120 #[derive(Educe)]
121 #[educe(Debug)]
122 enum Enum {
123 V1,
124 #[educe(Debug(named_field = false))]
125 V2 {
126 f1: u8,
127 },
128 #[educe(Debug(named_field = true))]
129 V3(
130 u8,
131 #[educe(Debug(name(value)))]
132 i32
133 ),
134 }
135 ```
136
137 ###### Use Another Method to Handle the Formatting
138
139 The `method` parameter can be utilized to replace the implementation of the `Debug` trait for a field, eliminating the need to implement the `Debug` trait for the type of that field.
140
141 ```rust
142 use educe::Educe;
143
144 use std::fmt::{self, Formatter};
145
146 fn fmt<T>(_s: &T, f: &mut Formatter<'_>) -> fmt::Result {
147 f.write_str("Hi")
148 }
149
150 #[derive(Educe)]
151 #[educe(Debug)]
152 enum Enum<T> {
153 V1,
154 V2 {
155 #[educe(Debug(method(fmt)))]
156 f1: u8,
157 },
158 V3(
159 #[educe(Debug(method(std::fmt::UpperHex::fmt)))]
160 u8,
161 #[educe(Debug(method(fmt)))]
162 T
163 ),
164 }
165 ```
166
167 ###### Generic Parameters Bound to the `Debug` Trait or Others
168
169 Generic parameters will be automatically bound to the `Debug` trait if necessary.
170
171 ```rust
172 use educe::Educe;
173
174 #[derive(Educe)]
175 #[educe(Debug)]
176 enum Enum<T, K> {
177 V1,
178 V2 {
179 f1: K,
180 },
181 V3(
182 T
183 ),
184 }
185 ```
186
187 Or you can set the where predicates by yourself.
188
189 ```rust
190 use educe::Educe;
191
192 use std::fmt::{self, Formatter};
193
194 fn fmt<D>(_s: &D, f: &mut Formatter<'_>) -> fmt::Result {
195 f.write_str("Hi")
196 }
197
198 #[derive(Educe)]
199 #[educe(Debug(bound(T: std::fmt::Debug)))]
200 enum Enum<T, K> {
201 V1,
202 V2 {
203 #[educe(Debug(method(fmt)))]
204 f1: K,
205 },
206 V3(
207 T
208 ),
209 }
210 ```
211
212 In the above case, `T` is bound to the `Debug` trait, but `K` is not.
213
214 Or, you can have `educe` replicate the behaviour of `std`'s `derive`'s, where a bound is produced for *every* generic parameter, without regard to how it's used in the structure:
215
216 ```rust
217 use educe::Educe;
218
219 #[derive(Educe)]
220 #[educe(Debug(bound(*)))]
221 struct Struct<T> {
222 #[educe(Debug(ignore))]
223 f: T,
224 }
225 ```
226
227 This can be useful if you don't want to make the trait implementation part of your permanent public API. In this example, `Struct<T>` doesn't implement `Debug` unless `T` does. I.e., it has a `T: Debug` bound even though that's not needed right now. Later we might want to display `f`; we wouldn't then need to make a breaking API change by adding the bound.
228
229 This was the behaviour of `Trait(bound)` in educe 0.4.x and earlier.
230
231 ###### Union
232
233 A union will be formatted as a `u8` slice because we don't know its fields at runtime. The fields of a union cannot be ignored, renamed, or formatted with other methods. The implementation is **unsafe** because it may expose uninitialized memory.
234
235 ```rust
236 use educe::Educe;
237
238 #[derive(Educe)]
239 #[educe(Debug(unsafe))]
240 union Union {
241 f1: u8,
242 f2: i32,
243 }
244 ```
245
246 #### Clone
247
248 Use `#[derive(Educe)]` and `#[educe(Clone)]` to implement the `Clone` trait for a struct, an enum, or a union. You can set a method to replace the `Clone` trait.
249
250 ###### Basic Usage
251
252 ```rust
253 use educe::Educe;
254
255 #[derive(Educe)]
256 #[educe(Clone)]
257 struct Struct {
258 f1: u8
259 }
260
261 #[derive(Educe)]
262 #[educe(Clone)]
263 enum Enum {
264 V1,
265 V2 {
266 f1: u8,
267 },
268 V3(u8),
269 }
270 ```
271
272 ###### Use Another Method to Perform Cloning
273
274 The `method` parameter can be utilized to replace the implementation of the `Clone` trait for a field, eliminating the need to implement the `Clone` trait for the type of that field.
275
276 ```rust
277 use educe::Educe;
278
279 fn clone(v: &u8) -> u8 {
280 v + 100
281 }
282
283 trait A {
284 fn add(&self, rhs: u8) -> Self;
285 }
286
287 fn clone2<T: A>(v: &T) -> T {
288 v.add(100)
289 }
290
291 #[derive(Educe)]
292 #[educe(Clone)]
293 enum Enum<T: A> {
294 V1,
295 V2 {
296 #[educe(Clone(method(clone)))]
297 f1: u8,
298 },
299 V3(
300 #[educe(Clone(method(clone2)))]
301 T
302 ),
303 }
304 ```
305
306 ###### Generic Parameters Bound to the `Clone` Trait or Others
307
308 Generic parameters will be automatically bound to the `Clone` trait if necessary. If the `#[educe(Copy)]` attribute exists, they will be bound to the `Copy` trait.
309
310 ```rust
311 use educe::Educe;
312
313 #[derive(Educe)]
314 #[educe(Clone)]
315 enum Enum<T, K> {
316 V1,
317 V2 {
318 f1: K,
319 },
320 V3(
321 T
322 ),
323 }
324 ```
325
326 Or you can set the where predicates by yourself.
327
328 ```rust
329 use educe::Educe;
330
331 trait A {
332 fn add(&self, rhs: u8) -> Self;
333 }
334
335 fn clone<T: A>(v: &T) -> T {
336 v.add(100)
337 }
338
339 #[derive(Educe)]
340 #[educe(Clone(bound(T: std::clone::Clone)))]
341 enum Enum<T, K: A> {
342 V1,
343 V2 {
344 #[educe(Clone(method(clone)))]
345 f1: K,
346 },
347 V3(
348 T
349 ),
350 }
351 ```
352
353 In the above case, `T` is bound to the `Clone` trait, but `K` is not.
354
355 Or, you can have `educe` replicate the behaviour of `std`'s `derive`'s by using `bound(*)`. See the [`Debug`](#debug) section for more information.
356
357 ```rust
358 use educe::Educe;
359
360 trait A {
361 fn add(&self, rhs: u8) -> Self;
362 }
363
364 fn clone<T: A>(v: &T) -> T {
365 v.add(100)
366 }
367
368 #[derive(Educe)]
369 #[educe(Clone(bound(*)))]
370 struct Struct<T: A> {
371 #[educe(Clone(method(clone)))]
372 f: T,
373 }
374 ```
375
376 ###### Union
377
378 Refer to the introduction of the `#[educe(Copy)]` attribute.
379
380 #### Copy
381
382 Use `#[derive(Educe)]` and `#[educe(Copy)]` to implement the `Copy` trait for a struct, an enum, or a union.
383
384 ###### Basic Usage
385
386 ```rust
387 use educe::Educe;
388
389 #[derive(Educe)]
390 #[educe(Copy, Clone)]
391 struct Struct {
392 f1: u8
393 }
394
395 #[derive(Educe)]
396 #[educe(Copy, Clone)]
397 enum Enum {
398 V1,
399 V2 {
400 f1: u8,
401 },
402 V3(u8),
403 }
404 ```
405
406 ###### Generic Parameters Bound to the `Copy` Trait or Others
407
408 All generic parameters will be automatically bound to the `Copy` trait.
409
410 ```rust
411 use educe::Educe;
412
413 #[derive(Educe)]
414 #[educe(Copy, Clone)]
415 enum Enum<T, K> {
416 V1,
417 V2 {
418 f1: K,
419 },
420 V3(
421 T
422 ),
423 }
424 ```
425
426 Or you can set the where predicates by yourself.
427
428 ```rust
429 use educe::Educe;
430
431 trait A {
432 fn add(&self, rhs: u8) -> Self;
433 }
434
435 fn clone<T: A>(v: &T) -> T {
436 v.add(100)
437 }
438
439 #[derive(Educe)]
440 #[educe(Copy, Clone(bound(T: Copy, K: A + Copy)))]
441 enum Enum<T, K> {
442 V1,
443 V2 {
444 #[educe(Clone(method(clone)))]
445 f1: K,
446 },
447 V3(
448 T
449 ),
450 }
451 ```
452
453 Note that utilizing custom cloning methods for a type that implements the `Copy` and `Clone` traits may not be entirely appropriate.
454
455 ###### Union
456
457 The `#[educe(Copy, Clone)]` attribute can be used for a union. The fields of a union cannot be cloned with other methods.
458
459 ```rust
460 use educe::Educe;
461
462 #[derive(Educe)]
463 #[educe(Copy, Clone)]
464 union Union {
465 f1: u8,
466 }
467 ```
468
469 #### PartialEq
470
471 Use `#[derive(Educe)]` and `#[educe(PartialEq)]` to implement the `PartialEq` trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the `PartialEq` trait.
472
473 ###### Basic Usage
474
475 ```rust
476 use educe::Educe;
477
478 #[derive(Educe)]
479 #[educe(PartialEq)]
480 struct Struct {
481 f1: u8
482 }
483
484 #[derive(Educe)]
485 #[educe(PartialEq)]
486 enum Enum {
487 V1,
488 V2 {
489 f1: u8,
490 },
491 V3(u8),
492 }
493 ```
494
495 ###### Ignore Fields
496
497 The `ignore` parameter can ignore a specific field.
498
499 ```rust
500 use educe::Educe;
501
502 #[derive(Educe)]
503 #[educe(PartialEq)]
504 struct Struct {
505 #[educe(PartialEq(ignore))]
506 f1: u8
507 }
508
509 #[derive(Educe)]
510 #[educe(PartialEq)]
511 enum Enum {
512 V1,
513 V2 {
514 #[educe(PartialEq(ignore))]
515 f1: u8,
516 },
517 V3(
518 #[educe(PartialEq(ignore))]
519 u8
520 ),
521 }
522 ```
523
524 ###### Use Another Method to Perform Comparison
525
526 The `method` parameter can be utilized to replace the implementation of the `PartialEq` trait for a field, eliminating the need to implement the `PartialEq` trait for the type of that field.
527
528 ```rust
529 use educe::Educe;
530
531 fn eq(a: &u8, b: &u8) -> bool {
532 a + 1 == *b
533 }
534
535 trait A {
536 fn is_same(&self, other: &Self) -> bool;
537 }
538
539 fn eq2<T: A>(a: &T, b: &T) -> bool {
540 a.is_same(b)
541 }
542
543 #[derive(Educe)]
544 #[educe(PartialEq)]
545 enum Enum<T: A> {
546 V1,
547 V2 {
548 #[educe(PartialEq(method(eq)))]
549 f1: u8,
550 },
551 V3(
552 #[educe(PartialEq(method(eq2)))]
553 T
554 ),
555 }
556 ```
557
558 ###### Generic Parameters Bound to the `PartialEq` Trait or Others
559
560 Generic parameters will be automatically bound to the `PartialEq` trait if necessary.
561
562 ```rust
563 use educe::Educe;
564
565 #[derive(Educe)]
566 #[educe(PartialEq)]
567 enum Enum<T, K> {
568 V1,
569 V2 {
570 f1: K,
571 },
572 V3(
573 T
574 ),
575 }
576 ```
577
578 Or you can set the where predicates by yourself.
579
580 ```rust
581 use educe::Educe;
582
583 trait A {
584 fn is_same(&self, other: &Self) -> bool;
585 }
586
587 fn eq<T: A>(a: &T, b: &T) -> bool {
588 a.is_same(b)
589 }
590
591 #[derive(Educe)]
592 #[educe(PartialEq(bound(T: std::cmp::PartialEq, K: A)))]
593 enum Enum<T, K> {
594 V1,
595 V2 {
596 #[educe(PartialEq(method(eq)))]
597 f1: K,
598 },
599 V3(
600 T
601 ),
602 }
603 ```
604
605 In the above case, `T` is bound to the `PartialEq` trait, but `K` is not.
606
607 You can have `educe` replicate the behaviour of `std`'s `derive`'s by using `bound(*)`. See the [`Debug`](#debug) section for more information.
608
609 ```rust
610 use educe::Educe;
611
612 #[derive(Educe)]
613 #[educe(PartialEq(bound(*)))]
614 struct Struct<T> {
615 #[educe(PartialEq(ignore))]
616 f: T,
617 }
618 ```
619
620 ###### Union
621
622 The `#[educe(PartialEq(unsafe))]` attribute can be used for a union. The fields of a union cannot be compared with other methods. The implementation is **unsafe** because it disregards the specific fields it utilizes.
623
624 ```rust
625 use educe::Educe;
626
627 #[derive(Educe)]
628 #[educe(PartialEq(unsafe))]
629 union Union {
630 f1: u8,
631 f2: i32
632 }
633 ```
634
635 #### Eq
636
637 Use `#[derive(Educe)]` and `#[educe(Eq)]` to implement the `Eq` trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the `PartialEq` trait.
638
639 ###### Basic Usage
640
641 ```rust
642 use educe::Educe;
643
644 #[derive(Educe)]
645 #[educe(PartialEq, Eq)]
646 struct Struct {
647 f1: u8
648 }
649
650 #[derive(Educe)]
651 #[educe(PartialEq, Eq)]
652 enum Enum {
653 V1,
654 V2 {
655 f1: u8,
656 },
657 V3(u8),
658 }
659 ```
660
661 ###### Ignore Fields
662
663 The `ignore` parameter can ignore a specific field.
664
665 ```rust
666 use educe::Educe;
667
668 #[derive(Educe)]
669 #[educe(PartialEq, Eq)]
670 struct Struct {
671 #[educe(Eq(ignore))]
672 f1: u8
673 }
674
675 #[derive(Educe)]
676 #[educe(PartialEq, Eq)]
677 enum Enum {
678 V1,
679 V2 {
680 #[educe(Eq(ignore))]
681 f1: u8,
682 },
683 V3(
684 #[educe(Eq(ignore))]
685 u8
686 ),
687 }
688 ```
689
690 ###### Use Another Method to Perform Comparison
691
692 The `method` parameter can be utilized to replace the implementation of the `Eq` trait for a field, eliminating the need to implement the `PartialEq` trait for the type of that field.
693
694 ```rust
695 use educe::Educe;
696
697 fn eq(a: &u8, b: &u8) -> bool {
698 a + 1 == *b
699 }
700
701 trait A {
702 fn is_same(&self, other: &Self) -> bool;
703 }
704
705 fn eq2<T: A>(a: &T, b: &T) -> bool {
706 a.is_same(b)
707 }
708
709 #[derive(Educe)]
710 #[educe(PartialEq, Eq)]
711 enum Enum<T: A> {
712 V1,
713 V2 {
714 #[educe(Eq(method(eq)))]
715 f1: u8,
716 },
717 V3(
718 #[educe(Eq(method(eq2)))]
719 T
720 ),
721 }
722 ```
723
724 ###### Generic Parameters Bound to the `PartialEq` Trait or Others
725
726 Generic parameters will be automatically bound to the `PartialEq` trait if necessary.
727
728 ```rust
729 use educe::Educe;
730
731 #[derive(Educe)]
732 #[educe(PartialEq, Eq)]
733 enum Enum<T, K> {
734 V1,
735 V2 {
736 f1: K,
737 },
738 V3(
739 T
740 ),
741 }
742 ```
743
744 Or you can set the where predicates by yourself.
745
746 ```rust
747 use educe::Educe;
748
749 trait A {
750 fn is_same(&self, other: &Self) -> bool;
751 }
752
753 fn eq<T: A>(a: &T, b: &T) -> bool {
754 a.is_same(b)
755 }
756
757 #[derive(Educe)]
758 #[educe(PartialEq(bound(T: std::cmp::PartialEq, K: A)), Eq)]
759 enum Enum<T, K> {
760 V1,
761 V2 {
762 #[educe(Eq(method(eq)))]
763 f1: K,
764 },
765 V3(
766 T
767 ),
768 }
769 ```
770
771 ###### Union
772
773 The `#[educe(PartialEq(unsafe), Eq)]` attribute can be used for a union. The fields of a union cannot be compared with other methods. The implementation is **unsafe** because it disregards the specific fields it utilizes.
774
775 ```rust
776 use educe::Educe;
777
778 #[derive(Educe)]
779 #[educe(PartialEq(unsafe), Eq)]
780 union Union {
781 f1: u8,
782 f2: i32
783 }
784 ```
785
786 #### PartialOrd
787
788 Use `#[derive(Educe)]` and `#[educe(PartialOrd)]` to implement the `PartialOrd` trait for a struct or enum. You can also choose to ignore specific fields or set a method to replace the `PartialOrd` trait.
789
790 ###### Basic Usage
791
792 ```rust
793 use educe::Educe;
794
795 #[derive(PartialEq, Educe)]
796 #[educe(PartialOrd)]
797 struct Struct {
798 f1: u8
799 }
800
801 #[derive(PartialEq, Educe)]
802 #[educe(PartialOrd)]
803 enum Enum {
804 V1,
805 V2 {
806 f1: u8,
807 },
808 V3(u8),
809 }
810 ```
811
812 ###### Ignore Fields
813
814 The `ignore` parameter can ignore a specific field.
815
816 ```rust
817 use educe::Educe;
818
819 #[derive(PartialEq, Educe)]
820 #[educe(PartialOrd)]
821 struct Struct {
822 #[educe(PartialOrd(ignore))]
823 f1: u8
824 }
825
826 #[derive(PartialEq, Educe)]
827 #[educe(PartialOrd)]
828 enum Enum {
829 V1,
830 V2 {
831 #[educe(PartialOrd(ignore))]
832 f1: u8,
833 },
834 V3(
835 #[educe(PartialOrd(ignore))]
836 u8
837 ),
838 }
839 ```
840
841 ###### Use Another Method to Perform Comparison
842
843 The `method` parameter can be utilized to replace the implementation of the `PartialOrd` trait for a field, eliminating the need to implement the `PartialOrd` trait for the type of that field.
844
845 ```rust
846 use educe::Educe;
847
848 use std::cmp::Ordering;
849
850 fn partial_cmp(a: &u8, b: &u8) -> Option<Ordering> {
851 if a > b {
852 Some(Ordering::Less)
853 } else if a < b {
854 Some(Ordering::Greater)
855 } else {
856 Some(Ordering::Equal)
857 }
858 }
859
860 trait A {
861 fn value(&self) -> u8;
862 }
863
864 fn partial_cmp2<T: A>(a: &T, b: &T) -> Option<Ordering> {
865 partial_cmp(&a.value(), &b.value())
866 }
867
868 #[derive(Educe)]
869 #[educe(PartialEq, PartialOrd)]
870 enum Enum<T: A> {
871 V1,
872 V2 {
873 #[educe(PartialOrd(method(partial_cmp)))]
874 f1: u8,
875 },
876 V3(
877 #[educe(PartialOrd(method(partial_cmp2)))]
878 T
879 ),
880 }
881 ```
882
883 ###### Ranking
884
885 Each field can add a `#[educe(PartialOrd(rank = priority_value))]` attribute, where `priority_value` is an integer value indicating its comparison precedence (lower values indicate higher priority). The default `priority_value` for a field depends on its ordinal position (lower towards the front) and starts with `isize::MIN`.
886
887 ```rust
888 use educe::Educe;
889
890 #[derive(PartialEq, Educe)]
891 #[educe(PartialOrd)]
892 struct Struct {
893 #[educe(PartialOrd(rank = 1))]
894 f1: u8,
895 #[educe(PartialOrd(rank = 0))]
896 f2: u8,
897 }
898 ```
899
900 For variants, the discriminant can be explicitly set for comparison.
901
902 ```rust
903 use educe::Educe;
904
905 #[derive(PartialEq, Educe)]
906 #[educe(PartialOrd)]
907 #[repr(u8)]
908 enum Enum {
909 Three { f1: u8 } = 3,
910 Two(u8) = 2,
911 One = 1,
912 }
913 ```
914
915 ###### Generic Parameters Bound to the `PartialOrd` Trait or Others
916
917 Generic parameters will be automatically bound to the `PartialOrd` trait if necessary.
918
919 ```rust
920 use educe::Educe;
921
922 #[derive(PartialEq, Educe)]
923 #[educe(PartialOrd)]
924 enum Enum<T, K> {
925 V1,
926 V2 {
927 f1: K,
928 },
929 V3(
930 T
931 ),
932 }
933 ```
934
935 Or you can set the where predicates by yourself.
936
937 ```rust
938 use educe::Educe;
939
940 use std::cmp::Ordering;
941
942 trait A {
943 fn value(&self) -> u8;
944 }
945
946 fn partial_cmp<T: A>(a: &T, b: &T) -> Option<Ordering> {
947 a.value().partial_cmp(&b.value())
948 }
949
950 #[derive(PartialEq, Educe)]
951 #[educe(PartialOrd(bound(T: std::cmp::PartialOrd, K: PartialEq + A)))]
952 enum Enum<T, K> {
953 V1,
954 V2 {
955 #[educe(PartialOrd(method(partial_cmp)))]
956 f1: K,
957 },
958 V3(
959 T
960 ),
961 }
962 ```
963
964 In the above case, `T` is bound to the `PartialOrd` trait, but `K` is not.
965
966 You can have `educe` replicate the behaviour of `std`'s `derive`'s by using `bound(*)`. See the [`Debug`](#debug) section for more information.
967
968 ```rust
969 use educe::Educe;
970
971 #[derive(PartialEq, Educe)]
972 #[educe(PartialOrd(bound(*)))]
973 struct Struct<T> {
974 #[educe(PartialOrd(ignore))]
975 f: T,
976 }
977 ```
978
979 #### Ord
980
981 Use `#[derive(Educe)]` and `#[educe(Ord)]` to implement the `Ord` trait for a struct or enum. You can also choose to ignore specific fields or set a method to replace the `Ord` trait.
982
983 ###### Basic Usage
984
985 ```rust
986 use educe::Educe;
987
988 #[derive(PartialEq, Eq, Educe)]
989 #[educe(PartialOrd, Ord)]
990 struct Struct {
991 f1: u8
992 }
993
994 #[derive(PartialEq, Eq, Educe)]
995 #[educe(PartialOrd, Ord)]
996 enum Enum {
997 V1,
998 V2 {
999 f1: u8,
1000 },
1001 V3(u8),
1002 }
1003 ```
1004
1005 ###### Ignore Fields
1006
1007 The `ignore` parameter can ignore a specific field.
1008
1009 ```rust
1010 use educe::Educe;
1011
1012 #[derive(PartialEq, Eq, Educe)]
1013 #[educe(PartialOrd, Ord)]
1014 struct Struct {
1015 #[educe(Ord(ignore))]
1016 f1: u8
1017 }
1018
1019 #[derive(PartialEq, Eq, Educe)]
1020 #[educe(PartialOrd, Ord)]
1021 enum Enum {
1022 V1,
1023 V2 {
1024 #[educe(Ord(ignore))]
1025 f1: u8,
1026 },
1027 V3(
1028 #[educe(Ord(ignore))]
1029 u8
1030 ),
1031 }
1032 ```
1033
1034 ###### Use Another Method to Perform Comparison
1035
1036 The `method` parameter can be utilized to replace the implementation of the `Ord` trait for a field, eliminating the need to implement the `Ord` trait for the type of that field.
1037
1038 ```rust
1039 use educe::Educe;
1040
1041 use std::cmp::Ordering;
1042
1043 fn cmp(a: &u8, b: &u8) -> Ordering {
1044 if a > b {
1045 Ordering::Less
1046 } else if a < b {
1047 Ordering::Greater
1048 } else {
1049 Ordering::Equal
1050 }
1051 }
1052
1053 trait A {
1054 fn value(&self) -> u8;
1055 }
1056
1057 fn cmp2<T: A>(a: &T, b: &T) -> Ordering {
1058 cmp(&a.value(), &b.value())
1059 }
1060
1061 #[derive(Educe)]
1062 #[educe(PartialEq, Eq, PartialOrd, Ord)]
1063 enum Enum<T: A> {
1064 V1,
1065 V2 {
1066 #[educe(Ord(method(cmp)))]
1067 f1: u8,
1068 },
1069 V3(
1070 #[educe(Ord(method(cmp2)))]
1071 T
1072 ),
1073 }
1074 ```
1075
1076 ###### Ranking
1077
1078 Each field can add a `#[educe(Ord(rank = priority_value))]` attribute, where `priority_value` is an integer value indicating its comparison precedence (lower values indicate higher priority). The default `priority_value` for a field depends on its ordinal position (lower towards the front) and starts with `isize::MIN`.
1079
1080 ```rust
1081 use educe::Educe;
1082
1083 #[derive(PartialEq, Eq, Educe)]
1084 #[educe(PartialOrd, Ord)]
1085 struct Struct {
1086 #[educe(Ord(rank = 1))]
1087 f1: u8,
1088 #[educe(Ord(rank = 0))]
1089 f2: u8,
1090 }
1091 ```
1092
1093 For variants, the discriminant can be explicitly set for comparison.
1094
1095 ```rust
1096 use educe::Educe;
1097
1098 #[derive(PartialEq, Eq, Educe)]
1099 #[educe(PartialOrd, Ord)]
1100 #[repr(u8)]
1101 enum Enum {
1102 Three { f1: u8 } = 3,
1103 Two(u8) = 2,
1104 One = 1,
1105 }
1106 ```
1107
1108 ###### Generic Parameters Bound to the `Ord` Trait or Others
1109
1110 Generic parameters will be automatically bound to the `Ord` trait if necessary.
1111
1112 ```rust
1113 use educe::Educe;
1114
1115 #[derive(PartialEq, Eq, Educe)]
1116 #[educe(PartialOrd, Ord)]
1117 enum Enum<T, K> {
1118 V1,
1119 V2 {
1120 f1: K,
1121 },
1122 V3(
1123 T
1124 ),
1125 }
1126 ```
1127
1128 Or you can set the where predicates by yourself.
1129
1130 ```rust
1131 use educe::Educe;
1132
1133 use std::cmp::Ordering;
1134
1135 trait A {
1136 fn value(&self) -> u8;
1137 }
1138
1139 fn cmp<T: A>(a: &T, b: &T) -> Ordering {
1140 a.value().cmp(&b.value())
1141 }
1142
1143 #[derive(PartialEq, Eq, Educe)]
1144 #[educe(PartialOrd, Ord(bound(T: std::cmp::Ord, K: std::cmp::Ord + A)))]
1145 enum Enum<T, K> {
1146 V1,
1147 V2 {
1148 #[educe(PartialOrd(method(cmp)))]
1149 f1: K,
1150 },
1151 V3(
1152 T
1153 ),
1154 }
1155 ```
1156
1157 #### Hash
1158
1159 Use `#[derive(Educe)]` and `#[educe(Hash)]` to implement the `Hash` trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the `Hash` trait.
1160
1161 ###### Basic Usage
1162
1163 ```rust
1164 use educe::Educe;
1165
1166 #[derive(Educe)]
1167 #[educe(Hash)]
1168 struct Struct {
1169 f1: u8
1170 }
1171
1172 #[derive(Educe)]
1173 #[educe(Hash)]
1174 enum Enum {
1175 V1,
1176 V2 {
1177 f1: u8,
1178 },
1179 V3(u8),
1180 }
1181 ```
1182
1183 ###### Ignore Fields
1184
1185 The `ignore` parameter can ignore a specific field.
1186
1187 ```rust
1188 use educe::Educe;
1189
1190 #[derive(Educe)]
1191 #[educe(Hash)]
1192 struct Struct {
1193 #[educe(Hash(ignore))]
1194 f1: u8
1195 }
1196
1197 #[derive(Educe)]
1198 #[educe(Hash)]
1199 enum Enum {
1200 V1,
1201 V2 {
1202 #[educe(Hash(ignore))]
1203 f1: u8,
1204 },
1205 V3(
1206 #[educe(Hash(ignore))]
1207 u8
1208 ),
1209 }
1210 ```
1211
1212 ###### Use Another Method for Hashing
1213
1214 The `method` parameter can be utilized to replace the implementation of the `Hash` trait for a field, eliminating the need to implement the `Hash` trait for the type of that field.
1215
1216 ```rust
1217 use educe::Educe;
1218
1219 use std::hash::{Hash, Hasher};
1220
1221 fn hash<H: Hasher>(_s: &u8, state: &mut H) {
1222 Hash::hash(&100, state)
1223 }
1224
1225 fn hash2<H: Hasher, T>(_s: &T, state: &mut H) {
1226 Hash::hash(&100, state)
1227 }
1228
1229 #[derive(Educe)]
1230 #[educe(Hash)]
1231 enum Enum<T> {
1232 V1,
1233 V2 {
1234 #[educe(Hash(method(hash)))]
1235 f1: u8,
1236 },
1237 V3(
1238 #[educe(Hash(method(hash2)))]
1239 T
1240 ),
1241 }
1242 ```
1243
1244 ###### Generic Parameters Bound to the `Hash` Trait or Others
1245
1246 Generic parameters will be automatically bound to the `Hash` trait if necessary.
1247
1248 ```rust
1249 use educe::Educe;
1250
1251 #[derive(Educe)]
1252 #[educe(Hash)]
1253 enum Enum<T, K> {
1254 V1,
1255 V2 {
1256 f1: K,
1257 },
1258 V3(
1259 T
1260 ),
1261 }
1262 ```
1263
1264 Or you can set the where predicates by yourself.
1265
1266 ```rust
1267 use educe::Educe;
1268
1269 use std::hash::{Hash, Hasher};
1270
1271 trait A {
1272 fn value(&self) -> u8;
1273 }
1274
1275 fn hash<H: Hasher, T: A>(s: &T, state: &mut H) {
1276 Hash::hash(&s.value(), state)
1277 }
1278
1279 #[derive(Educe)]
1280 #[educe(Hash(bound(T: std::hash::Hash, K: A)))]
1281 enum Enum<T, K> {
1282 V1,
1283 V2 {
1284 #[educe(Hash(method(hash)))]
1285 f1: K,
1286 },
1287 V3(
1288 T
1289 ),
1290 }
1291 ```
1292
1293 In the above case, `T` is bound to the `Hash` trait, but `K` is not.
1294
1295 You can have `educe` replicate the behaviour of `std`'s `derive`'s by using `bound(*)`. See the [`Debug`](#debug) section for more information.
1296
1297 ```rust
1298 use educe::Educe;
1299
1300 #[derive(Educe)]
1301 #[educe(Hash(bound(*)))]
1302 struct Struct<T> {
1303 #[educe(Hash(ignore))]
1304 f: T,
1305 }
1306 ```
1307
1308 ###### Union
1309
1310 The `#[educe(PartialEq(unsafe), Eq, Hash(unsafe))]` attribute can be used for a union. The fields of a union cannot be hashed with other methods. The implementation is **unsafe** because it disregards the specific fields it utilizes.
1311
1312 ```rust
1313 use educe::Educe;
1314
1315 #[derive(Educe)]
1316 #[educe(PartialEq(unsafe), Eq, Hash(unsafe))]
1317 union Union {
1318 f1: u8,
1319 f2: i32
1320 }
1321 ```
1322
1323 #### Default
1324
1325 Use `#[derive(Educe)]` and `#[educe(Default)]` to implement the `Default` trait for a struct, enum, or union. You can also choose to ignore specific fields or set a method to replace the `Hash` trait.
1326
1327 ###### Basic Usage
1328
1329 For enums and unions, it is necessary to designate a default variant (for enums) and a default field (for unions) unless the enum has only one variant or the union has only one field.
1330
1331 ```rust
1332 use educe::Educe;
1333
1334 #[derive(Educe)]
1335 #[educe(Default)]
1336 struct Struct {
1337 f1: u8
1338 }
1339
1340 #[derive(Educe)]
1341 #[educe(Default)]
1342 enum Enum {
1343 V1,
1344 #[educe(Default)]
1345 V2 {
1346 f1: u8,
1347 },
1348 V3(u8),
1349 }
1350
1351 #[derive(Educe)]
1352 #[educe(Default)]
1353 union Union {
1354 f1: u8,
1355 #[educe(Default)]
1356 f2: f64,
1357 }
1358 ```
1359
1360 ###### The Default Value for the Entire Type
1361
1362 ```rust
1363 use educe::Educe;
1364
1365 #[derive(Educe)]
1366 #[educe(Default(expression = Struct { f1: 1 }))]
1367 struct Struct {
1368 f1: u8
1369 }
1370
1371 #[derive(Educe)]
1372 #[educe(Default(expression = Enum::Struct { f1: 1 }))]
1373 enum Enum {
1374 Unit,
1375 Struct {
1376 f1: u8
1377 },
1378 Tuple(u8),
1379 }
1380
1381 #[derive(Educe)]
1382 #[educe(Default(expression = Union { f1: 1 }))]
1383 union Union {
1384 f1: u8,
1385 f2: f64,
1386 }
1387 ```
1388
1389 You may need to activate the `full` feature to enable support for advanced expressions.
1390
1391 ###### The Default Values for Specific Fields
1392
1393 ```rust
1394 use educe::Educe;
1395
1396 #[derive(Educe)]
1397 #[educe(Default)]
1398 struct Struct {
1399 #[educe(Default = 1)]
1400 f1: u8,
1401 #[educe(Default = 11111111111111111111111111111)]
1402 f2: i128,
1403 #[educe(Default = 1.1)]
1404 f3: f64,
1405 #[educe(Default = true)]
1406 f4: bool,
1407 #[educe(Default = "Hi")]
1408 f5: &'static str,
1409 #[educe(Default = "Hello")]
1410 f6: String,
1411 #[educe(Default = 'M')]
1412 f7: char,
1413 }
1414
1415 #[derive(Educe)]
1416 #[educe(Default)]
1417 enum Enum {
1418 Unit,
1419 #[educe(Default)]
1420 Tuple(
1421 #[educe(Default(expression = 0 + 1))]
1422 u8,
1423 #[educe(Default(expression = -11111111111111111111111111111 * -1))]
1424 i128,
1425 #[educe(Default(expression = 1.0 + 0.1))]
1426 f64,
1427 #[educe(Default(expression = !false))]
1428 bool,
1429 #[educe(Default(expression = "Hi"))]
1430 &'static str,
1431 #[educe(Default(expression = String::from("Hello")))]
1432 String,
1433 #[educe(Default(expression = 'M'))]
1434 char,
1435 ),
1436 }
1437
1438 #[derive(Educe)]
1439 #[educe(Default)]
1440 union Union {
1441 f1: u8,
1442 f2: i128,
1443 f3: f64,
1444 f4: bool,
1445 #[educe(Default = "Hi")]
1446 f5: &'static str,
1447 f6: char,
1448 }
1449 ```
1450
1451 ###### Generic Parameters Bound to the `Default` Trait or Others
1452
1453 Generic parameters will be automatically bound to the `Default` trait if necessary.
1454
1455 ```rust
1456 use educe::Educe;
1457
1458 #[derive(Educe)]
1459 #[educe(Default)]
1460 enum Enum<T> {
1461 Unit,
1462 #[educe(Default)]
1463 Struct {
1464 f1: T
1465 },
1466 Tuple(T),
1467 }
1468 ```
1469
1470 Or you can set the where predicates by yourself.
1471
1472 ```rust
1473 use educe::Educe;
1474
1475 #[derive(Educe)]
1476 #[educe(Default(bound(T: std::default::Default)))]
1477 enum Enum<T> {
1478 Unit,
1479 #[educe(Default)]
1480 Struct {
1481 f1: T
1482 },
1483 Tuple(T),
1484 }
1485 ```
1486
1487 ###### The `new` Associated Function
1488
1489 With the `#[educe(Default(new))]` attribute, your type will include an additional associated function called `new`. This function can be utilized to invoke the `default` method of the `Default` trait.
1490
1491 ```rust
1492 use educe::Educe;
1493
1494 #[derive(Educe)]
1495 #[educe(Default(new))]
1496 struct Struct {
1497 f1: u8
1498 }
1499 ```
1500
1501 #### Deref
1502
1503 Use `#[derive(Educe)]` and `#[educe(Deref)]` to implement the `Deref` trait for a struct or enum.
1504
1505 ###### Basic Usage
1506
1507 You must designate a field as the default for obtaining an immutable reference unless the number of fields is exactly one.
1508
1509 ```rust
1510 use educe::Educe;
1511
1512 #[derive(Educe)]
1513 #[educe(Deref)]
1514 struct Struct {
1515 f1: u8,
1516 #[educe(Deref)]
1517 f2: u8,
1518 }
1519
1520 #[derive(Educe)]
1521 #[educe(Deref)]
1522 enum Enum {
1523 Struct {
1524 f1: u8
1525 },
1526 Struct2 {
1527 f1: u8,
1528 #[educe(Deref)]
1529 f2: u8,
1530 },
1531 Tuple(u8),
1532 Tuple2(
1533 u8,
1534 #[educe(Deref)]
1535 u8
1536 ),
1537 }
1538 ```
1539
1540 #### DerefMut
1541
1542 Use `#[derive(Educe)]` and `#[educe(DerefMut)]` to implement the `DerefMut` trait for a struct or enum.
1543
1544 ###### Basic Usage
1545
1546 You must designate a field as the default for obtaining an mutable reference unless the number of fields is exactly one.
1547
1548 ```rust
1549 use educe::Educe;
1550
1551 #[derive(Educe)]
1552 #[educe(Deref, DerefMut)]
1553 struct Struct {
1554 f1: u8,
1555 #[educe(Deref, DerefMut)]
1556 f2: u8,
1557 }
1558
1559 #[derive(Educe)]
1560 #[educe(Deref, DerefMut)]
1561 enum Enum {
1562 Struct {
1563 f1: u8
1564 },
1565 Struct2 {
1566 f1: u8,
1567 #[educe(Deref, DerefMut)]
1568 f2: u8,
1569 },
1570 Tuple(u8),
1571 Tuple2(
1572 #[educe(DerefMut)]
1573 u8,
1574 #[educe(Deref)]
1575 u8
1576 ),
1577 }
1578 ```
1579
1580 The mutable dereferencing fields do not need to be the same as the immutable dereferencing fields, but their types must be consistent.
1581
1582 #### Into
1583
1584 Use `#[derive(Educe)]` and `#[educe(Into(type))]` to implement the `Into<type>` trait for a struct or enum.
1585
1586 ###### Basic Usage
1587
1588 You need to designate a field as the default for `Into<type>` conversion unless the number of fields is exactly one. If you don't, educe will automatically try to find a proper one.
1589
1590 ```rust
1591 use educe::Educe;
1592
1593 #[derive(Educe)]
1594 #[educe(Into(u8), Into(u16))]
1595 struct Struct {
1596 f1: u8,
1597 f2: u16,
1598 }
1599
1600 #[derive(Educe)]
1601 #[educe(Into(u8))]
1602 enum Enum {
1603 V1 {
1604 f1: u8,
1605 #[educe(Into(u8))]
1606 f2: u8,
1607 },
1608 V2 (
1609 u8
1610 ),
1611 }
1612 ```
1613
1614 ###### Use Another Method to Perform Into Conversion
1615
1616 The `method` parameter can be utilized to replace the implementation of the `Into` trait for a field, eliminating the need to implement the `Into` trait for the type of that field.
1617
1618 ```rust
1619 use educe::Educe;
1620
1621 fn into(v: u16) -> u8 {
1622 v as u8
1623 }
1624
1625 #[derive(Educe)]
1626 #[educe(Into(u8))]
1627 enum Enum {
1628 V1 {
1629 #[educe(Into(u8, method(into)))]
1630 f1: u16,
1631 },
1632 V2 (
1633 u8
1634 ),
1635 }
1636 ```
1637
1638 ###### Generic Parameters Bound to the `Into` Trait or Others
1639
1640 Generic parameters will be automatically bound to the `Into<type>` trait if necessary.
1641
1642 ```rust
1643 use educe::Educe;
1644
1645 #[derive(Educe)]
1646 #[educe(Into(u8))]
1647 enum Enum<T, K> {
1648 V1 {
1649 f1: K,
1650 },
1651 V2 (
1652 T
1653 ),
1654 }
1655 ```
1656
1657 Or you can set the where predicates by yourself.
1658
1659 ```rust
1660 use educe::Educe;
1661
1662 fn into<T>(_v: T) -> u8 {
1663 0
1664 }
1665
1666 #[derive(Educe)]
1667 #[educe(Into(u8, bound(K: Into<u8>)))]
1668 enum Enum<T, K> {
1669 V1 {
1670 f1: K,
1671 },
1672 V2 (
1673 #[educe(Into(u8, method(into)))]
1674 T
1675 ),
1676 }
1677 ```
1678
1679 ## Crates.io
1680
1681 https://crates.io/crates/educe
1682
1683 ## Documentation
1684
1685 https://docs.rs/educe
1686
1687 ## License
1688
1689 [MIT](LICENSE)