1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Converting the `dyn-hash` README example to `#[dyn_shim]`.
//!
//! The original program:
//!
//! ```ignore
//! use dyn_hash::DynHash;
//!
//! trait MyTrait: DynHash {
//! /* ... */
//! }
//!
//! // Implement std::hash::Hash for dyn MyTrait
//! dyn_hash::hash_trait_object!(MyTrait);
//!
//! // Now data structures containing Box<dyn MyTrait> can derive Hash:
//! #[derive(Hash)]
//! struct Container {
//! trait_object: Box<dyn MyTrait>,
//! }
//! ```
//!
//! The conversion, applied below:
//!
//! 1. `use dyn_hash::DynHash;` becomes `use dyn_shim::dyn_shim;`.
//! 2. The `: DynHash` supertrait moves into the attribute: a plain
//! `trait MyTrait` annotated with `#[dyn_shim(DynMyTrait: Hash)]`.
//! 3. The `dyn_hash::hash_trait_object!(MyTrait);` call is dropped with no
//! replacement; the attribute generates the `Hash` impls for the shim's
//! `dyn` types.
//! 4. Trait objects are written `dyn DynMyTrait` instead of `dyn MyTrait`.
//!
//! Trait impls are untouched.
//!
//! Step 2 loosens a requirement the original had: with `: DynHash`, every
//! implementor of `MyTrait` was forced to be `Hash`. The attribute bound
//! requires `Hash` only of types used through the shim, so a non-`Hash` type
//! may now implement `MyTrait`; it simply never becomes a `DynMyTrait` and
//! cannot enter a `Box<dyn DynMyTrait>`. To keep the original contract, also
//! write the supertrait yourself: `trait MyTrait: Hash`. That composes with
//! the attribute, and gives generic code over `T: MyTrait` back its
//! `.hash(...)`.
//!
//! Run with: `cargo run --example migrate_dyn_hash`
use dyn_shim;
use ;
// Was: trait MyTrait: DynHash {
// Was: dyn_hash::hash_trait_object!(MyTrait); (dropped, no replacement)
// Data structures containing Box<dyn DynMyTrait> can derive Hash:
// Added to make the example runnable; the README stops at the definitions.