From 6c7922a404bd792d2fc767e0969af84bf05a967a Mon Sep 17 00:00:00 2001 From: Florian Gilcher Date: Mon, 12 Aug 2019 21:17:21 +0200 Subject: [PATCH] Add small patterns library --- docs/src/SUMMARY.md | 1 + docs/src/patterns.md | 6 +++++- docs/src/patterns/small-patterns.md | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 docs/src/patterns/small-patterns.md diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 33ee1e5..a71a1be 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -17,6 +17,7 @@ - [Proper Shutdown](./patterns/proper-shutdown.md) - [Background Tasks](./patterns/background-tasks.md) - [Testing](./patterns/testing.md) + - [Collected Small Patterns](./patters/small-patterns.md) - [Security practices](./security/index.md) - [Security disclosures and policy](./security/policy.md) - [Glossary](./glossary.md) diff --git a/docs/src/patterns.md b/docs/src/patterns.md index d435f36..a19b81b 100644 --- a/docs/src/patterns.md +++ b/docs/src/patterns.md @@ -1 +1,5 @@ -# Async Patterns +# Patterns + +This section documents small, useful patterns. + +It is intended to be read at a glance, allowing you to get back when you have a problem. \ No newline at end of file diff --git a/docs/src/patterns/small-patterns.md b/docs/src/patterns/small-patterns.md new file mode 100644 index 0000000..586702a --- /dev/null +++ b/docs/src/patterns/small-patterns.md @@ -0,0 +1,18 @@ +# Small Patterns + +A collection of small, useful patterns. + + + +## Splitting streams + +`async-std` doesn't provide a `split()` method on `io` handles. Instead, splitting a stream into a read and write half can be done like this: + +```rust +use async_std::io; + +async fn echo(stream: io::TcpStream) { + let (reader, writer) = &mut (&stream, &stream); + io::copy(reader, writer).await?; +} +``` \ No newline at end of file