Skip to content

Commit

Permalink
Moved 'explicitly typed values' section from README.md to src/lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jofas committed Jan 28, 2024
1 parent 714bd50 commit dd50de5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 53 deletions.
52 changes: 0 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,57 +25,5 @@ let hello = hash_map! {
};
```

## Explicitly Typed Values for Trait Objects

As shown in the example above, the compiler uses type inference to infer the correct type
for the created map.
Unfortunately, type inference alone can not detect [trait objects][trait objects].
This will not work, because the compiler is unable to figure out the right type:

```compile_fail
use std::collections::HashMap;
use std::fmt::Debug;
use map_macro::hash_map;
let hello: HashMap<&str, &dyn Debug> = hash_map! {
"en" => &"Hello",
"de" => &"Hallo",
"fr" => &"Bonjour",
"es" => &"Hola",
};
```

The `map_e!` macro enables you to use trait objects as values through
[type coercion][type coercion], making the example above compile successfully:

```rust
use std::collections::HashMap;
use std::fmt::Debug;

use map_macro::hash_map_e;

let hello: HashMap<&str, &dyn Debug> = hash_map_e! {
"en" => &"Hello",
"de" => &"Hallo",
"fr" => &"Bonjour",
"es" => &"Hola",
};
```

Note that you need to give an explicit type to the binding when you use `map_e!`, because
it relies on knowing what type it should coerce the values to.
Also, only values and not keys can be trait objects, because keys must
implement the [`Hash`][hash] trait, which is not
[object safe][object safe].

Where the trait bounds on generic type parameters of the collections allow trait objects,
macros for explicitly typed variants are provided.
The explicitly typed versions of the macros are indicated by an `_e` suffix.

[std]: https://doc.rust-lang.org/std/collections/index.html
[trait objects]: https://doc.rust-lang.org/reference/types/trait-object.html
[type coercion]: https://doc.rust-lang.org/reference/type-coercions.html
[object safe]: https://doc.rust-lang.org/reference/items/traits.html#object-safety
[hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
[hashbrown]: https://docs.rs/hashbrown/latest/hashbrown/
2 changes: 1 addition & 1 deletion src/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! **Note:** to be compatible with all versions of `hashbrown` at once, this
//! crate doesn't re-export `hashbrown`, which means that if you rename
//! `hashbrown` in your dependencies, the macros from this module won't be able
//! to import the types resulting in a compile-time error.
//! to import the needed types resulting in a compile-time error.
//!
/// Macro for creating a [`HashMap`](::hashbrown::HashMap).
Expand Down
55 changes: 55 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
#![doc = include_str!("../README.md")]
//!
//! ## Explicitly Typed Values for Trait Objects
//!
//! As shown in the example above, the compiler uses type inference to infer the correct type
//! for the created map.
//! Unfortunately, type inference alone can not detect [trait objects][trait objects].
//! This will not work, because the compiler is unable to figure out the right type:
//!
//! ```compile_fail
//! use std::collections::HashMap;
//! use std::fmt::Debug;
//!
//! use map_macro::hash_map;
//!
//! let hello: HashMap<&str, &dyn Debug> = hash_map! {
//! "en" => &"Hello",
//! "de" => &"Hallo",
//! "fr" => &"Bonjour",
//! "es" => &"Hola",
//! };
//! ```
//!
//! The `map_e!` macro enables you to use trait objects as values through
//! [type coercion][type coercion], making the example above compile successfully:
//!
//! ```rust
//! use std::collections::HashMap;
//! use std::fmt::Debug;
//!
//! use map_macro::hash_map_e;
//!
//! let hello: HashMap<&str, &dyn Debug> = hash_map_e! {
//! "en" => &"Hello",
//! "de" => &"Hallo",
//! "fr" => &"Bonjour",
//! "es" => &"Hola",
//! };
//! ```
//!
//! Note that you need to give an explicit type to the binding when you use `map_e!`, because
//! it relies on knowing what type it should coerce the values to.
//! Also, only values and not keys can be trait objects, because keys must
//! implement the [`Hash`][hash] trait, which is not
//! [object safe][object safe].
//!
//! Where the trait bounds on generic type parameters of the collections allow trait objects,
//! macros for explicitly typed variants are provided.
//! The explicitly typed versions of the macros are indicated by an `_e` suffix.
//!
//! [trait objects]: https://doc.rust-lang.org/reference/types/trait-object.html
//! [type coercion]: https://doc.rust-lang.org/reference/type-coercions.html
//! [object safe]: https://doc.rust-lang.org/reference/items/traits.html#object-safety
//! [hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
//!
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![no_std]

Expand Down

1 comment on commit dd50de5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: dd50de5 Previous: 714bd50 Ratio
map allocation 106361829 ns/iter (± 1446904) 105285638 ns/iter (± 817514) 1.01
set allocation 156638077 ns/iter (± 5450993) 156821815 ns/iter (± 3742240) 1.00
vec_no_clone allocation 334702236 ns/iter (± 1133446) 339022775 ns/iter (± 918852) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.