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

Add template deduction guide for Slice constructor #1449

Merged

Conversation

JonathonReinhart
Copy link
Contributor

@JonathonReinhart JonathonReinhart commented Feb 27, 2025

PR #1367 added an explicit Slice constructor which allows constructing from any C++ container.

This adds a C++17 template deduction guide for that constructor which allows the Slice type to be inferred from the container's contained type.

So instead of this:

void Foo(std::span<const uint8_t> data) {
  foo(rust::Slice<const uint8_t>(data));
}

One could write this, omitting the rust::Slice template arguments:

void Foo(std::span<const uint8_t> data) {
  foo(rust::Slice(data));
}

@JonathonReinhart
Copy link
Contributor Author

FYI @anforowicz

@JonathonReinhart JonathonReinhart force-pushed the add-Slice-ctor-deduction-guide branch from 069b1ba to a2d7e2b Compare February 27, 2025 07:04
@JonathonReinhart JonathonReinhart force-pushed the add-Slice-ctor-deduction-guide branch 3 times, most recently from f846bb6 to 8eed1f7 Compare February 27, 2025 17:38
PR dtolnay#1367 added an explicit Slice constructor which allows constructing
from any C++ container.

This adds a template deduction guide for that constructor which allows
the Slice type to be inferred from the container's contained type,
taking into account the const-ness of the type pointed to by data().

This applies only to C++17 and newer.

Test: cargo test
Test: cargo test -F c++14
Test: cargo test -F c++17
Test: cargo test -F c++20
@JonathonReinhart JonathonReinhart force-pushed the add-Slice-ctor-deduction-guide branch from 8eed1f7 to 33f3fa8 Compare February 27, 2025 19:42
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thank you!

@dtolnay dtolnay merged commit 5702ef8 into dtolnay:master Feb 28, 2025
21 checks passed
@dtolnay
Copy link
Owner

dtolnay commented Feb 28, 2025

Published in cxx 1.0.142.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants