Rename `rs_std::Char` to `rs_std::rs_char`.
PiperOrigin-RevId: 510523253
diff --git a/cc_bindings_from_rs/bazel_support/BUILD b/cc_bindings_from_rs/bazel_support/BUILD
index 1d9cb06..6c339df 100644
--- a/cc_bindings_from_rs/bazel_support/BUILD
+++ b/cc_bindings_from_rs/bazel_support/BUILD
@@ -15,6 +15,6 @@
alias(
name = "cc_deps_for_bindings",
- actual = "//support/rs_std:char",
+ actual = "//support/rs_std:rs_char",
visibility = ["//visibility:public"],
)
diff --git a/cc_bindings_from_rs/bindings.rs b/cc_bindings_from_rs/bindings.rs
index 59ba2a9..d9a5d3a 100644
--- a/cc_bindings_from_rs/bindings.rs
+++ b/cc_bindings_from_rs/bindings.rs
@@ -331,12 +331,12 @@
ty::TyKind::Float(ty::FloatTy::F64) => keyword(quote! { double }),
// ABI compatibility and other details are described in the doc comments in
- // `crubit/support/rs_std/char.h` and `crubit/support/rs_std/char_test.cc` (search for
+ // `crubit/support/rs_std/rs_char.h` and `crubit/support/rs_std/char_test.cc` (search for
// "Layout tests").
ty::TyKind::Char => {
- let rs_char_path = format!("{}/rs_std/char.h", &*input.crubit_support_path);
+ let rs_char_path = format!("{}/rs_std/rs_char.h", &*input.crubit_support_path);
CcSnippet::with_include(
- quote! { rs_std::Char },
+ quote! { rs_std::rs_char },
CcInclude::user_header(rs_char_path.into()),
)
},
@@ -3686,7 +3686,7 @@
("u32", ("std::uint32_t", "<cstdint>", "", "")),
("u64", ("std::uint64_t", "<cstdint>", "", "")),
("usize", ("std::uintptr_t", "<cstdint>", "", "")),
- ("char", ("rs_std::Char", "\"crubit/support/for/tests/rs_std/char.h\"", "", "")),
+ ("char", ("rs_std::rs_char", "\"crubit/support/for/tests/rs_std/rs_char.h\"", "", "")),
("SomeStruct", ("::rust_out::SomeStruct", "", "SomeStruct", "")),
("SomeEnum", ("::rust_out::SomeEnum", "", "SomeEnum", "")),
("SomeUnion", ("::rust_out::SomeUnion", "", "SomeUnion", "")),
diff --git a/cc_bindings_from_rs/test/functions/BUILD b/cc_bindings_from_rs/test/functions/BUILD
index 2d2ef58..c18b809 100644
--- a/cc_bindings_from_rs/test/functions/BUILD
+++ b/cc_bindings_from_rs/test/functions/BUILD
@@ -33,6 +33,6 @@
deps = [
":functions_cc_api",
"@com_google_googletest//:gtest_main",
- "//support/rs_std:char",
+ "//support/rs_std:rs_char",
],
)
diff --git a/cc_bindings_from_rs/test/functions/functions_test.cc b/cc_bindings_from_rs/test/functions/functions_test.cc
index ece07dd..a94b043 100644
--- a/cc_bindings_from_rs/test/functions/functions_test.cc
+++ b/cc_bindings_from_rs/test/functions/functions_test.cc
@@ -5,7 +5,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "cc_bindings_from_rs/test/functions/functions_cc_api.h"
-#include "support/rs_std/char.h"
+#include "support/rs_std/rs_char.h"
namespace crubit {
namespace {
@@ -50,10 +50,10 @@
EXPECT_EQ(12 + 34, fn_param_ty_tests::add_i32(12, 34));
}
-TEST(FnParamTyTests, Char) {
- std::optional<const rs_std::Char> input = rs_std::Char::from_u32(U'🦀');
+TEST(FnParamTyTests, rs_char) {
+ std::optional<const rs_std::rs_char> input = rs_std::rs_char::from_u32(U'🦀');
ASSERT_TRUE(input.has_value());
- rs_std::Char output = fn_param_ty_tests::char_identity(*input);
+ rs_std::rs_char output = fn_param_ty_tests::char_identity(*input);
EXPECT_EQ(input, output);
}
diff --git a/support/rs_std/BUILD b/support/rs_std/BUILD
index 15f65c6..c7cae47 100644
--- a/support/rs_std/BUILD
+++ b/support/rs_std/BUILD
@@ -3,8 +3,8 @@
package(default_applicable_licenses = ["//third_party/crubit:license"])
cc_library(
- name = "char",
- hdrs = ["char.h"],
+ name = "rs_char",
+ hdrs = ["rs_char.h"],
visibility = ["//:__subpackages__"],
# It is important to be thoughtful when adding new dependencies for `char`
@@ -16,10 +16,10 @@
)
cc_test(
- name = "char_test",
- srcs = ["char_test.cc"],
+ name = "rs_char_test",
+ srcs = ["rs_char_test.cc"],
deps = [
- ":char",
+ ":rs_char",
"@com_google_googletest//:gtest_main",
],
)
diff --git a/support/rs_std/README.md b/support/rs_std/README.md
index 34ad798..9b34605 100644
--- a/support/rs_std/README.md
+++ b/support/rs_std/README.md
@@ -6,7 +6,7 @@
This directory contains the `rs_std` C++ library that provides the following
APIs:
- Manually authored APIs that help work with Rust builtin types. For
- example, `rs_std::Char` represents Rust's `char` type (a separate type from
+ example, `rs_std::rs_char` represents Rust's `char` type (a separate type from
C++'s `char32_t` is needed to detect certain invalid bit patterns that result
in Undefined Behavior in Rust; additionally `char32_t` takes at least 32
bits, rather than exactly 32 bits).
diff --git a/support/rs_std/char.h b/support/rs_std/rs_char.h
similarity index 64%
rename from support/rs_std/char.h
rename to support/rs_std/rs_char.h
index 69c21f9..6b22fc5 100644
--- a/support/rs_std/char.h
+++ b/support/rs_std/rs_char.h
@@ -12,22 +12,22 @@
namespace rs_std {
-// `rs_std::Char` is a C++ representation of the `char` type from Rust.
-class Char final {
+// `rs_std::rs_char` is a C++ representation of the `char` type from Rust.
+class rs_char final {
public:
- // Creates a default `Char` - one that represents ASCII NUL character.
+ // Creates a default `rs_char` - one that represents ASCII NUL character.
//
// Providing the default constructor helps to ensure that the `value_` always
// effectively stores a C++ equivalent of a well-defined Rust's `u32` value
// (and never has a `MaybeUninit<u32>` value). See also the P2723R1 proposal
// for C++ which argues that zero-initialization may mitigate 10% of exploits.
- constexpr Char() = default;
+ constexpr rs_char() = default;
- // Converts a `uint32_t` into a `rs_std::Char`.
+ // Converts a `uint32_t` into a `rs_std::rs_char`.
//
- // Note that not all valid `uint32_t`s are valid `rs_std::Char`s. `from_u32`
- // will return `std::nullopt` if the input is not a valid value for a
- // `rs_std::Char`.
+ // Note that not all valid `uint32_t`s are valid `rs_std::rs_char`s.
+ // `from_u32` will return `std::nullopt` if the input is not a valid value for
+ // a `rs_std::rs_char`.
//
// See also
// https://doc.rust-lang.org/reference/behavior-considered-undefined.html
@@ -36,7 +36,7 @@
//
// This function mimics Rust's `char::from_u32`:
// https://doc.rust-lang.org/std/primitive.char.html#method.from_u32
- static constexpr std::optional<Char> from_u32(char32_t c) {
+ static constexpr std::optional<rs_char> from_u32(char32_t c) {
// TODO(lukasza): Consider using slightly more efficient checks similarly
// to how `char_try_from_u32` is implemented in Rust standard library.
if (ABSL_PREDICT_FALSE(c > 0x10ffff)) {
@@ -53,30 +53,30 @@
return from_u32_unchecked(c);
}
- constexpr Char(const Char&) = default;
- constexpr Char& operator=(const Char&) = default;
- constexpr Char(Char&&) = default;
- constexpr Char& operator=(Char&&) = default;
- ~Char() = default;
+ constexpr rs_char(const rs_char&) = default;
+ constexpr rs_char& operator=(const rs_char&) = default;
+ constexpr rs_char(rs_char&&) = default;
+ constexpr rs_char& operator=(rs_char&&) = default;
+ ~rs_char() = default;
explicit constexpr operator std::uint32_t() const { return value_; }
- constexpr bool operator==(const Char& other) const {
+ constexpr bool operator==(const rs_char& other) const {
return value_ == other.value_;
}
- constexpr bool operator!=(const Char& other) const {
+ constexpr bool operator!=(const rs_char& other) const {
return value_ != other.value_;
}
- constexpr bool operator<=(const Char& other) const {
+ constexpr bool operator<=(const rs_char& other) const {
return value_ <= other.value_;
}
- constexpr bool operator<(const Char& other) const {
+ constexpr bool operator<(const rs_char& other) const {
return value_ < other.value_;
}
- constexpr bool operator>=(const Char& other) const {
+ constexpr bool operator>=(const rs_char& other) const {
return value_ >= other.value_;
}
- constexpr bool operator>(const Char& other) const {
+ constexpr bool operator>(const rs_char& other) const {
return value_ > other.value_;
}
@@ -84,7 +84,7 @@
//
// This constant mimics Rust's `char::MAX`:
// https://doc.rust-lang.org/std/primitive.char.html#associatedconstant.MAX
- static const Char MAX;
+ static const rs_char MAX;
private:
// This function mimics Rust's `char::from_u32_unchecked`:
@@ -92,23 +92,23 @@
//
// TODO(b/254095482): Figure out how to annotate/expose unsafe functions in
// C++ and then make this method public.
- static constexpr Char from_u32_unchecked(std::uint32_t value) {
- return Char(value);
+ static constexpr rs_char from_u32_unchecked(std::uint32_t value) {
+ return rs_char(value);
}
// Private constructor - intended to only be used from `from_u32_unchecked`.
- explicit constexpr Char(std::uint32_t value) : value_(value) {}
+ explicit constexpr rs_char(std::uint32_t value) : value_(value) {}
// See "layout tests" comments in `char_test.cc` for explanation why
// `char32_t` is not used.
std::uint32_t value_ = '\0';
};
-// Definition of `Char::MAX` - it can't be defined and declared within the
-// `class` definition, because before `Char` is fully defined the compiler
+// Definition of `rs_char::MAX` - it can't be defined and declared within the
+// `class` definition, because before `rs_char` is fully defined the compiler
// complains that `constexpr` variable cannot have non-literal type
-// 'const Char'.
-constexpr Char Char::MAX = Char::from_u32_unchecked(0x10ffff);
+// 'const rs_char'.
+constexpr rs_char rs_char::MAX = rs_char::from_u32_unchecked(0x10ffff);
} // namespace rs_std
diff --git a/support/rs_std/char_test.cc b/support/rs_std/rs_char_test.cc
similarity index 70%
rename from support/rs_std/char_test.cc
rename to support/rs_std/rs_char_test.cc
index de911c8..ff84fee 100644
--- a/support/rs_std/char_test.cc
+++ b/support/rs_std/rs_char_test.cc
@@ -2,7 +2,7 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-#include "support/rs_std/char.h"
+#include "support/rs_std/rs_char.h"
#include <stdint.h>
@@ -13,15 +13,16 @@
namespace {
-// Check that `rs_std::Char` is trivially destructible, copyable, and moveable.
+// Check that `rs_std::rs_char` is trivially destructible, copyable, and
+// moveable.
//
// There are no constructor-related checks, because well-formed-ness checks
-// require going through factory methods like `Char::from_u32`.
-static_assert(std::is_trivially_destructible_v<rs_std::Char>);
-static_assert(std::is_trivially_copy_constructible_v<rs_std::Char>);
-static_assert(std::is_trivially_copy_assignable_v<rs_std::Char>);
-static_assert(std::is_trivially_move_constructible_v<rs_std::Char>);
-static_assert(std::is_trivially_move_assignable_v<rs_std::Char>);
+// require going through factory methods like `rs_char::from_u32`.
+static_assert(std::is_trivially_destructible_v<rs_std::rs_char>);
+static_assert(std::is_trivially_copy_constructible_v<rs_std::rs_char>);
+static_assert(std::is_trivially_copy_assignable_v<rs_std::rs_char>);
+static_assert(std::is_trivially_move_constructible_v<rs_std::rs_char>);
+static_assert(std::is_trivially_move_assignable_v<rs_std::rs_char>);
// Layout tests.
//
@@ -34,9 +35,9 @@
// builtin `char32_t` type "has the same size, signedness, and alignment as
// std::uint_least32_t" (and therefore it is not guaranteed to be exactly
// 32-bits wide as required for ABI-compatibility with Rust).
-static_assert(sizeof(rs_std::Char) == 4);
-static_assert(alignof(rs_std::Char) == 4);
-static_assert(std::is_standard_layout_v<rs_std::Char>);
+static_assert(sizeof(rs_std::rs_char) == 4);
+static_assert(alignof(rs_std::rs_char) == 4);
+static_assert(std::is_standard_layout_v<rs_std::rs_char>);
// This test covers the following case from
// https://en.cppreference.com/w/cpp/language/character_literal:
@@ -46,7 +47,7 @@
// - the representation of c-char in the execution character set (until C++23)
// - the corresponding code point from ordinary literal encoding (since C++23).
TEST(RsCharTest, FromAsciiLiteral) {
- std::optional<const rs_std::Char> c = rs_std::Char::from_u32('x');
+ std::optional<const rs_std::rs_char> c = rs_std::rs_char::from_u32('x');
ASSERT_TRUE(c.has_value());
EXPECT_EQ(0x78, uint32_t{*c});
}
@@ -60,7 +61,7 @@
// with a single UTF-8 code unit (that is, c-char is in the range 0x0-0x7F,
// inclusive).
TEST(RsCharTest, FromUtf8Literal) {
- std::optional<const rs_std::Char> c = rs_std::Char::from_u32(u8'x');
+ std::optional<const rs_std::rs_char> c = rs_std::rs_char::from_u32(u8'x');
ASSERT_TRUE(c.has_value());
EXPECT_EQ(0x78, uint32_t{*c});
}
@@ -74,7 +75,7 @@
// single UTF-16 code unit (that is, c-char is in the range 0x0-0xFFFF,
// inclusive).
TEST(RsCharTest, FromUtf16Literal) {
- std::optional<const rs_std::Char> c = rs_std::Char::from_u32(u'Ł');
+ std::optional<const rs_std::rs_char> c = rs_std::rs_char::from_u32(u'Ł');
ASSERT_TRUE(c.has_value());
EXPECT_EQ(0x141, uint32_t{*c});
}
@@ -85,52 +86,52 @@
// UTF-32 character literal, e.g. U'猫' or U'🍌'. Such literal has type
// `char32_t` and the value equal to ISO/IEC 10646 code point value of c-char.
TEST(RsCharTest, FromUtf32Literal) {
- std::optional<const rs_std::Char> c = rs_std::Char::from_u32(U'🦀');
+ std::optional<const rs_std::rs_char> c = rs_std::rs_char::from_u32(U'🦀');
ASSERT_TRUE(c.has_value());
EXPECT_EQ(0x1F980, uint32_t{*c});
}
TEST(RsCharTest, FromU32ValidityChecks) {
// Max 32-bit value.
- EXPECT_FALSE(rs_std::Char::from_u32(0xffffffff).has_value());
+ EXPECT_FALSE(rs_std::rs_char::from_u32(0xffffffff).has_value());
// A value just above Rust's `char::MAX`:
// https://doc.rust-lang.org/std/primitive.char.html#associatedconstant.MAX.
- EXPECT_FALSE(rs_std::Char::from_u32(0x110000).has_value());
+ EXPECT_FALSE(rs_std::rs_char::from_u32(0x110000).has_value());
// Smallest/greatest "high"/"low" surrogates.
- EXPECT_FALSE(rs_std::Char::from_u32(0xd800).has_value());
- EXPECT_FALSE(rs_std::Char::from_u32(0xdbff).has_value());
- EXPECT_FALSE(rs_std::Char::from_u32(0xdc00).has_value());
- EXPECT_FALSE(rs_std::Char::from_u32(0xdfff).has_value());
+ EXPECT_FALSE(rs_std::rs_char::from_u32(0xd800).has_value());
+ EXPECT_FALSE(rs_std::rs_char::from_u32(0xdbff).has_value());
+ EXPECT_FALSE(rs_std::rs_char::from_u32(0xdc00).has_value());
+ EXPECT_FALSE(rs_std::rs_char::from_u32(0xdfff).has_value());
// Smallest valid value.
- std::optional<rs_std::Char> maybe_c = rs_std::Char::from_u32('\0');
+ std::optional<rs_std::rs_char> maybe_c = rs_std::rs_char::from_u32('\0');
ASSERT_TRUE(maybe_c.has_value());
EXPECT_EQ(0x00, uint32_t{*maybe_c});
// Greatest valid value. See also Rust's `char::MAX`:
// https://doc.rust-lang.org/std/primitive.char.html#associatedconstant.MAX.
- maybe_c = rs_std::Char::from_u32(0x10ffff);
+ maybe_c = rs_std::rs_char::from_u32(0x10ffff);
ASSERT_TRUE(maybe_c.has_value());
EXPECT_EQ(0x10ffff, uint32_t{*maybe_c});
// Just below surrogates.
- maybe_c = rs_std::Char::from_u32(0xd7ff);
+ maybe_c = rs_std::rs_char::from_u32(0xd7ff);
ASSERT_TRUE(maybe_c.has_value());
EXPECT_EQ(0xd7ff, uint32_t{*maybe_c});
// Just above surrogates.
- maybe_c = rs_std::Char::from_u32(0xe000);
+ maybe_c = rs_std::rs_char::from_u32(0xe000);
ASSERT_TRUE(maybe_c.has_value());
EXPECT_EQ(0xe000, uint32_t{*maybe_c});
}
-// Test that `rs_std::Char` values can be compared with other `rs_std::Char`
-// values.
+// Test that `rs_std::rs_char` values can be compared with other
+// `rs_std::rs_char` values.
TEST(RsCharTest, ComparisonWithAnotherRsChar) {
- std::optional<const rs_std::Char> a = rs_std::Char::from_u32('a');
- std::optional<const rs_std::Char> b = rs_std::Char::from_u32('b');
+ std::optional<const rs_std::rs_char> a = rs_std::rs_char::from_u32('a');
+ std::optional<const rs_std::rs_char> b = rs_std::rs_char::from_u32('b');
ASSERT_TRUE(a.has_value());
ASSERT_TRUE(b.has_value());
@@ -157,7 +158,7 @@
}
TEST(RsCharTest, DefaultConstructedValue) {
- rs_std::Char c;
+ rs_std::rs_char c;
EXPECT_EQ(0, uint32_t{c});
}