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
24 changes: 24 additions & 0 deletions rs_bindings_from_cc/test/struct/unsafe_attributes/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""End-to-end example of using unsafe attributes on structs."""

load("//common:crubit_wrapper_macros_oss.bzl", "crubit_rust_test")
load("//rs_bindings_from_cc/test:test_bindings.bzl", "crubit_test_cc_library")

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

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

crubit_rust_test(
name = "main",
srcs = ["test.rs"],
cc_deps = [":unsafe_attributes"],
deps = [
"@crate_index//:googletest",
],
)
23 changes: 23 additions & 0 deletions rs_bindings_from_cc/test/struct/unsafe_attributes/test.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

#![deny(warnings)]

use unsafe_attributes::*;

#[googletest::gtest]
#[deny(unsafe_code)]
fn test_safe_struct() {
UseSafeStructUnannotated(SafeStructUnannotated::default());
}

#[googletest::gtest]
#[deny(unused_unsafe)]
fn test_unsafe_struct() {
unsafe { UseSafeStructAnnotatedUnsafe(SafeStructAnnotatedUnsafe::default()) };
unsafe { UseUnsafeStructUnannotated(UnsafeStructUnannotated::default()) };

// TODO(b/481018055): this should not be unsafe.
unsafe { UseUnsafeStructAnnotatedSafe(UnsafeStructAnnotatedSafe::default()) };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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 "rs_bindings_from_cc/test/struct/unsafe_attributes/unsafe_attributes.h"

void UseSafeStructUnannotated(SafeStructUnannotated s) {}
void UseSafeStructAnnotatedUnsafe(SafeStructAnnotatedUnsafe s) {}
void UseUnsafeStructUnannotated(UnsafeStructUnannotated s) {}
void UseUnsafeStructAnnotatedSafe(UnsafeStructAnnotatedSafe s) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 CRUBIT_RS_BINDINGS_FROM_CC_TEST_STRUCT_UNSAFE_ATTRIBUTES_H_
#define CRUBIT_RS_BINDINGS_FROM_CC_TEST_STRUCT_UNSAFE_ATTRIBUTES_H_

#include "support/annotations.h"

struct SafeStructUnannotated {};

struct CRUBIT_UNSAFE SafeStructAnnotatedUnsafe {};

struct UnsafeStructUnannotated {
public:
void* ptr = nullptr;
};

struct CRUBIT_UNSAFE_MARK_SAFE UnsafeStructAnnotatedSafe {
public:
void* ptr = nullptr;
};

void UseSafeStructUnannotated(SafeStructUnannotated s);
void UseSafeStructAnnotatedUnsafe(SafeStructAnnotatedUnsafe s);

void UseUnsafeStructUnannotated(UnsafeStructUnannotated s);
void UseUnsafeStructAnnotatedSafe(UnsafeStructAnnotatedSafe s);

#endif // CRUBIT_RS_BINDINGS_FROM_CC_TEST_STRUCT_UNSAFE_ATTRIBUTES_H_