Skip to content

Commit dd50de5

Browse files
committed
Moved 'explicitly typed values' section from README.md to src/lib.rs
1 parent 714bd50 commit dd50de5

File tree

3 files changed

+56
-53
lines changed

3 files changed

+56
-53
lines changed

README.md

-52
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,5 @@ let hello = hash_map! {
2525
};
2626
```
2727

28-
## Explicitly Typed Values for Trait Objects
29-
30-
As shown in the example above, the compiler uses type inference to infer the correct type
31-
for the created map.
32-
Unfortunately, type inference alone can not detect [trait objects][trait objects].
33-
This will not work, because the compiler is unable to figure out the right type:
34-
35-
```compile_fail
36-
use std::collections::HashMap;
37-
use std::fmt::Debug;
38-
39-
use map_macro::hash_map;
40-
41-
let hello: HashMap<&str, &dyn Debug> = hash_map! {
42-
"en" => &"Hello",
43-
"de" => &"Hallo",
44-
"fr" => &"Bonjour",
45-
"es" => &"Hola",
46-
};
47-
```
48-
49-
The `map_e!` macro enables you to use trait objects as values through
50-
[type coercion][type coercion], making the example above compile successfully:
51-
52-
```rust
53-
use std::collections::HashMap;
54-
use std::fmt::Debug;
55-
56-
use map_macro::hash_map_e;
57-
58-
let hello: HashMap<&str, &dyn Debug> = hash_map_e! {
59-
"en" => &"Hello",
60-
"de" => &"Hallo",
61-
"fr" => &"Bonjour",
62-
"es" => &"Hola",
63-
};
64-
```
65-
66-
Note that you need to give an explicit type to the binding when you use `map_e!`, because
67-
it relies on knowing what type it should coerce the values to.
68-
Also, only values and not keys can be trait objects, because keys must
69-
implement the [`Hash`][hash] trait, which is not
70-
[object safe][object safe].
71-
72-
Where the trait bounds on generic type parameters of the collections allow trait objects,
73-
macros for explicitly typed variants are provided.
74-
The explicitly typed versions of the macros are indicated by an `_e` suffix.
75-
7628
[std]: https://doc.rust-lang.org/std/collections/index.html
77-
[trait objects]: https://doc.rust-lang.org/reference/types/trait-object.html
78-
[type coercion]: https://doc.rust-lang.org/reference/type-coercions.html
79-
[object safe]: https://doc.rust-lang.org/reference/items/traits.html#object-safety
80-
[hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
8129
[hashbrown]: https://docs.rs/hashbrown/latest/hashbrown/

src/hashbrown.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! **Note:** to be compatible with all versions of `hashbrown` at once, this
1818
//! crate doesn't re-export `hashbrown`, which means that if you rename
1919
//! `hashbrown` in your dependencies, the macros from this module won't be able
20-
//! to import the types resulting in a compile-time error.
20+
//! to import the needed types resulting in a compile-time error.
2121
//!
2222
2323
/// Macro for creating a [`HashMap`](::hashbrown::HashMap).

src/lib.rs

+55
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,59 @@
11
#![doc = include_str!("../README.md")]
2+
//!
3+
//! ## Explicitly Typed Values for Trait Objects
4+
//!
5+
//! As shown in the example above, the compiler uses type inference to infer the correct type
6+
//! for the created map.
7+
//! Unfortunately, type inference alone can not detect [trait objects][trait objects].
8+
//! This will not work, because the compiler is unable to figure out the right type:
9+
//!
10+
//! ```compile_fail
11+
//! use std::collections::HashMap;
12+
//! use std::fmt::Debug;
13+
//!
14+
//! use map_macro::hash_map;
15+
//!
16+
//! let hello: HashMap<&str, &dyn Debug> = hash_map! {
17+
//! "en" => &"Hello",
18+
//! "de" => &"Hallo",
19+
//! "fr" => &"Bonjour",
20+
//! "es" => &"Hola",
21+
//! };
22+
//! ```
23+
//!
24+
//! The `map_e!` macro enables you to use trait objects as values through
25+
//! [type coercion][type coercion], making the example above compile successfully:
26+
//!
27+
//! ```rust
28+
//! use std::collections::HashMap;
29+
//! use std::fmt::Debug;
30+
//!
31+
//! use map_macro::hash_map_e;
32+
//!
33+
//! let hello: HashMap<&str, &dyn Debug> = hash_map_e! {
34+
//! "en" => &"Hello",
35+
//! "de" => &"Hallo",
36+
//! "fr" => &"Bonjour",
37+
//! "es" => &"Hola",
38+
//! };
39+
//! ```
40+
//!
41+
//! Note that you need to give an explicit type to the binding when you use `map_e!`, because
42+
//! it relies on knowing what type it should coerce the values to.
43+
//! Also, only values and not keys can be trait objects, because keys must
44+
//! implement the [`Hash`][hash] trait, which is not
45+
//! [object safe][object safe].
46+
//!
47+
//! Where the trait bounds on generic type parameters of the collections allow trait objects,
48+
//! macros for explicitly typed variants are provided.
49+
//! The explicitly typed versions of the macros are indicated by an `_e` suffix.
50+
//!
51+
//! [trait objects]: https://doc.rust-lang.org/reference/types/trait-object.html
52+
//! [type coercion]: https://doc.rust-lang.org/reference/type-coercions.html
53+
//! [object safe]: https://doc.rust-lang.org/reference/items/traits.html#object-safety
54+
//! [hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
55+
//!
56+
257
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
358
#![no_std]
459

0 commit comments

Comments
 (0)