Skip to content

Commit 7735f59

Browse files
committed
unstable book: OIBIT
1 parent 692b94a commit 7735f59

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# `optin_builtin_traits`
2+
3+
The tracking issue for this feature is [#13231]
4+
5+
[#13231]: https://github.com/rust-lang/rust/issues/13231
6+
7+
----
8+
9+
The `optin_builtin_traits` feature gate allows you to define _auto traits_.
10+
11+
Auto traits, like [`Send`] or [`Sync`] in the standard library, are marker traits
12+
that are automatically implemented for every type, unless the type, or a type it contains,
13+
has explictly opted out via a _negative impl_.
14+
15+
[`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html
16+
[`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html
17+
18+
```rust
19+
impl !Type for Trait
20+
```
21+
22+
Example:
23+
24+
```rust
25+
#![feature(optin_builtin_traits)]
26+
27+
trait Valid {}
28+
29+
impl Valid for .. {}
30+
31+
struct True;
32+
struct False;
33+
34+
impl !Valid for False {}
35+
36+
struct MaybeValid<T>(T);
37+
38+
fn must_be_valid<T: Valid>(_t: T) {
39+
40+
}
41+
42+
fn main() {
43+
//works
44+
must_be_valid( MaybeValid(True) );
45+
46+
// compiler error - trait bound not satisfied
47+
// must_be_valid( MaybeValid(False) );
48+
}
49+
```

0 commit comments

Comments
 (0)