Skip to content

Commit bc6c024

Browse files
authored
Merge pull request #2415 from shepmaster/spelling
Impl trait spelling and code fixes
2 parents 244f953 + 71fb550 commit bc6c024

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

text/1522-conservative-impl-trait.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ With the placeholder syntax used in discussions so far,
2121
abstract return types would be used roughly like this:
2222

2323
```rust
24-
fn foo(n: u32) -> impl Iterator<Item=u32> {
24+
fn foo(n: u32) -> impl Iterator<Item = u32> {
2525
(0..n).map(|x| x * 100)
2626
}
2727
// ^ behaves as if it had return type Map<Range<u32>, Closure>
@@ -30,7 +30,6 @@ fn foo(n: u32) -> impl Iterator<Item=u32> {
3030
for x in foo(10) {
3131
// x = 0, 100, 200, ...
3232
}
33-
3433
```
3534

3635
# Background
@@ -61,10 +60,10 @@ its motivation and some other parts of its text below.
6160
6261
In today's Rust, you can write a function signature like
6362

64-
````rust
65-
fn consume_iter_static<I: Iterator<u8>>(iter: I)
66-
fn consume_iter_dynamic(iter: Box<Iterator<u8>>)
67-
````
63+
```rust
64+
fn consume_iter_static<I: Iterator<Item = u8>>(iter: I)
65+
fn consume_iter_dynamic(iter: Box<Iterator<Item = u8>>)
66+
```
6867

6968
In both cases, the function does not depend on the exact type of the argument.
7069
The type is held "abstract", and is assumed only to satisfy a trait bound.
@@ -78,15 +77,15 @@ The type is held "abstract", and is assumed only to satisfy a trait bound.
7877

7978
On the other hand, while you can write
8079

81-
````rust
82-
fn produce_iter_dynamic() -> Box<Iterator<u8>>
83-
````
80+
```rust
81+
fn produce_iter_dynamic() -> Box<Iterator<Item = u8>>
82+
```
8483

8584
you _cannot_ write something like
8685

87-
````rust
88-
fn produce_iter_static() -> Iterator<u8>
89-
````
86+
```rust
87+
fn produce_iter_static() -> Iterator<Item = u8>
88+
```
9089

9190
That is, in today's Rust, abstract return types can only be written using trait
9291
objects, which can be a significant performance penalty. This RFC proposes
@@ -98,22 +97,22 @@ Here are some problems that unboxed abstract types solve or mitigate:
9897

9998
* _Returning unboxed closures_. Closure syntax generates an anonymous type
10099
implementing a closure trait. Without unboxed abstract types, there is no way
101-
to use this syntax while returning the resulting closure unboxed, because there
100+
to use this syntax while returning the resulting closure unboxed because there
102101
is no way to write the name of the generated type.
103102

104103
* _Leaky APIs_. Functions can easily leak implementation details in their return
105104
type, when the API should really only promise a trait bound. For example, a
106105
function returning `Rev<Splits<'a, u8>>` is revealing exactly how the iterator
107106
is constructed, when the function should only promise that it returns _some_
108-
type implementing `Iterator<u8>`. Using newtypes/structs with private fields
107+
type implementing `Iterator<Item = u8>`. Using newtypes/structs with private fields
109108
helps, but is extra work. Unboxed abstract types make it as easy to promise only
110109
a trait bound as it is to return a concrete type.
111110

112111
* _Complex types_. Use of iterators in particular can lead to huge types:
113112

114-
````rust
115-
Chain<Map<'a, (int, u8), u16, Enumerate<Filter<'a, u8, vec::MoveItems<u8>>>>, SkipWhile<'a, u16, Map<'a, &u16, u16, slice::Items<u16>>>>
116-
````
113+
```rust
114+
Chain<Map<'a, (i32, u8), u16, Enumerate<Filter<'a, u8, vec::MoveItems<u8>>>>, SkipWhile<'a, u16, Map<'a, &u16, u16, slice::Items<u16>>>>
115+
```
117116

118117
Even when using newtypes to hide the details, the type still has to be written
119118
out, which can be very painful. Unboxed abstract types only require writing the
@@ -142,7 +141,7 @@ with other extensions.
142141
## Syntax
143142

144143
Let's start with the bikeshed: The proposed syntax is `impl Trait` in return type
145-
position, composing like trait objects to forms like `impl Foo+Send+'a`.
144+
position, composing like trait objects to forms like `impl Foo + Send + 'a`.
146145

147146
It can be explained as "a type that implements `Trait`",
148147
and has been used in that form in most earlier discussions and proposals.
@@ -163,7 +162,7 @@ The core semantics of the feature is described below.
163162

164163
Note that the sections after this one go into more detail on some of the design
165164
decisions, and that **it is likely for many of the mentioned limitations to be
166-
lifted at some point in the future**. For clarity, we'll separately categories the *core
165+
lifted at some point in the future**. For clarity, we'll separately categorize the *core
167166
semantics* of the feature (aspects that would stay unchanged with future extensions)
168167
and the *initial limitations* (which are likely to be lifted later).
169168

@@ -182,7 +181,7 @@ and the *initial limitations* (which are likely to be lifted later).
182181
- The type would not be known to implement any other trait, with
183182
the exception of OIBITS (aka "auto traits") and default traits like `Sized`.
184183
- The type would not be considered equal to the actual underlying type.
185-
- The type would not be allowed to appear as the Self type for an `impl` block.
184+
- The type would not be allowed to appear as the `Self` type for an `impl` block.
186185

187186
- Because OIBITS like `Send` and `Sync` will leak through an abstract return
188187
type, there will be some additional complexity in the compiler due to some
@@ -224,7 +223,7 @@ and the *initial limitations* (which are likely to be lifted later).
224223
unless these are themselves part of a legal return type.
225224

226225
- Eventually, we will want to allow the feature to be used within traits, and
227-
like in argument position as well (as an ergonomic improvement over today's generics).
226+
likely in argument position as well (as an ergonomic improvement over today's generics).
228227
- Using `impl Trait` multiple times in the same return type would be valid,
229228
like for example in `-> (impl Foo, impl Bar)`.
230229

@@ -253,7 +252,7 @@ and the *initial limitations* (which are likely to be lifted later).
253252

254253
## Rationale
255254

256-
### Why this semantics for the return type?
255+
### Why these semantics for the return type?
257256

258257
There has been a lot of discussion about what the semantics of the return type
259258
should be, with the theoretical extremes being "full return type inference" and
@@ -411,11 +410,11 @@ so the syntax is forbidden there.
411410

412411
### Compatibility with conditional trait bounds
413412

414-
On valid critique for the existing `impl Trait` proposal is that it does not
415-
cover more complex scenarios, where the return type would implement
413+
One valid critique for the existing `impl Trait` proposal is that it does not
414+
cover more complex scenarios where the return type would implement
416415
one or more traits depending on whether a type parameter does so with another.
417416

418-
For example, a iterator adapter might want to implement `Iterator` and
417+
For example, an iterator adapter might want to implement `Iterator` and
419418
`DoubleEndedIterator`, depending on whether the adapted one does:
420419

421420
```rust
@@ -427,7 +426,7 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for SkipOne<I> { ... }
427426

428427
Using just `-> impl Iterator`, this would not be possible to reproduce.
429428

430-
Since there has been no proposals so far that would address this in a way
429+
Since there have been no proposals so far that would address this in a way
431430
that would conflict with the fixed-trait-set case, this RFC punts on that issue as well.
432431

433432
### Limitation to free/inherent functions
@@ -436,10 +435,10 @@ One important usecase of abstract return types is to use them in trait methods.
436435

437436
However, there is an issue with this, namely that in combinations with generic
438437
trait methods, they are effectively equivalent to higher kinded types.
439-
Which is an issue because Rust HKT story is not yet figured out, so
438+
Which is an issue because Rust's HKT story is not yet figured out, so
440439
any "accidental implementation" might cause unintended fallout.
441440

442-
HKT allows you to be generic over a type constructor, aka a
441+
HKT allows you to be generic over a type constructor, a.k.a. a
443442
"thing with type parameters", and then instantiate them at some later point to
444443
get the actual type.
445444
For example, given a HK type `T` that takes one type as parameter, you could
@@ -460,7 +459,7 @@ with a `u32` or `bool`,
460459
just like `T<u32>` and `T<bool>` might give us `Vec<u32>` or `Box<bool>`
461460
in the example above.
462461

463-
The problem does not exists with trait method return types today because
462+
The problem does not exist with trait method return types today because
464463
they are concrete:
465464

466465
```rust
@@ -473,7 +472,7 @@ Given the above code, there is no way for `bar` to choose a return type `X`
473472
that could fundamentally differ between instantiations of `Self`
474473
while still being instantiable with an arbitrary `U`.
475474

476-
At most you could return a associated type, but then you'd loose the generics
475+
At most you could return a associated type, but then you'd lose the generics
477476
from `bar`
478477

479478
```rust
@@ -483,7 +482,7 @@ trait Foo {
483482
}
484483
```
485484

486-
So, in conclusion, since Rusts HKT story is not yet fleshed out,
485+
So, in conclusion, since Rust's HKT story is not yet fleshed out,
487486
and the compatibility of the current compiler with it is unknown,
488487
it is not yet possible to reach a concrete solution here.
489488

0 commit comments

Comments
 (0)