@@ -188,7 +188,7 @@ method to actually perform the TLS handshake. The first argument is the domain
188
188
name we think we're connecting to, with the I/O object as the second.
189
189
190
190
[ `ClientContext::new` ] : http://alexcrichton.com/futures-rs/futures_tls/struct.ClientContext.html#method.new
191
- [ handshake ] : http://alexcrichton.com/futures-rs/futures_tls/struct.ClientContext.html#method.handshake
191
+ [ ` handshake` ] : http://alexcrichton.com/futures-rs/futures_tls/struct.ClientContext.html#method.handshake
192
192
193
193
Like with the [ ` tcp_connect ` ] from before, however, the [ ` handshake ` ] method
194
194
returns a future. The actual TLS handshake may take some time as the client and
@@ -341,12 +341,12 @@ bounds can be found in the [FAQ][faq-why-send].
341
341
342
342
[ Back to ` Future ` ] [ future-trait ]
343
343
344
- ```
344
+ ``` rust
345
345
type Item : Send + 'static ;
346
346
type Error : Send + 'static ;
347
347
```
348
348
349
- The next property of the [ ` Future ` ] trait you'll probably notice are the two
349
+ The next property of the [ ` Future ` ] trait you'll probably notice is the two
350
350
associated types it contains. These represent the types of values that the
351
351
` Future ` can resolve to. Each instance of ` Future ` can be thought of as
352
352
resolving to a ` Result<Self::Item, Self::Error> ` .
@@ -380,7 +380,7 @@ fn foo<F>(future: F)
380
380
381
381
[ Back to ` Future ` ] [ future-trait ]
382
382
383
- ```
383
+ ``` rust
384
384
fn poll (& mut self , task : & mut Task ) -> Poll <Self :: Item , Self :: Error >;
385
385
fn schedule (& mut self , task : & mut Task );
386
386
```
@@ -435,11 +435,11 @@ panic, some may never resolve again, etc. This means that implementors of the
435
435
[ ` Future ` ] trait don't need to maintain state to check if [ ` poll ` ] has already
436
436
returned successfully.
437
437
438
- If a call to [ ` poll ` ] return ` Poll::NotReady ` , then futures still need to know
438
+ If a call to [ ` poll ` ] returns ` Poll::NotReady ` , then futures still need to know
439
439
how to figure out when to get poll'd later! This is where the [ ` schedule ` ]
440
- method comes into the picture. Like with [ ` poll ` ] this method takes ` &mut self ` ,
440
+ method comes into the picture. Like with [ ` poll ` ] , this method takes ` &mut self ` ,
441
441
giving us the same guarantees as before. Similarly, it is passed a [ ` Task ` ] , but
442
- the relationship between [ ` schedule ` ] and [ ` Task ` ] is somewhat different that
442
+ the relationship between [ ` schedule ` ] and [ ` Task ` ] is somewhat different than
443
443
that ` poll ` has.
444
444
445
445
Each [ ` Task ` ] can have a [ ` TaskHandle ` ] extracted from it via the
@@ -472,9 +472,9 @@ you want to convert it to a future of `u32`? For this sort of composition, the
472
472
` Future ` trait also provides a large number of ** combinators** which can be seen
473
473
on the [ ` Future ` ] trait itself.
474
474
475
- The combinators are designed to be very similar to the ` Iterator ` combinators.
476
- That is, they all consume the receiving future and return a new future. For
477
- example we could have:
475
+ These combinators are designed to be very similar to the [ ` Iterator ` ]
476
+ combinators. That is, they all consume the receiving future and return a new
477
+ future. For example, we could have:
478
478
479
479
``` rust
480
480
fn parse <F >(future : F ) -> Box <Future <Item = u32 , Error = F :: Error >>
@@ -487,18 +487,20 @@ fn parse<F>(future: F) -> Box<Future<Item=u32, Error=F::Error>>
487
487
```
488
488
489
489
Here we're using [ ` map ` ] to transform a future of ` String ` to a future of ` u32 ` ,
490
- ignoring errors. This example returns a ` Box ` , but that's not always necessary,
490
+ ignoring errors. This example returns a [ ` Box ` ] , but that's not always necessary,
491
491
and is discussed in the [ returning futures] [ returning-futures ] section.
492
492
493
493
The combinators on futures allow expressing concepts like:
494
494
495
- * Change the type of a future ([ ` map ` ] , [ ` map_err] ` )
495
+ * Change the type of a future ([ ` map ` ] , [ ` map_err ` ] )
496
496
* Run another future after one has completed ([ ` then ` ] , [ ` and_then ` ] ,
497
497
[ ` or_else ` ] )
498
498
* Figuring out which of two futures resolves first ([ ` select ` ] )
499
499
* Waiting for two futures to both complete ([ ` join ` ] )
500
500
* Defining the behavior of [ ` poll ` ] after resolution ([ ` fuse ` ] )
501
501
502
+ [ `Iterator` ] : https://doc.rust-lang.org/1.10.0/std/iter/trait.Iterator.html
503
+ [ `Box` ] : https://doc.rust-lang.org/1.10.0/std/boxed/struct.Box.html
502
504
[ `map` ] : http://alexcrichton.com/futures-rs/futures/trait.Future.html#method.map
503
505
[ `map_err` ] : http://alexcrichton.com/futures-rs/futures/trait.Future.html#method.map_err
504
506
[ `then` ] : http://alexcrichton.com/futures-rs/futures/trait.Future.html#method.then
@@ -508,11 +510,11 @@ The combinators on futures allow expressing concepts like:
508
510
[ `join` ] : http://alexcrichton.com/futures-rs/futures/trait.Future.html#method.join
509
511
[ `fuse` ] : http://alexcrichton.com/futures-rs/futures/trait.Future.html#method.fuse
510
512
511
- Usage of the combinators should feel very similar to the ` Iterator ` trait in
512
- Rust or futures in [ Scala] [ scala-futures ] . Most composition of futures ends up
513
- being done through these combinators. All combinators are zero-cost which is
514
- another way of saying no memory is allocated internally and the implementation
515
- will optimize to what you would have otherwise written by hand.
513
+ Usage of the combinators should feel very similar to the [ ` Iterator ` ] trait in
514
+ Rust or [ futures in Scala] [ scala-futures ] . Most composition of futures ends up
515
+ being done through these combinators. All combinators are zero-cost, that means
516
+ no memory is allocated internally and the implementation will optimize to what
517
+ you would have otherwise written by hand.
516
518
517
519
---
518
520
@@ -521,10 +523,10 @@ will optimize to what you would have otherwise written by hand.
521
523
522
524
[ Back to top] [ top ]
523
525
524
- Previously we've taken a long look at the [ ` Future ` ] trait which is useful if
525
- we're only producing one value over time, but sometimes computations are best
526
- modeled as a * stream* of values being produced over time. For example a TCP
527
- listener produces a number of TCP socket connections over its lifetime. For
526
+ Previously, we've taken a long look at the [ ` Future ` ] trait which is useful if
527
+ we're only producing one value over time. But sometimes computations are best
528
+ modeled as a * stream* of values being produced over time. For example, a TCP
529
+ listener produces a number of TCP socket connections over its lifetime. For
528
530
that purpose the [ ` futures ` ] crate also includes a [ ` Stream ` ] trait:
529
531
530
532
``` rust
@@ -547,7 +549,7 @@ stream's [`poll`][stream-poll] method return `Option<Self::Item>` instead of
547
549
548
550
A [ ` Stream ` ] produces optionally many values over time, signaling termination of
549
551
the stream by returning ` Poll::Ok(None) ` . At its heart a [ ` Stream ` ] represents
550
- and asynchronous stream of values being produced in order.
552
+ an asynchronous stream of values being produced in order.
551
553
552
554
A [ ` Stream ` ] is actually just a special instance of a [ ` Future ` ] , and can be
553
555
converted to a future through the [ ` into_future ` ] method. The [ returned
@@ -572,31 +574,30 @@ stream-specific combinators like [`fold`] are also provided.
572
574
573
575
[ Back to top] [ top ]
574
576
575
- Alright at this point we've got a good idea of the [ ` Future ` ] and [ ` Stream ` ]
577
+ Alright! At this point we've got a good idea of the [ ` Future ` ] and [ ` Stream ` ]
576
578
traits both in how they're implemented as well as how they're composed together.
577
- But where to all these futures originally come from? Let's take a look at a few
579
+ But where do all these futures originally come from? Let's take a look at a few
578
580
concrete implementations of futures and streams here.
579
581
580
582
First, any value already available is trivially a future that is immediately
581
- ready. For this, the [ ` done ` ] , [ ` failed ` ] , [ ` finished ` ] suffice. The [ ` done ` ]
582
- variant takes a ` Result<T, E> ` and returns a ` Future<Item=T, Error=E> ` . The
583
- [ ` failed ` ] and [ ` finished ` ] variants then specify either ` T ` or ` E ` and leave
584
- the other associated type as a wildcard.
583
+ ready. For this, the [ ` done ` ] , [ ` failed ` ] , [ ` finished ` ] functions suffice. The
584
+ [ ` done ` ] variant takes a ` Result<T, E> ` and returns a ` Future<Item=T, Error=E> ` .
585
+ The [ ` failed ` ] and [ ` finished ` ] variants then specify either ` T ` or ` E ` and
586
+ leave the other associated type as a wildcard.
585
587
586
588
[ `done` ] : http://alexcrichton.com/futures-rs/futures/fn.done.html
587
589
[ `finished` ] : http://alexcrichton.com/futures-rs/futures/fn.finished.html
588
590
[ `failed` ] : http://alexcrichton.com/futures-rs/futures/fn.failed.html
589
591
590
- For streams the equivalent of an "immediately ready" stream is the [ ` iter ` ]
592
+ For streams, the equivalent of an "immediately ready" stream is the [ ` iter ` ]
591
593
function which creates a stream that yields the same items as the underlying
592
594
iterator.
593
595
594
596
[ `iter` ] : http://alexcrichton.com/futures-rs/futures/stream/fn.iter.html
595
597
596
598
In situations though where a value isn't immediately ready, there are also much
597
599
more general implementations of [ ` Future ` ] and [ ` Stream ` ] that are available in
598
- the [ ` futures ` ] crate, the first of which is [ ` promise ` ] . Let's first take a
599
- look:
600
+ the [ ` futures ` ] crate, the first of which is [ ` promise ` ] . Let's take a look:
600
601
601
602
[ `promise` ] : http://alexcrichton.com/futures-rs/futures/fn.promise.html
602
603
@@ -623,14 +624,15 @@ fn main() {
623
624
```
624
625
625
626
Here we can see that the [ ` promise ` ] function returns two halves (like
626
- [ ` mspc::channel ` ] ). The first half, ` tx ` , is of type [ ` Complete ` ] and is used to
627
- complete the promise, providing a value to the future on the other end. The
628
- [ ` Complete::complete ` ] method will transmit the value to the receiving end.
627
+ [ ` mpsc::channel ` ] ). The first half, ` tx ` ("transmitter"), is of type [ ` Complete ` ]
628
+ and is used to complete the promise, providing a value to the future on the
629
+ other end. The [ ` Complete::complete ` ] method will transmit the value to the
630
+ receiving end.
629
631
630
- The second half, ` rx ` , is of type [ ` Promise ` ] which is a type that implements
631
- the [ ` Future ` ] trait. The ` Item ` type is ` T ` , the type of the promise. The
632
- ` Error ` type is [ ` Canceled ` ] which happens when the [ ` Complete ` ] half is dropped
633
- without completing the computation.
632
+ The second half, ` rx ` ("receiver") , is of type [ ` Promise ` ] which is a type that
633
+ implements the [ ` Future ` ] trait. The ` Item ` type is ` T ` , the type of the promise.
634
+ The ` Error ` type is [ ` Canceled ` ] , which happens when the [ ` Complete ` ] half is
635
+ dropped without completing the computation.
634
636
635
637
[ `mpsc::channel` ] : https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html
636
638
[ `Complete` ] : http://alexcrichton.com/futures-rs/futures/struct.Complete.html
@@ -641,9 +643,8 @@ without completing the computation.
641
643
This concrete implementation of ` Future ` can be used (as shown here) to
642
644
communicate values across threads. Each half implements the ` Send ` trait and is
643
645
a separately owned entity to get passed around. It's generally not recommended
644
- to make liberal use of this type of future, however, unless necessary. The
645
- combinators above or other forms of base futures should be preferred wherever
646
- possible.
646
+ to make liberal use of this type of future, however. The combinators above or
647
+ other forms of base futures should be preferred wherever possible.
647
648
648
649
For the [ ` Stream ` ] trait, a similar primitive is available, [ ` channel ` ] . This
649
650
type also has two halves, where the sending half is used to send messages and
@@ -665,8 +666,8 @@ has caught up.
665
666
666
667
[ Back to top] [ top ]
667
668
668
- When working with futures one of the first things you're likely to run into is
669
- wanting to return a [ ` Future ` ] ! Like with the ` Iterator ` trait, however, this
669
+ When working with futures, one of the first things you're likely to run into is
670
+ wanting to return a [ ` Future ` ] ! Like with the [ ` Iterator ` ] trait, however, this
670
671
isn't currently always the easiest thing to do. Let's walk through your options,
671
672
however.
672
673
@@ -688,7 +689,7 @@ fn foo() -> Box<Future<Item = u32, Error = io::Error>> {
688
689
}
689
690
```
690
691
691
- The upside of this strategy is that it's easy to write down (just a ` Box ` ) and
692
+ The upside of this strategy is that it's easy to write down (just a [ ` Box ` ] ) and
692
693
easy to create (through the [ ` boxed ` ] method). This is also maximally flexible
693
694
in terms of future changes to the method as * any* future can be returned from
694
695
this method.
@@ -827,13 +828,13 @@ Enter, a [`Task`]!
827
828
In the [ ` futures ` ] crate there is a struct called [ ` Task ` ] which is used to
828
829
drive a computation represented by futures. One particular instance of a
829
830
` Future ` may be short-lived, and may only be part of one large computations. For
830
- example in our [ "hello world"] [ hello-world ] example we had a number of futures,
831
+ example, in our [ "hello world"] [ hello-world ] example we had a number of futures,
831
832
but only one actually existed in memory at a time. For the entire program, we'll
832
833
have one [ ` Task ` ] that followed the logical "thread of execution" as the future
833
834
progressed.
834
835
835
836
In short, a ` Task ` is the one that's actually orchestrating the top-level calls
836
- to ` poll ` and ` schedule ` . Its main method, [ ` run ` ] does exactly this. Internally
837
+ to ` poll ` and ` schedule ` . Its main method, [ ` run ` ] does exactly this. Internally,
837
838
` Task ` has synchronization for if [ ` notify ` ] is called on multiple threads it'll
838
839
ensure that the calls to [ ` poll ` ] are coordinated.
839
840
@@ -856,7 +857,7 @@ lifetime of the computation.
856
857
857
858
---
858
859
859
- ## Task local data
860
+ ## Task- local data
860
861
[ task-local-data ] : #task-local-data
861
862
862
863
[ Back to top] [ top ]
0 commit comments