Change `rstd::Char` into a `class`.
Before this CL, `rstd::Char` was a type alias for `std::uint32_t`.
After this CL `rstd::Char` is a separate `class` (that privately wraps
`std::uint32_t`). This change is a step toward a follow-up CL that will
start rejecting invalid bit patterns in constructors of `rstd::Char`.
PiperOrigin-RevId: 502991306
diff --git a/support/rstd/char.h b/support/rstd/char.h
index 2270ba0..d4c1450 100644
--- a/support/rstd/char.h
+++ b/support/rstd/char.h
@@ -10,13 +10,50 @@
namespace rstd {
// `rstd::Char` is a C++ representation of the `char` type from Rust.
-//
-// See "layout tests" comments in `char_test.cc` for explanation why `char32_t`
-// is not used.
-//
-// TODO(b/265338802): Reject `char` values with invalid bit patterns (possibly
-// retaining `constexpr` aspect of some conversions).
-using Char = std::uint32_t;
+class Char final {
+ public:
+ // TODO(b/265338802): Reject `char` values that may represent a part of a
+ // UTF-8 character (i.e. only the first 0-127 ASCII characters should be
+ // accepted).
+ constexpr explicit Char(char c) : value_(c) {}
+
+ // TODO(b/265338802): Reject `char` values with invalid bit patterns
+ // (retaining the `constexpr` aspect if possible).
+ constexpr explicit Char(char16_t c) : value_(c) {}
+ constexpr explicit Char(char32_t c) : value_(c) {}
+
+ constexpr Char(const Char&) = default;
+ constexpr Char& operator=(const Char&) = default;
+ constexpr Char(Char&&) = default;
+ constexpr Char& operator=(Char&&) = default;
+ ~Char() = default;
+
+ explicit constexpr operator std::uint32_t() const { return value_; }
+
+ constexpr bool operator==(const Char& other) const {
+ return value_ == other.value_;
+ }
+ constexpr bool operator!=(const Char& other) const {
+ return value_ != other.value_;
+ }
+ constexpr bool operator<=(const Char& other) const {
+ return value_ <= other.value_;
+ }
+ constexpr bool operator<(const Char& other) const {
+ return value_ < other.value_;
+ }
+ constexpr bool operator>=(const Char& other) const {
+ return value_ >= other.value_;
+ }
+ constexpr bool operator>(const Char& other) const {
+ return value_ > other.value_;
+ }
+
+ private:
+ // See "layout tests" comments in `char_test.cc` for explanation why
+ // `char32_t` is not used.
+ std::uint32_t value_;
+};
} // namespace rstd