Merge pull request #602 from dtolnay/slice

Obtain accurate alignment for empty slice of opaque Rust type
This commit is contained in:
David Tolnay 2020-12-27 13:19:37 -08:00 committed by GitHub
commit b97573b4e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View File

@ -146,6 +146,8 @@ pub(super) fn write(out: &mut OutFile) {
out.next_section();
writeln!(out, "template <typename T>");
writeln!(out, "::std::size_t size_of();");
writeln!(out, "template <typename T>");
writeln!(out, "::std::size_t align_of();");
}
ifndef::write(out, builtin.rust_string, "CXXBRIDGE1_RUST_STRING");

View File

@ -521,7 +521,8 @@ inline std::size_t Str::length() const noexcept { return this->len; }
#ifndef CXXBRIDGE1_RUST_SLICE
#define CXXBRIDGE1_RUST_SLICE
template <typename T>
Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
Slice<T>::Slice() noexcept
: ptr(reinterpret_cast<T *>(align_of<T>())), len(0) {}
template <typename T>
Slice<T>::Slice(T *s, std::size_t count) noexcept : ptr(s), len(count) {}