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

Moved 'explicitly typed values' section from README.md to src/lib.rs #36

Merged
merged 1 commit into from
Jan 28, 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
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
Loading