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

Ergonomics: allow constructing rust::Slice from any C++ container. #1367

Merged
merged 1 commit into from
Aug 14, 2024

Conversation

anforowicz
Copy link
Contributor

After this commit, it is possible to explicitly construct rust::Slice from a reference to any continguous C++ container (any container that exposes data and size accessors).

The new constructor results in a slightly more ergonomic code, by removing the need to explicit extract and pass c.data() and c.size() at a callsite of a rust::Slice constructor. The callsites using the new constructor are also more obviously correct, because they doesn't require double-checking that the passed data and size match.

The implementation of the new constructor mimics std::span from C++20, but for C++11 compatibility reimplements std::size / std::ranges::size in a pedestrian way (same for std::data / std::ranges::data).

After this commit, it is possible to explicitly construct `rust::Slice`
from a reference to any continguous C++ container (any container that
exposes `data` and `size` accessors).

The new constructor results in a slightly more ergonomic code, by
removing the need to explicit extract and pass `c.data()` and `c.size()`
at a callsite of a `rust::Slice` constructor.  The callsites using the
new constructor are also more obviously correct, because they doesn't
require double-checking that the passed `data` and `size` match.

The implementation of the new constructor mimics `std::span` from C++20,
but for C++11 compatibility reimplements `std::size` /
`std::ranges::size` in a pedestrian way (same for `std::data` /
`std::ranges::data`).
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.

Thanks!

@dtolnay dtolnay merged commit 6c8ac69 into dtolnay:master Aug 14, 2024
16 checks passed
@anforowicz anforowicz deleted the rust-slice-from-vec branch August 14, 2024 18:25
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.

2 participants