Repository navigation
Add working std::unordered_map overloads #70
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| //! Test C++ [std::unordered_map][unordered_map] overloads that can't be represented in Rust, and | ||
| //! record the macro diagnostics. Error output is in `stdcpp-unordered-map.stderr`. | ||
| //! Working overloads are in `src/stdcpp/unordered_map.rs`. | ||
| //! | ||
| //! [unordered_map]: https://en.cppreference.com/cpp/container/unordered_map | ||
| #![feature(splat, tuple_trait)] | ||
| #![allow(incomplete_features)] | ||
| #![allow(unused_braces)] | ||
|
|
||
| use splat_overload::overload; | ||
|
|
||
| /// Workaround for missing generic support in the `overload!` macro: the map key type. | ||
| type K = std::ffi::c_int; | ||
|
|
||
| /// Workaround for missing generic support in the `overload!` macro: the mapped value type. | ||
| type V = std::ffi::c_int; | ||
|
|
||
| /// A fake key-value pair, matching the working `KeyValue` type. | ||
| #[expect(dead_code, reason = "Incompatible overloads can't be used")] | ||
| #[derive(Copy, Clone)] | ||
| struct KeyValue { | ||
| key: K, | ||
| value: V, | ||
| } | ||
|
|
||
| /// A wrapper struct to hold the returned C++ pointer. | ||
| /// We fake the C++ map pointer with a Rust vector to help with type checking. | ||
| #[expect(dead_code, reason = "Incompatible overloads can't be used")] | ||
| struct StdUnorderedMap(Vec<KeyValue>); | ||
|
|
||
| // Accessors: `max_load_factor(...)` | ||
| overload! { | ||
| impl StdUnorderedMap { | ||
| /// Gets the maximum load factor, a `const` method in C++. | ||
| /// | ||
| /// ### Incompatibilities | ||
| /// | ||
| /// The `overload!` macro requires all overloads in a group to have matching | ||
| /// receiver kinds, but `max_load_factor` uses both `&self` and `&mut self`. | ||
| /// | ||
| /// ### Workarounds | ||
| /// | ||
| /// Use associated functions with explicit `&StdUnorderedMap` and | ||
| /// `&mut StdUnorderedMap` parameters, as in `src/stdcpp/unordered_map.rs`. | ||
| fn max_load_factor(&self) -> f32 { | ||
| todo!() | ||
| } | ||
|
|
||
| /// Set the maximum load factor, a non-`const` method in C++. | ||
| fn max_load_factor(&mut self, _ml: f32) { | ||
| todo!() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Accessors: `contains(...)` | ||
| overload! { | ||
| impl StdUnorderedMap { | ||
| /// Check whether the map contains `key`. | ||
| fn contains(&self, _key: &K) -> bool { | ||
| todo!() | ||
| } | ||
|
|
||
| /// Checks whether the map contains a key equal to `key`, even if it has a | ||
| /// different type from the map's key type. | ||
| /// | ||
| /// ### Incompatibilities | ||
| /// | ||
| /// The `overload!` macro does not preserve generic parameters, leaving `Q` | ||
| /// unresolved in the generated code. | ||
| /// | ||
| /// ### Limitations | ||
| /// | ||
| /// C++ only allows this overload when both `Hash` and `KeyEqual` support | ||
| /// transparent lookup through `is_transparent`. Rust has no direct equivalent. | ||
| /// | ||
| /// ### Workaround | ||
| /// | ||
| /// Use `HashMap::get` with the `K: Borrow<Q>` trait bound instead. | ||
| fn contains<Q>(&self, _key: &Q) -> bool { | ||
| todo!() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| error: proc macro panicked | ||
| --> cpp-overload-test/examples/stdcpp-unordered-map.rs:32:1 | ||
| | | ||
| 32 | / overload! { | ||
| 33 | | impl StdUnorderedMap { | ||
| 34 | | /// Gets the maximum load factor, a `const` method in C++. | ||
| 35 | | /// | ||
| ... | | ||
| 54 | | } | ||
| | |_^ | ||
| | | ||
| = help: message: all overloads must have the same receiver kind (&self, &mut self, self, or no receiver), got max_load_factor: &self and max_load_factor: &mut self | ||
|
|
||
| error[E0425]: cannot find type `Q` in this scope | ||
| --> cpp-overload-test/examples/stdcpp-unordered-map.rs:80:38 | ||
| | | ||
| 80 | fn contains<Q>(&self, _key: &Q) -> bool { | ||
| | ^ not found in this scope | ||
| | | ||
| note: similarly named type alias `K` defined here | ||
| --> cpp-overload-test/examples/stdcpp-unordered-map.rs:13:1 | ||
| | | ||
| 13 | type K = std::ffi::c_int; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| help: a type alias with a similar name exists | ||
| | | ||
| 80 - fn contains<Q>(&self, _key: &Q) -> bool { | ||
| 80 + fn contains<Q>(&self, _key: &K) -> bool { | ||
| | | ||
|
|
||
| For more information about this error, try `rustc --explain E0425`. | ||
| error: could not compile `cpp-overload-test` (example "stdcpp-unordered-map") due to 2 previous errors | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.