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
34 changes: 34 additions & 0 deletions cc_bindings_from_rs/test/string/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""End-to-end tests of `cc_bindings_from_rs`, focusing on Rust String bindings."""

load(
"@rules_rust//rust:defs.bzl",
"rust_library",
)
load(
"//cc_bindings_from_rs/bazel_support:cc_bindings_from_rust_rule.bzl",
"cc_bindings_from_rust",
)
load("//common:crubit_wrapper_macros_oss.bzl", "crubit_cc_test")

package(default_applicable_licenses = ["//:license"])

rust_library(
name = "string",
srcs = ["string.rs"],
)

cc_bindings_from_rust(
name = "string_cc_api",
crate = ":string",
)

crubit_cc_test(
name = "string_test",
srcs = ["string_test.cc"],
deps = [
":string_cc_api",
"//support/rs_std",
"//support/rs_std:rs_alloc",
"//testing/base/public:gunit_main",
],
)
23 changes: 23 additions & 0 deletions cc_bindings_from_rs/test/string/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

//! End-to-end tests of `cc_bindings_from_rs`, focusing on `String` bindings.

pub fn roundtrip_rust_string(val: String) -> String {
val
}

pub fn compute_rust_string_length(val: String) -> usize {
val.len()
}

// The point of this test is to generate bindings for `&String` and not `&str`.
#[allow(clippy::ptr_arg)]
pub fn compute_rust_string_ref_length(val: &String) -> usize {
val.len()
}

pub fn append_to_rust_string(val: &mut String, s: &str) {
val.push_str(s);
}
35 changes: 35 additions & 0 deletions cc_bindings_from_rs/test/string/string_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "cc_bindings_from_rs/test/string/string.h"

#include <utility>

#include "gtest/gtest.h"
#include "support/rs_std/rs_std.h"

namespace crubit {
namespace {

TEST(RustStringBridging, Basic) {
rs::std::string::String s("hello world");
EXPECT_EQ(string::compute_rust_string_length(std::move(s)), 11);

rs::std::string::String s2("hello world from Rust");
rs::std::string::String roundtripped =
string::roundtrip_rust_string(std::move(s2));
EXPECT_EQ(string::compute_rust_string_length(std::move(roundtripped)), 21);
}

TEST(RustStringBridging, References) {
rs::std::string::String s("hello");
EXPECT_EQ(string::compute_rust_string_ref_length(s), 5);

string::append_to_rust_string(s, " appended");
EXPECT_EQ(string::compute_rust_string_ref_length(s), 14);
EXPECT_EQ(s.as_str(), "hello appended");
}

} // namespace
} // namespace crubit