]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #56402 - scottmcm:better-marker-trait-example, r=Centril
authorkennytm <kennytm@gmail.com>
Mon, 3 Dec 2018 10:07:10 +0000 (18:07 +0800)
committerGitHub <noreply@github.com>
Mon, 3 Dec 2018 10:07:10 +0000 (18:07 +0800)
Improve the unstable book example for #[marker] trait

The previous one didn't actually use the Display&Debug bounds in any way, so I think this one is a bit more meaningful.

src/doc/unstable-book/src/language-features/marker-trait-attr.md

index 9dd7b6fae9bc61edd5c6ddd85d7a77d465904cce..dedc7d3015d4d65c88c7568d8c6be850dac291aa 100644 (file)
@@ -17,15 +17,17 @@ when they'd need to do the same thing for every type anyway).
 ```rust
 #![feature(marker_trait_attr)]
 
-use std::fmt::{Debug, Display};
+#[marker] trait CheapToClone: Clone {}
 
-#[marker] trait MyMarker {}
+impl<T: Copy> CheapToClone for T {}
 
-impl<T: Debug> MyMarker for T {}
-impl<T: Display> MyMarker for T {}
+// These could potentally overlap with the blanket implementation above,
+// so are only allowed because CheapToClone is a marker trait.
+impl<T: CheapToClone, U: CheapToClone> CheapToClone for (T, U) {}
+impl<T: CheapToClone> CheapToClone for std::ops::Range<T> {}
 
-fn foo<T: MyMarker>(t: T) -> T {
-    t
+fn cheap_clone<T: CheapToClone>(t: T) -> T {
+    t.clone()
 }
 ```