Skip to content

Commit 8a23260

Browse files
authored
Add chapter for Impl-side dependencies (#6)
* Draft code outline for Impl-side dependencies * Fix errors and CI * Update Nix * Add more example code * Finish drafting impl-side dependencies
1 parent 98ca3b9 commit 8a23260

File tree

8 files changed

+572
-6
lines changed

8 files changed

+572
-6
lines changed

.github/workflows/build.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ jobs:
4141
4242
- name: Run tests
4343
run: |
44+
cargo clean
45+
cargo test
4446
nix shell .#mdbook -c \
45-
mdbook test
47+
mdbook test -L target/debug/deps
4648
4749
- name: Upload artifact
4850
uses: actions/upload-pages-artifact@v3

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
book
2+
3+
4+
# Added by cargo
5+
6+
/target

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "cgp-patterns"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
itertools = "0.13.0"

content/blanket-implementations.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,19 @@ person.greet();
6767
As shown above, we are able to call `person.greet()` without having a context-specific
6868
implementation of `CanGreet` for `Person`.
6969

70+
## Extension Traits
71+
7072
The use of blanket trait implementation is commonly found in many Rust libraries today.
7173
For example, [`Itertools`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html)
7274
provides a blanket implementation for any context that implements `Iterator`.
7375
Another example is [`StreamExt`](https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html),
7476
which is implemented for any context that implements `Stream`.
7577

78+
Traits such as `Itertools` and `StreamExt` are sometimes known as _extension traits_. This is
79+
because the purpose of the trait is to _extend_ the behavior of existing types, without having
80+
to own the type or base traits. While the use of extension traits is a common use case for
81+
blanket implementations, there are other ways we can make use of blanket implementations.
82+
7683
## Overriding Blanket Implementations
7784

7885
Traits containing blanket implementation are usually not meant to be implemented manually
@@ -215,6 +222,6 @@ Although a context many define its own context-specific provider to override the
215222
provider, it would face other limitations such as not being able to implement other traits
216223
that may cause a conflict.
217224

218-
In practice, we consider that blanket implementations allow for _singular context-generic provider_
225+
In practice, we consider that blanket implementations allow for _a singule context-generic provider_
219226
to be defined. In future chapters, we will look at how to relax the singular constraint,
220227
to make it possible to allow _multiple_ context-generic or context-specific providers to co-exist.

0 commit comments

Comments
 (0)