From 7735f59e8084a69361224da2b1bc7337214c2c85 Mon Sep 17 00:00:00 2001 From: tinaun Date: Tue, 10 Oct 2017 04:20:49 -0400 Subject: [PATCH 1/4] unstable book: OIBIT --- .../language-features/optin-builtin-traits.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/doc/unstable-book/src/language-features/optin-builtin-traits.md diff --git a/src/doc/unstable-book/src/language-features/optin-builtin-traits.md b/src/doc/unstable-book/src/language-features/optin-builtin-traits.md new file mode 100644 index 0000000000000..9baa7eb35328d --- /dev/null +++ b/src/doc/unstable-book/src/language-features/optin-builtin-traits.md @@ -0,0 +1,49 @@ +# `optin_builtin_traits` + +The tracking issue for this feature is [#13231] + +[#13231]: https://github.com/rust-lang/rust/issues/13231 + +---- + +The `optin_builtin_traits` feature gate allows you to define _auto traits_. + +Auto traits, like [`Send`] or [`Sync`] in the standard library, are marker traits +that are automatically implemented for every type, unless the type, or a type it contains, +has explictly opted out via a _negative impl_. + +[`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html +[`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html + +```rust +impl !Type for Trait +``` + +Example: + +```rust + #![feature(optin_builtin_traits)] + + trait Valid {} + + impl Valid for .. {} + + struct True; + struct False; + + impl !Valid for False {} + + struct MaybeValid(T); + + fn must_be_valid(_t: T) { + + } + + fn main() { + //works + must_be_valid( MaybeValid(True) ); + + // compiler error - trait bound not satisfied + // must_be_valid( MaybeValid(False) ); + } +``` From 364148dbf9d7a3e4a8e0c766ae3395f63fcb5a01 Mon Sep 17 00:00:00 2001 From: tinaun Date: Tue, 10 Oct 2017 04:42:59 -0400 Subject: [PATCH 2/4] unstable book: unboxed_closures --- .../src/language-features/unboxed-closures.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/doc/unstable-book/src/language-features/unboxed-closures.md diff --git a/src/doc/unstable-book/src/language-features/unboxed-closures.md b/src/doc/unstable-book/src/language-features/unboxed-closures.md new file mode 100644 index 0000000000000..5146fcd873984 --- /dev/null +++ b/src/doc/unstable-book/src/language-features/unboxed-closures.md @@ -0,0 +1,25 @@ +# `unboxed_closures` + +The tracking issue for this feature is [#29625] + +See Also: [`fn_traits`](library-features/fn-traits.md) + +[#29625]: https://github.com/rust-lang/rust/issues/29625 + +---- + +The `unboxed_closures` feature allows you to write functions using the `"rust-call"` ABI, +required for implmenting the [`Fn*`] family of traits. `"rust-call"` functions must have +exactly one (non self) argument, a tuple representing the argument list. + +[`Fn*`]: https://doc.rust-lang.org/std/ops/trait.Fn.html + +```rust +#![feature(unboxed_closures)] + +extern "rust-call" fn add_args(args: (u32, u32)) { + args.0 + args.1 +} + +fn main() {} +``` From d07825258e2d5860442616ede39eb11ca3f8c9b6 Mon Sep 17 00:00:00 2001 From: tinaun Date: Tue, 10 Oct 2017 04:51:39 -0400 Subject: [PATCH 3/4] unstable book: fn_traits --- .../language-features/optin-builtin-traits.md | 2 +- .../src/library-features/fn-traits.md | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/doc/unstable-book/src/library-features/fn-traits.md diff --git a/src/doc/unstable-book/src/language-features/optin-builtin-traits.md b/src/doc/unstable-book/src/language-features/optin-builtin-traits.md index 9baa7eb35328d..4ff46dedba1bb 100644 --- a/src/doc/unstable-book/src/language-features/optin-builtin-traits.md +++ b/src/doc/unstable-book/src/language-features/optin-builtin-traits.md @@ -15,7 +15,7 @@ has explictly opted out via a _negative impl_. [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html -```rust +```rust, ignore impl !Type for Trait ``` diff --git a/src/doc/unstable-book/src/library-features/fn-traits.md b/src/doc/unstable-book/src/library-features/fn-traits.md new file mode 100644 index 0000000000000..d0f1fe8dc7c15 --- /dev/null +++ b/src/doc/unstable-book/src/library-features/fn-traits.md @@ -0,0 +1,35 @@ +# `fn_traits` + +The tracking issue for this feature is [#29625] + +See Also: [`unboxed_closures`](language-features/unboxed_closures.md) + +[#29625]: https://github.com/rust-lang/rust/issues/29625 + +---- + +The `fn_traits` feature allows for implementation of the [`Fn*`] traits +for creating custom closure-like types. + +[`Fn*`]: https://doc.rust-lang.org/std/ops/trait.Fn.html + +```rust +#![feature(unboxed_closures)] +#![feature(fn_traits)] + +struct Adder { + a: u32 +} + +impl FnOnce<(u32, )> for Adder { + type Output = u32; + extern "rust-call" fn call_once(self, b: (u32, )) -> Self::Output { + self.a + b.0 + } +} + +fn main() { + let adder = Adder { a: 3 }; + assert_eq!(adder(2), 5); +} +``` From d5ef9f9036743c77cbc30286a3b3b8e8185330d1 Mon Sep 17 00:00:00 2001 From: tinaun Date: Tue, 10 Oct 2017 05:03:47 -0400 Subject: [PATCH 4/4] formatting fixes --- .../language-features/optin-builtin-traits.md | 36 +++++++++---------- .../src/language-features/unboxed-closures.md | 4 +-- .../src/library-features/fn-traits.md | 2 +- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/optin-builtin-traits.md b/src/doc/unstable-book/src/language-features/optin-builtin-traits.md index 4ff46dedba1bb..ee24dd87d90c3 100644 --- a/src/doc/unstable-book/src/language-features/optin-builtin-traits.md +++ b/src/doc/unstable-book/src/language-features/optin-builtin-traits.md @@ -6,44 +6,42 @@ The tracking issue for this feature is [#13231] ---- -The `optin_builtin_traits` feature gate allows you to define _auto traits_. +The `optin_builtin_traits` feature gate allows you to define auto traits. Auto traits, like [`Send`] or [`Sync`] in the standard library, are marker traits that are automatically implemented for every type, unless the type, or a type it contains, -has explictly opted out via a _negative impl_. +has explictly opted out via a negative impl. [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html -```rust, ignore +```rust,ignore impl !Type for Trait ``` Example: ```rust - #![feature(optin_builtin_traits)] +#![feature(optin_builtin_traits)] - trait Valid {} +trait Valid {} - impl Valid for .. {} +impl Valid for .. {} - struct True; - struct False; +struct True; +struct False; - impl !Valid for False {} +impl !Valid for False {} - struct MaybeValid(T); +struct MaybeValid(T); - fn must_be_valid(_t: T) { +fn must_be_valid(_t: T) { } - } - - fn main() { - //works - must_be_valid( MaybeValid(True) ); +fn main() { + // works + must_be_valid( MaybeValid(True) ); - // compiler error - trait bound not satisfied - // must_be_valid( MaybeValid(False) ); - } + // compiler error - trait bound not satisfied + // must_be_valid( MaybeValid(False) ); +} ``` diff --git a/src/doc/unstable-book/src/language-features/unboxed-closures.md b/src/doc/unstable-book/src/language-features/unboxed-closures.md index 5146fcd873984..0eaed7a1989c5 100644 --- a/src/doc/unstable-book/src/language-features/unboxed-closures.md +++ b/src/doc/unstable-book/src/language-features/unboxed-closures.md @@ -2,7 +2,7 @@ The tracking issue for this feature is [#29625] -See Also: [`fn_traits`](library-features/fn-traits.md) +See Also: [`fn_traits`](library-features/fn-traits.html) [#29625]: https://github.com/rust-lang/rust/issues/29625 @@ -17,7 +17,7 @@ exactly one (non self) argument, a tuple representing the argument list. ```rust #![feature(unboxed_closures)] -extern "rust-call" fn add_args(args: (u32, u32)) { +extern "rust-call" fn add_args(args: (u32, u32)) -> u32 { args.0 + args.1 } diff --git a/src/doc/unstable-book/src/library-features/fn-traits.md b/src/doc/unstable-book/src/library-features/fn-traits.md index d0f1fe8dc7c15..72a3f36c10b69 100644 --- a/src/doc/unstable-book/src/library-features/fn-traits.md +++ b/src/doc/unstable-book/src/library-features/fn-traits.md @@ -2,7 +2,7 @@ The tracking issue for this feature is [#29625] -See Also: [`unboxed_closures`](language-features/unboxed_closures.md) +See Also: [`unboxed_closures`](language-features/unboxed-closures.html) [#29625]: https://github.com/rust-lang/rust/issues/29625