Skip to content

Commit f019e0a

Browse files
anforowiczcopybara-github
authored andcommitted
Example of constructing TuplePayload variants of enums.
PiperOrigin-RevId: 878549100
1 parent 47f3cac commit f019e0a

5 files changed

Lines changed: 220 additions & 1 deletion

File tree

docs/rust/enums.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,24 @@ with the corresponding variant.
5656

5757
### Example
5858

59-
TODO(b/489757240): Add an example.
59+
Given the following Rust crate:
60+
61+
```
62+
{{ #include ../../examples/rust/enum_with_payload/example.rs }}
63+
```
64+
<!-- class:Color -->
65+
66+
67+
Crubit will generate the following bindings:
68+
69+
<!-- TODO(b/487357254): Add an example for StructPayloadVariant constructors
70+
once available. -->
71+
72+
```
73+
{{ #include ../../examples/rust/enum_with_payload/example_generated.h }}
74+
```
75+
<!-- snippet:comment,block function:MakeRgb -->
76+
6077

6178
## Known issues
6279

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
2+
load(
3+
"@rules_rust//rust:defs.bzl",
4+
"rust_library",
5+
)
6+
load(
7+
"//cc_bindings_from_rs/bazel_support:cc_bindings_from_rust_rule.bzl",
8+
"cc_bindings_from_rust",
9+
)
10+
load(
11+
"//cc_bindings_from_rs/test/golden:golden_test.bzl",
12+
"golden_test",
13+
)
14+
15+
package(default_applicable_licenses = ["//:license"])
16+
17+
licenses(["notice"])
18+
19+
rust_library(
20+
name = "example_crate",
21+
srcs = ["example.rs"],
22+
)
23+
24+
cc_bindings_from_rust(
25+
name = "example_crate_cc_api",
26+
crate = ":example_crate",
27+
)
28+
29+
cc_binary(
30+
name = "main",
31+
srcs = ["main.cc"],
32+
deps = [":example_crate_cc_api"],
33+
)
34+
35+
golden_test(
36+
name = "example_golden_test",
37+
golden_h = "example_generated.h",
38+
rust_library = "example_crate",
39+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Part of the Crubit project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
#[derive(Copy, Clone)]
6+
pub enum Color {
7+
/// A completely transparent color (no payload)
8+
Transparent,
9+
/// A grayscale value from 0 to 255
10+
Grayscale(u8),
11+
/// Red, Green, and Blue values from 0 to 255
12+
Rgb(u8, u8, u8),
13+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Part of the Crubit project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
// Automatically @generated C++ bindings for the following Rust crate:
6+
// example_crate_golden
7+
// Features: supported, types
8+
9+
// clang-format off
10+
#ifndef THIRD_PARTY_CRUBIT_EXAMPLES_RUST_ENUM_WITH_PAYLOAD_EXAMPLE_CRATE_GOLDEN
11+
#define THIRD_PARTY_CRUBIT_EXAMPLES_RUST_ENUM_WITH_PAYLOAD_EXAMPLE_CRATE_GOLDEN
12+
13+
#pragma clang diagnostic push
14+
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
15+
#pragma clang diagnostic ignored "-Wunused-private-field"
16+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
17+
#include "support/annotations_internal.h"
18+
#include "support/internal/slot.h"
19+
20+
#include <array>
21+
#include <cstddef>
22+
#include <cstdint>
23+
#include <cstring>
24+
#include <type_traits>
25+
#include <utility>
26+
27+
namespace example_crate {
28+
29+
// Generated from:
30+
// examples/rust/enum_with_payload/example.rs;l=6
31+
struct CRUBIT_INTERNAL_RUST_TYPE(":: example_crate_golden :: Color") alignas(1)
32+
[[clang::trivial_abi]] Color final {
33+
public:
34+
// `example_crate_golden::Color` doesn't implement the `Default` trait
35+
Color() = delete;
36+
37+
// A completely transparent color (no payload)
38+
//
39+
// Generated from:
40+
// examples/rust/enum_with_payload/example.rs;l=8
41+
static constexpr Color MakeTransparent();
42+
43+
// A grayscale value from 0 to 255
44+
//
45+
// Generated from:
46+
// examples/rust/enum_with_payload/example.rs;l=10
47+
static ::example_crate::Color MakeGrayscale(std::uint8_t __param_0);
48+
49+
// Red, Green, and Blue values from 0 to 255
50+
//
51+
// Generated from:
52+
// examples/rust/enum_with_payload/example.rs;l=12
53+
static ::example_crate::Color MakeRgb(std::uint8_t __param_0,
54+
std::uint8_t __param_1,
55+
std::uint8_t __param_2);
56+
57+
// No custom `Drop` impl and no custom "drop glue" required
58+
~Color() = default;
59+
Color(Color&&) = default;
60+
::example_crate::Color& operator=(Color&&) = default;
61+
62+
// Rust types that are `Copy` get trivial, `default` C++ copy constructor and
63+
// assignment operator.
64+
Color(const Color&) = default;
65+
::example_crate::Color& operator=(const Color&) = default;
66+
Color(::crubit::UnsafeRelocateTag, Color&& value) {
67+
std::memcpy(this, &value, sizeof(value));
68+
}
69+
70+
private:
71+
// Field type has been replaced with a blob of bytes: No support for bindings
72+
// of individual non-repr(C) `enum`s
73+
std::array<unsigned char, 4> __opaque_blob_of_bytes;
74+
75+
private:
76+
struct PrivateBytesTag {};
77+
constexpr Color(PrivateBytesTag, std::array<unsigned char, 4> bytes)
78+
: __opaque_blob_of_bytes(bytes) {}
79+
80+
private:
81+
static void __crubit_field_offset_assertions();
82+
};
83+
84+
static_assert(
85+
sizeof(Color) == 4,
86+
"Verify that ADT layout didn't change since this header got generated");
87+
static_assert(
88+
alignof(Color) == 1,
89+
"Verify that ADT layout didn't change since this header got generated");
90+
91+
// `static` constructor
92+
inline constexpr Color Color::MakeTransparent() {
93+
return Color(PrivateBytesTag{}, {0, 0, 0, 0});
94+
}
95+
96+
namespace __crubit_internal {
97+
extern "C" void __crubit_thunk_Grayscale(std::uint8_t,
98+
::example_crate::Color* __ret_ptr);
99+
}
100+
inline ::example_crate::Color Color::MakeGrayscale(std::uint8_t __param_0) {
101+
crubit::Slot<::example_crate::Color> __return_value_ret_val_holder;
102+
auto* __return_value_storage = __return_value_ret_val_holder.Get();
103+
__crubit_internal::__crubit_thunk_Grayscale(__param_0,
104+
__return_value_storage);
105+
return std::move(__return_value_ret_val_holder).AssumeInitAndTakeValue();
106+
}
107+
108+
namespace __crubit_internal {
109+
extern "C" void __crubit_thunk_Rgb(std::uint8_t, std::uint8_t, std::uint8_t,
110+
::example_crate::Color* __ret_ptr);
111+
}
112+
inline ::example_crate::Color Color::MakeRgb(std::uint8_t __param_0,
113+
std::uint8_t __param_1,
114+
std::uint8_t __param_2) {
115+
crubit::Slot<::example_crate::Color> __return_value_ret_val_holder;
116+
auto* __return_value_storage = __return_value_ret_val_holder.Get();
117+
__crubit_internal::__crubit_thunk_Rgb(__param_0, __param_1, __param_2,
118+
__return_value_storage);
119+
return std::move(__return_value_ret_val_holder).AssumeInitAndTakeValue();
120+
}
121+
static_assert(std::is_trivially_destructible_v<Color>);
122+
static_assert(std::is_trivially_move_constructible_v<::example_crate::Color>);
123+
static_assert(std::is_trivially_move_assignable_v<::example_crate::Color>);
124+
static_assert(std::is_trivially_copy_constructible_v<::example_crate::Color>);
125+
static_assert(std::is_trivially_copy_assignable_v<::example_crate::Color>);
126+
inline void Color::__crubit_field_offset_assertions() {
127+
static_assert(0 == offsetof(Color, __opaque_blob_of_bytes));
128+
}
129+
} // namespace example_crate
130+
131+
#pragma clang diagnostic pop
132+
#endif // THIRD_PARTY_CRUBIT_EXAMPLES_RUST_ENUM_WITH_PAYLOAD_EXAMPLE_CRATE_GOLDEN
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Part of the Crubit project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
#include <utility>
6+
7+
#include "examples/rust/enum_with_payload/example_crate.h"
8+
9+
int main(int argc, char* argv[]) {
10+
example_crate::Color transparent_color =
11+
example_crate::Color::MakeTransparent();
12+
13+
example_crate::Color red_color = example_crate::Color::MakeRgb(255, 0, 0);
14+
15+
std::ignore = transparent_color;
16+
std::ignore = red_color;
17+
return 0;
18+
}

0 commit comments

Comments
 (0)