Skip to content

Commit

Permalink
Move trait bounds before passing args in readme
Browse files Browse the repository at this point in the history
Also fix broken link for pass_args example.
  • Loading branch information
ozars committed Apr 12, 2024
1 parent 0183a30 commit eb35f4b
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ simple_example`.
[simple_example]: examples/simple_example.rs
[`cargo-expand`]: https://crates.io/crates/cargo-expand

## Passing Arguments
## Trait Bounds

Arguments can be passed to specializations, however, argument types need to
declared explicitly (i.e. they won't be captured automatically as it happens
with closures).
Trait bounds can be provided for the default case:

```rust
#![feature(min_specialization)]
Expand All @@ -105,35 +103,39 @@ use std::fmt::Display;

use specialized_dispatch::specialized_dispatch;

fn example<T: Display>(expr: T, arg: &str) -> String {
// The expression type must also bind to the same trait.
fn example<E: Display>(expr: E) -> String {
specialized_dispatch!(
T -> String,
default fn <T: Display>(v: T, arg: &str) => {
format!("default value: {}, arg: {}", v, arg)
},
fn (v: u8, arg: &str) => format!("u8: {}, arg: {}", v, arg),
fn (v: u16, arg: &str) => format!("u16: {}, arg: {}", v, arg),
expr, arg,
E -> String,
// Notice the trait bound.
default fn <T: Display>(v: T) => format!("default value: {}", v),
// Note that specializations also need to satisfy the same bound.
fn (v: u8) => format!("u8: {}", v),
fn (v: u16) => format!("u16: {}", v),
expr,
)
}

fn main() {
assert_eq!(example(1.5, "I'm a"), "default value: 1.5, arg: I'm a");
assert_eq!(example(5u8, "walnut"), "u8: 5, arg: walnut");
assert_eq!(example(10u16, "tree"), "u16: 10, arg: tree");
assert_eq!(example(1.5), "default value: 1.5");
assert_eq!(example(5u8), "u8: 5");
assert_eq!(example(10u16), "u16: 10");
println!("Done!");
}
```

Likewise, the example above is [included][pass_args] in the repository.
Likewise, the example above is [included][trait_bound] in the repository.

It can be run with `cargo run --example pass_args` or inspected with
It can be run with `cargo run --example trait_bound` or inspected with
`cargo-expand`.

[trait_bound]: examples/pass_args.rs
[trait_bound]: examples/trait_bound.rs

## Trait Bounds
## Passing Arguments

Trait bounds can be provided for the default case:
Arguments can be passed to specializations, however, argument types need to
declared explicitly (i.e. they won't be captured automatically as it happens
with closures).

```rust
#![feature(min_specialization)]
Expand All @@ -142,33 +144,31 @@ use std::fmt::Display;

use specialized_dispatch::specialized_dispatch;

// The expression type must also bind to the same trait.
fn example<E: Display>(expr: E) -> String {
fn example<T: Display>(expr: T, arg: &str) -> String {
specialized_dispatch!(
E -> String,
// Notice the trait bound.
default fn <T: Display>(v: T) => format!("default value: {}", v),
// Note that specializations also need to satisfy the same bound.
fn (v: u8) => format!("u8: {}", v),
fn (v: u16) => format!("u16: {}", v),
expr,
T -> String,
default fn <T: Display>(v: T, arg: &str) => {
format!("default value: {}, arg: {}", v, arg)
},
fn (v: u8, arg: &str) => format!("u8: {}, arg: {}", v, arg),
fn (v: u16, arg: &str) => format!("u16: {}, arg: {}", v, arg),
expr, arg,
)
}

fn main() {
assert_eq!(example(1.5), "default value: 1.5");
assert_eq!(example(5u8), "u8: 5");
assert_eq!(example(10u16), "u16: 10");
println!("Done!");
assert_eq!(example(1.5, "I'm a"), "default value: 1.5, arg: I'm a");
assert_eq!(example(5u8, "walnut"), "u8: 5, arg: walnut");
assert_eq!(example(10u16, "tree"), "u16: 10, arg: tree");
}
```

Likewise, the example above is [included][trait_bound] in the repository.
Likewise, the example above is [included][pass_args] in the repository.

It can be run with `cargo run --example trait_bound` or inspected with
It can be run with `cargo run --example pass_args` or inspected with
`cargo-expand`.

[trait_bound]: examples/trait_bound.rs
[pass_args]: examples/pass_args.rs

## Limitations

Expand Down

0 comments on commit eb35f4b

Please sign in to comment.