diff --git a/include/grpc/event_engine/internal/slice_cast.h b/include/grpc/event_engine/internal/slice_cast.h index ab9db95f937..8bcca60a24a 100644 --- a/include/grpc/event_engine/internal/slice_cast.h +++ b/include/grpc/event_engine/internal/slice_cast.h @@ -22,25 +22,25 @@ namespace internal { // Opt-in trait class for slice conversions. // Declare a specialization of this class for any types that are compatible // with `SliceCast`. Both ways need to be declared (i.e. if -// ConstRefSliceCastable exists, you should declare -// ConstRefSliceCastable too). +// SliceCastable exists, you should declare +// SliceCastable too). // The type has no members, it's just the existance of the specialization that // unlocks SliceCast usage for a type pair. template -struct ConstRefSliceCastable; +struct SliceCastable; // This is strictly too wide, but consider all types to be SliceCast-able to // themselves. // Unfortunately this allows `const int& x = SliceCast(x);` which is kind // of bogus. template -struct ConstRefSliceCastable {}; +struct SliceCastable {}; // Cast to `const Result&` from `const T&` without any runtime checks. // This is only valid if `sizeof(Result) == sizeof(T)`, and if `Result`, `T` are -// opted in as compatible via `ConstRefSliceCastable`. +// opted in as compatible via `SliceCastable`. template -const Result& SliceCast(const T& value, ConstRefSliceCastable = {}) { +const Result& SliceCast(const T& value, SliceCastable = {}) { // Insist upon sizes being equal to catch mismatches. // We assume if sizes are opted in and sizes are equal then yes, these two // types are expected to be layout compatible and actually appear to be. @@ -48,6 +48,18 @@ const Result& SliceCast(const T& value, ConstRefSliceCastable = {}) { return reinterpret_cast(value); } +// Cast to `Result&` from `T&` without any runtime checks. +// This is only valid if `sizeof(Result) == sizeof(T)`, and if `Result`, `T` are +// opted in as compatible via `SliceCastable`. +template +Result& SliceCast(T& value, SliceCastable = {}) { + // Insist upon sizes being equal to catch mismatches. + // We assume if sizes are opted in and sizes are equal then yes, these two + // types are expected to be layout compatible and actually appear to be. + static_assert(sizeof(Result) == sizeof(T), "size mismatch"); + return reinterpret_cast(value); +} + } // namespace internal } // namespace experimental } // namespace grpc_event_engine diff --git a/include/grpc/event_engine/slice.h b/include/grpc/event_engine/slice.h index 1e296a21876..bb109230303 100644 --- a/include/grpc/event_engine/slice.h +++ b/include/grpc/event_engine/slice.h @@ -284,9 +284,9 @@ class GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND Slice namespace internal { template <> -struct ConstRefSliceCastable {}; +struct SliceCastable {}; template <> -struct ConstRefSliceCastable {}; +struct SliceCastable {}; } // namespace internal } // namespace experimental diff --git a/src/core/lib/slice/slice.h b/src/core/lib/slice/slice.h index 1199a7af2f7..9742d878c9f 100644 --- a/src/core/lib/slice/slice.h +++ b/src/core/lib/slice/slice.h @@ -410,13 +410,13 @@ namespace grpc_event_engine { namespace experimental { namespace internal { template <> -struct ConstRefSliceCastable {}; +struct SliceCastable {}; template <> -struct ConstRefSliceCastable {}; +struct SliceCastable {}; template <> -struct ConstRefSliceCastable {}; +struct SliceCastable {}; template <> -struct ConstRefSliceCastable {}; +struct SliceCastable {}; } // namespace internal } // namespace experimental } // namespace grpc_event_engine diff --git a/test/core/slice/slice_test.cc b/test/core/slice/slice_test.cc index 0e216aec5cc..451f026388d 100644 --- a/test/core/slice/slice_test.cc +++ b/test/core/slice/slice_test.cc @@ -441,6 +441,15 @@ TEST(SliceTest, SliceCastWorks) { EXPECT_EQ(&other, &test); } +TEST(SliceTest, MutableSliceCastWorks) { + using ::grpc_event_engine::experimental::internal::SliceCast; + Slice test = Slice::FromCopiedString("hello world!"); + grpc_slice& slice = SliceCast(test); + EXPECT_EQ(&slice, &test.c_slice()); + slice = grpc_slice_from_static_string("goodbye world!"); + EXPECT_EQ(test.as_string_view(), "goodbye world!"); +} + } // namespace } // namespace grpc_core