Skip to Content
Async Rust ―高いパフォーマンスと安全性を両立するRustによる非同期処理
book

Async Rust ―高いパフォーマンスと安全性を両立するRustによる非同期処理

by Maxwell Flitton, Caroline Morton, 中田 秀基
July 2025
Intermediate to advanced
284 pages
4h 4m
Japanese
O'Reilly Japan, Inc.
Content preview from Async Rust ―高いパフォーマンスと安全性を両立するRustによる非同期処理

9章デザインパターン

これまでに、さまざまな非同期の概念を説明し、問題を解決するために非同期コードを実装する方法を複数紹介した。しかし、ソフトウェアエンジニアリングは、単独で存在するわけではない。本書で得た非同期プログラミングに関する知識を実際に適用する際には、完全な環境に、独立した非同期のコードを書くというわけにはいかない。非同期でない既存のコードに、非同期コードを適用しなければならないこともあるだろう。サーバなどの第三者が提供するサービスとやり取りすることもあるだろう。この場合は、サーバからのレスポンスの変化に対処する必要がある。本章では、さまざまな問題の解決のために非同期コードを実装する際の一助となるデザインパターンについて説明する。

本章を読めば、これまで非同期プログラミングをサポートしてこなかった既存のコードに対して、非同期コードを実装できるようになる。さらに、ウォーターフォールデザインパターンを実装して、再利用可能な非同期コンポーネントのフローを構築できるようになる。非同期タスクのコードを変更して機能を追加するのではなく、デコレータパターンを実装して、プログラムを実行する際もしくはビルドする際にコンパイルフラグを立てるだけで、ロギングなどの機能を追加できるようになる。また、リトライやサーキットブレーカパターンを用いて、非同期システム全体でエラーに対応できるようになる。

まず、デザインパターンを実装する前に、非同期コードをシステム内に実装できるようになる必要がある。したがって、まずは独立したモジュールの構築から始めよう。

9.1 独立したモジュールの作成

非同期コードを全く含まないRustの既存コードがあり、この既存コードに非同期Rustコードを統合したいとしよう。既存コードを完全に書き直して非同期コードを導入するよりも、これらのコード間の相互作用の爆発半径を小さく保つほうがいい。大幅に書き直す場合、締めきりを守れることはほとんどなく、書き直しが遅れると既存コードに機能がさらに追加されてしまって、書き直しが終わらなくなるからだ。非同期コードを独立したモジュールに書き、同期エントリポイントを与えることで、小さく始めることができる。同期エントリポイントを用いると、非同期モジュールを既存のコードのどこからでも使うことができる。また、他の開発者が非同期プログラミングをよく理解していなくても、非同期モジュールを使うことができる。このようにすると、統合が容易になる。他の開発者は、時間があるときに非同期プログラミングを理解すればいい。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Async Rust

Async Rust

Maxwell Flitton, Caroline Morton
Rust Web Programming

Rust Web Programming

Maxwell Flitton

Publisher Resources

ISBN: 9784814401185Publisher Website