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

Add chapter for Impl-side dependencies #6

Merged
merged 5 commits into from
Jun 30, 2024
Merged
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
4 changes: 3 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ jobs:

- name: Run tests
run: |
cargo clean
cargo test
nix shell .#mdbook -c \
mdbook test
mdbook test -L target/debug/deps

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
book


# Added by cargo

/target
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "cgp-patterns"
version = "0.1.0"
edition = "2021"

[dependencies]
itertools = "0.13.0"
9 changes: 8 additions & 1 deletion content/blanket-implementations.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,19 @@ person.greet();
As shown above, we are able to call `person.greet()` without having a context-specific
implementation of `CanGreet` for `Person`.

## Extension Traits

The use of blanket trait implementation is commonly found in many Rust libraries today.
For example, [`Itertools`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html)
provides a blanket implementation for any context that implements `Iterator`.
Another example is [`StreamExt`](https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html),
which is implemented for any context that implements `Stream`.

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

## Overriding Blanket Implementations

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

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