Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate ch06-02 in french. #11

Open
wants to merge 1 commit into
base: french-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions FRENCH/examples-sources/06_02_join/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "example_06_02_join"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"

[lib]

[dev-dependencies]
futures = "0.3"
77 changes: 77 additions & 0 deletions FRENCH/examples-sources/06_02_join/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![cfg(test)]

struct Book;
struct Music;
async fn get_book() -> Book { Book }
async fn get_music() -> Music { Music }

mod naiive {
use super::*;
// ANCHOR: naiive
async fn get_book_and_music() -> (Book, Music) {
let book = get_book().await;
let music = get_music().await;
(book, music)
}
// ANCHOR_END: naiive
}

mod other_langs {
use super::*;
// ANCHOR: other_langs
// WRONG -- don't do this
async fn get_book_and_music() -> (Book, Music) {
let book_future = get_book();
let music_future = get_music();
(book_future.await, music_future.await)
}
// ANCHOR_END: other_langs
}

mod join {
use super::*;
// ANCHOR: join
use futures::join;

async fn get_book_and_music() -> (Book, Music) {
let book_fut = get_book();
let music_fut = get_music();
join!(book_fut, music_fut)
}
// ANCHOR_END: join
}

mod try_join {
use super::{Book, Music};
// ANCHOR: try_join
use futures::try_join;

async fn get_book() -> Result<Book, String> { /* ... */ Ok(Book) }
async fn get_music() -> Result<Music, String> { /* ... */ Ok(Music) }

async fn get_book_and_music() -> Result<(Book, Music), String> {
let book_fut = get_book();
let music_fut = get_music();
try_join!(book_fut, music_fut)
}
// ANCHOR_END: try_join
}

mod mismatched_err {
use super::{Book, Music};
// ANCHOR: try_join_map_err
use futures::{
future::TryFutureExt,
try_join,
};

async fn get_book() -> Result<Book, ()> { /* ... */ Ok(Book) }
async fn get_music() -> Result<Music, String> { /* ... */ Ok(Music) }

async fn get_book_and_music() -> Result<(Book, Music), String> {
let book_fut = get_book().map_err(|()| "Unable to get book".to_string());
let music_fut = get_music();
try_join!(book_fut, music_fut)
}
// ANCHOR_END: try_join_map_err
}
1 change: 1 addition & 0 deletions FRENCH/examples-sources/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[workspace]
members = [
"06_02_join",
]
10 changes: 10 additions & 0 deletions FRENCH/examples/06_02_join/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "example_06_02_join"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"

[lib]

[dev-dependencies]
futures = "0.3"
77 changes: 77 additions & 0 deletions FRENCH/examples/06_02_join/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![cfg(test)]

struct Livre;
struct Musique;
async fn obtenir_livre() -> Livre { Livre }
async fn obtenir_musique() -> Musique { Musique }

mod naiive {
use super::*;
// ANCHOR: naiive
async fn obtenir_livre_et_musique() -> (Livre, Musique) {
let livre = obtenir_livre().await;
let musique = obtenir_musique().await;
(livre, musique)
}
// ANCHOR_END: naiive
}

mod other_langs {
use super::*;
// ANCHOR: other_langs
// MAUVAISE FAÇON -- ne faites pas cela
async fn obtenir_livre_et_musique() -> (Livre, Musique) {
let future_livre = obtenir_livre();
let future_musique = obtenir_musique();
(future_livre.await, future_musique.await)
}
// ANCHOR_END: other_langs
}

mod join {
use super::*;
// ANCHOR: join
use futures::join;

async fn obtenir_livre_et_musique() -> (Livre, Musique) {
let future_livre = obtenir_livre();
let future_musique = obtenir_musique();
join!(future_livre, future_musique)
}
// ANCHOR_END: join
}

mod try_join {
use super::{Livre, Musique};
// ANCHOR: try_join
use futures::try_join;

async fn obtenir_livre() -> Result<Livre, String> { /* ... */ Ok(Livre) }
async fn obtenir_musique() -> Result<Musique, String> { /* ... */ Ok(Musique) }

async fn obtenir_livre_et_musique() -> Result<(Livre, Musique), String> {
let future_livre = obtenir_livre();
let future_musique = obtenir_musique();
try_join!(future_livre, future_musique)
}
// ANCHOR_END: try_join
}

mod mismatched_err {
use super::{Livre, Musique};
// ANCHOR: try_join_map_err
use futures::{
future::TryFutureExt,
try_join,
};

async fn obtenir_livre() -> Result<Livre, ()> { /* ... */ Ok(Livre) }
async fn obtenir_musique() -> Result<Musique, String> { /* ... */ Ok(Musique) }

async fn obtenir_livre_et_musique() -> Result<(Livre, Musique), String> {
let future_livre = obtenir_livre().map_err(|()| "Impossible d'obtenir le livre".to_string());
let future_musique = obtenir_musique();
try_join!(future_livre, future_musique)
}
// ANCHOR_END: try_join_map_err
}
1 change: 1 addition & 0 deletions FRENCH/examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[workspace]
members = [
"06_02_join",
]
150 changes: 150 additions & 0 deletions FRENCH/src/06_multiple_futures/02_join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<!--
# `join!`
-->

# `join!`

<!--
The `futures::join` macro makes it possible to wait for multiple different
futures to complete while executing them all concurrently.
-->

La macro `futures::join` permet d'attendre que plusieurs futures différentes se
terminent pendant qu'elles sont toutes exécutées en concurrence.

<!--
# `join!`
-->

## `join!`

<!--
When performing multiple asynchronous operations, it's tempting to simply
`.await` them in a series:
-->

Lorsque nous avons besoin de faire plusieurs opérations asynchrones, il peut
être tentant d'utiliser `.await` en série sur elles :

<!--
```rust,edition2018,ignore
{{#include ../../examples-sources/06_02_join/src/lib.rs:naiive}}
```
-->

```rust,edition2018,ignore
{{#include ../../examples/06_02_join/src/lib.rs:naiive}}
```

<!--
However, this will be slower than necessary, since it won't start trying to
`get_music` until after `get_book` has completed. In some other languages,
futures are ambiently run to completion, so two operations can be
run concurrently by first calling each `async fn` to start the futures, and
then awaiting them both:
-->

En revanche, cela peut être plus lent que nécessaire, puisqu'il ne commence
qu'à `obtenir_musique` avant que `obtenir_livre` soit terminé. Dans d'autres
langages, les futures sont exécutées normalement jusqu'à leur fin, donc deux
opérations peuvent être exécutées en concurrence en appelant chacune des
`async fn` pour démarrer les futures, et ensuite attendre la fin des deux :

<!--
```rust,edition2018,ignore
{{#include ../../examples-sources/06_02_join/src/lib.rs:other_langs}}
```
-->

```rust,edition2018,ignore
{{#include ../../examples/06_02_join/src/lib.rs:other_langs}}
```

<!--
However, Rust futures won't do any work until they're actively `.await`ed.
This means that the two code snippets above will both run
`book_future` and `music_future` in series rather than running them
concurrently. To correctly run the two futures concurrently, use
`futures::join!`:
-->

Malheureusement, les futures en Rust ne font rien tant qu'on n'utilise pas
`.await` sur elles. Cela signifie que les deux extraits de code ci-dessus vont
exécuter `future_livre` et `future_musique` en série au lieu de les exécuter en
concurrence. Pour exécuter correctement les deux futures en concurrence,
utilisons `futures::join!` :

<!--
```rust,edition2018,ignore
{{#include ../../examples-sources/06_02_join/src/lib.rs:join}}
```
-->

```rust,edition2018,ignore
{{#include ../../examples/06_02_join/src/lib.rs:join}}
```

<!--
The value returned by `join!` is a tuple containing the output of each
`Future` passed in.
-->

La valeur retournée par `join!` est une tuple contenant le résultat de chacune
des `Future`s qu'on lui a donné.

<!--
## `try_join!`
-->

## `try_join!`

<!--
For futures which return `Result`, consider using `try_join!` rather than
`join!`. Since `join!` only completes once all subfutures have completed,
it'll continue processing other futures even after one of its subfutures
has returned an `Err`.
-->

Pour les futures qui retournent `Result`, il vaut mieux utiliser `try_join!`
plutôt que `join!`. Comme `join!` se termine uniquement lorsque toutes les
sous-futures se soient terminées, il va continuer à calculer les autres futures
même si une de ses sous-futures a retourné une `Err`.

<!--
Unlike `join!`, `try_join!` will complete immediately if one of the subfutures
returns an error.
-->

Contrairement à `join!`, `try_join!` va se terminer tout de suite si une des
sous-futures retourne une erreur.

<!--
```rust,edition2018,ignore
{{#include ../../examples-sources/06_02_join/src/lib.rs:try_join}}
```
-->

```rust,edition2018,ignore
{{#include ../../examples/06_02_join/src/lib.rs:try_join}}
```

<!--
Note that the futures passed to `try_join!` must all have the same error type.
Consider using the `.map_err(|e| ...)` and `.err_into()` functions from
`futures::future::TryFutureExt` to consolidate the error types:
-->

Notez que les futures envoyées au `try_join!` doivent toutes avoir le même type
d'erreur. Vous pouvez utiliser les fonctions `.map_err(|e| ...)` et
`.err_into()` de `futures::future::TryFutureExt` pour regrouper les types
d'erreurs :

<!--
```rust,edition2018,ignore
{{#include ../../examples-sources/06_02_join/src/lib.rs:try_join_map_err}}
```
-->

```rust,edition2018,ignore
{{#include ../../examples/06_02_join/src/lib.rs:try_join_map_err}}
```
2 changes: 2 additions & 0 deletions FRENCH/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Table des matières

- [`join!`](06_multiple_futures/02_join.md)

[Traduction des termes](translation-terms.md)
1 change: 1 addition & 0 deletions FRENCH/src/translation-terms.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ français.

| Anglais | Français | Remarques |
| ------- | ------ | ------ |
| tuple | tuple | - |