]> git.proxmox.com Git - rustc.git/blob - src/doc/unstable-book/src/language-features/marker-trait-attr.md
New upstream version 1.43.0+dfsg1
[rustc.git] / src / doc / unstable-book / src / language-features / marker-trait-attr.md
1 # `marker_trait_attr`
2
3 The tracking issue for this feature is: [#29864]
4
5 [#29864]: https://github.com/rust-lang/rust/issues/29864
6
7 ------------------------
8
9 Normally, Rust keeps you from adding trait implementations that could
10 overlap with each other, as it would be ambiguous which to use. This
11 feature, however, carves out an exception to that rule: a trait can
12 opt-in to having overlapping implementations, at the cost that those
13 implementations are not allowed to override anything (and thus the
14 trait itself cannot have any associated items, as they're pointless
15 when they'd need to do the same thing for every type anyway).
16
17 ```rust
18 #![feature(marker_trait_attr)]
19
20 #[marker] trait CheapToClone: Clone {}
21
22 impl<T: Copy> CheapToClone for T {}
23
24 // These could potentially overlap with the blanket implementation above,
25 // so are only allowed because CheapToClone is a marker trait.
26 impl<T: CheapToClone, U: CheapToClone> CheapToClone for (T, U) {}
27 impl<T: CheapToClone> CheapToClone for std::ops::Range<T> {}
28
29 fn cheap_clone<T: CheapToClone>(t: T) -> T {
30 t.clone()
31 }
32 ```
33
34 This is expected to replace the unstable `overlapping_marker_traits`
35 feature, which applied to all empty traits (without needing an opt-in).