Skip to content
Merged
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
5 changes: 5 additions & 0 deletions rs_bindings_from_cc/importers/cxx_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,11 @@ std::optional<IR::Item> CXXRecordDeclImporter::Import(
FormattedError::FromStatus(std::move(trait_derives).status()));
}

if (*is_thread_safe) {
trait_derives->send = true;
trait_derives->sync = true;
}

absl::StatusOr<SafetyAnnotation> safety_annotation =
GetSafetyAnnotation(*record_decl);
if (!safety_annotation.ok()) {
Expand Down
29 changes: 29 additions & 0 deletions rs_bindings_from_cc/test/annotations/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,32 @@ crubit_rust_test(
"@crate_index//:googletest",
],
)

crubit_test_cc_library(
name = "thread_safe",
hdrs = ["thread_safe.h"],
deps = [
"//support:annotations",
],
)

crubit_rust_test(
name = "thread_safe_test",
srcs = ["thread_safe_test.rs"],
cc_deps = [
":thread_safe",
],
deps = [
"//support:ctor",
"@crate_index//:googletest",
"@crate_index//:static_assertions", # v1
],
)

golden_test(
name = "thread_safe_golden_test",
basename = "thread_safe",
cc_library = "thread_safe",
golden_cc = "thread_safe_api_impl.cc",
golden_rs = "thread_safe_rs_api.rs",
)
37 changes: 37 additions & 0 deletions rs_bindings_from_cc/test/annotations/thread_safe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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

#ifndef THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_THREAD_SAFE_H_
#define THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_THREAD_SAFE_H_

#include "support/annotations.h"

namespace crubit::test {

// A simple thread-safe struct.
class CRUBIT_THREAD_SAFE ThreadSafeStruct final {
public:
int ConstGet() const { return x_; }
// A non-const method for testing the generation behavior.
// The implementation doesn't actually do anything non-const, but it doesn't
// matter for what we are testing, here.
int NonConstGet() { return x_; }

private:
int x_ = 0;
};

// A regular (non-thread-safe) struct for comparison.
class RegularStruct final {
public:
int ConstGet() const { return x_; }
int NonConstGet() { return x_; }

private:
int x_ = 0;
};

} // namespace crubit::test

#endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_ANNOTATIONS_THREAD_SAFE_H_
70 changes: 70 additions & 0 deletions rs_bindings_from_cc/test/annotations/thread_safe_api_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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

// Automatically @generated Rust bindings for the following C++ target:
// //rs_bindings_from_cc/test/annotations:thread_safe
// Features: fmt, supported, types

#include "support/internal/cxx20_backports.h"
#include "support/internal/offsetof.h"
#include "support/internal/sizeof.h"

#include <cstddef>
#include <memory>

// Public headers of the C++ library being wrapped.
#include "rs_bindings_from_cc/test/annotations/thread_safe.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wthread-safety-analysis"

static_assert(CRUBIT_SIZEOF(class crubit::test::ThreadSafeStruct) == 4);
static_assert(alignof(class crubit::test::ThreadSafeStruct) == 4);

extern "C" void __rust_thunk___ZN6crubit4test16ThreadSafeStructC1Ev(
class crubit::test::ThreadSafeStruct* __this) {
crubit::construct_at(__this);
}

extern "C" int __rust_thunk___ZNK6crubit4test16ThreadSafeStruct8ConstGetEv(
class crubit::test::ThreadSafeStruct const* __this) {
return __this->ConstGet();
}

static_assert((int (::crubit::test::ThreadSafeStruct::*)() const) &
::crubit::test::ThreadSafeStruct::ConstGet);

extern "C" int __rust_thunk___ZN6crubit4test16ThreadSafeStruct11NonConstGetEv(
class crubit::test::ThreadSafeStruct* __this) {
return __this->NonConstGet();
}

static_assert((int (::crubit::test::ThreadSafeStruct::*)()) &
::crubit::test::ThreadSafeStruct::NonConstGet);

static_assert(CRUBIT_SIZEOF(class crubit::test::RegularStruct) == 4);
static_assert(alignof(class crubit::test::RegularStruct) == 4);

extern "C" void __rust_thunk___ZN6crubit4test13RegularStructC1Ev(
class crubit::test::RegularStruct* __this) {
crubit::construct_at(__this);
}

extern "C" int __rust_thunk___ZNK6crubit4test13RegularStruct8ConstGetEv(
class crubit::test::RegularStruct const* __this) {
return __this->ConstGet();
}

static_assert((int (::crubit::test::RegularStruct::*)() const) &
::crubit::test::RegularStruct::ConstGet);

extern "C" int __rust_thunk___ZN6crubit4test13RegularStruct11NonConstGetEv(
class crubit::test::RegularStruct* __this) {
return __this->NonConstGet();
}

static_assert((int (::crubit::test::RegularStruct::*)()) &
::crubit::test::RegularStruct::NonConstGet);

#pragma clang diagnostic pop
Loading
Loading