Skip to content
Open
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
25 changes: 25 additions & 0 deletions docs/rust/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ If you wish to accept more than one reference/pointer in C++, a raw pointer
(`*const T`, `*mut T`) can be used instead. However, all of the usual `unsafe`
caveats apply.

## Slice parameters

Rust slice parameters (`&[T]`, `&str`) map to `rs_std::SliceRef<const T>` and
`rs_std::StrRef` in C++.

C++ callers can pass string literals (`"hello"`) or `std::string_view` directly
to satisfy string and byte-slice parameters.

```rust {.good}
fn process_text(_: &str) {} // C++ callers can pass "hello" or std::string_view
fn process_bytes(_: &[u8]) {} // C++ callers can pass "hello", std::string_view, or std::span<const uint8_t>
```

However, mutable slices cannot be constructed from string literals:

```rust {.bad}
fn process_mut_bytes(_: &mut [u8]) {} // Cannot pass string literals to mutable byte slices
```

* **`&str`**: Maps to `rs_std::StrRef` (`crubit/support/rs_std/str_ref.h`).
Converts from string literals and `std::string_view`.
* **`&[u8]`**: Maps to `rs_std::SliceRef<const uint8_t>`
(`crubit/support/rs_std/slice_ref.h`). Converts from string literals,
`std::string_view`, `std::vector<uint8_t>`, and `std::array<uint8_t, N>`.

## Generic functions

Crubit doesn't support generating bindings for *arbitrary* generic functions.
Expand Down
33 changes: 20 additions & 13 deletions support/rs_std/slice_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <cstddef>
#include <cstdint>
#include <ranges> // NOLINT(build/c++20); <internal link>
#include <span> // NOLINT(build/c++20); <internal link>
#include <span> // NOLINT(build/c++20); <internal link>
#include <string_view>
#include <type_traits>

Expand All @@ -34,35 +34,42 @@ class CRUBIT_INTERNAL_RUST_TYPE("&[]", T) CRUBIT_TRIVIAL_ABI CRUBIT_VIEW
// To mirror slices in Rust, the data pointer is not null.
constexpr SliceRef() noexcept : dangling_ptr_(alignof(T)), size_(0) {}

// Primary constructor that initializes dangling_ptr_ for empty slices to
// satisfy Rust's non-null slice invariant, or a ptr_ otherwise.
constexpr SliceRef(T* ptr, size_t size) noexcept
: dangling_ptr_(alignof(T)), size_(size) {
if (size > 0 && ptr != nullptr) {
ptr_ = ptr;
}
}

// Style waiver for implicit conversions granted in cl/662479273.
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr SliceRef(std::span<T> span) noexcept
// Store a dangling pointer assuming `span` is empty-- we have to
// initialize the union to something.
: dangling_ptr_(alignof(T)), size_(span.size()) {
// Store a valid pointer when `span` is not empty.
if (!span.empty()) {
ptr_ = span.data();
}
}
: SliceRef(span.data(), span.size()) {}

// Implicit conversion from mutable SliceRef to const SliceRef.
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr SliceRef(const SliceRef<std::remove_const_t<T>>& other) noexcept
requires(std::is_const_v<T>)
: SliceRef(std::span<T>(other.data(), other.size())) {}
: SliceRef(other.data(), other.size()) {}

// Explicit conversion from `std::string_view` in order to avoid
// marking this case as `CRUBIT_LIFETIME_BOUND`.
// Conversion from `std::string_view` for 1-byte const types.
// We explicitly overload to avoid marking this case as CRUBIT_LIFETIME_BOUND.
//
// Note that `std::span` solves this using an `EnableIfIsView` typeclass.
//
// Style waiver for implicit conversions granted in cl/662479273.
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr SliceRef(std::string_view str) noexcept
requires(std::is_const_v<T> && sizeof(T) == 1)
: dangling_ptr_(alignof(T)), size_(str.size()) {
if (!str.empty()) {
ptr_ = str.data();
if constexpr (std::is_same_v<std::remove_cv_t<T>, char>) {
ptr_ = str.data();
} else {
ptr_ = reinterpret_cast<T*>(str.data());
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions support/rs_std/slice_ref_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ TEST(ImplicitConversionTest, ToAbslSpan) {
EXPECT_THAT(span, ElementsAre(1, 2));
}

TEST(ImplicitConversionTest, FromStringViewChar) {
constexpr std::string_view kStr = "hello";
constexpr rs_std::SliceRef<const char> kSlice(kStr);
EXPECT_EQ(kSlice.size(), 5);
EXPECT_EQ(kSlice.data(), kStr.data());
}

TEST(ImplicitConversionTest, FromConstUint8Array) {
static constexpr std::array<uint8_t, 5> kArr = {'h', 'e', 'l', 'l', 'o'};
static constexpr rs_std::SliceRef<const uint8_t> kSlice = kArr;
EXPECT_EQ(kSlice.size(), 5);
EXPECT_EQ(kSlice.to_span()[0], 'h');
}

void Fuzzer(std::vector<uint8_t> data) {
const rs_std::SliceRef<const uint8_t> s = data;
EXPECT_EQ(absl::Span<const uint8_t>(data), s.to_span());
Expand Down