Expand SliceCast to support casts to MutableSlice type (#31996)

* Expand SliceCast to support casts to MutableSlice type

* update
pull/31856/merge
Vignesh Babu 2 years ago committed by GitHub
parent a2cf991f08
commit 4f9ba0f49b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      include/grpc/event_engine/slice.h
  2. 7
      include/grpc/event_engine/slice_buffer.h
  3. 15
      src/core/lib/slice/slice.h
  4. 13
      test/core/slice/slice_test.cc

@ -178,8 +178,9 @@ struct CopyConstructors {
} // namespace slice_detail
class MutableSlice : public slice_detail::BaseSlice,
public slice_detail::CopyConstructors<MutableSlice> {
class GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND MutableSlice
: public slice_detail::BaseSlice,
public slice_detail::CopyConstructors<MutableSlice> {
public:
MutableSlice() = default;
explicit MutableSlice(const grpc_slice& slice);
@ -287,6 +288,16 @@ template <>
struct SliceCastable<Slice, grpc_slice> {};
template <>
struct SliceCastable<grpc_slice, Slice> {};
template <>
struct SliceCastable<MutableSlice, grpc_slice> {};
template <>
struct SliceCastable<grpc_slice, MutableSlice> {};
template <>
struct SliceCastable<MutableSlice, Slice> {};
template <>
struct SliceCastable<Slice, MutableSlice> {};
} // namespace internal
} // namespace experimental

@ -119,10 +119,17 @@ class SliceBuffer {
/// associated slice.
Slice RefSlice(size_t index);
/// Array access into the SliceBuffer. It returns a non mutable reference to
/// the slice at the specified index
const Slice& operator[](size_t index) const {
return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
}
/// Return mutable reference to the slice at the specified index
Slice& MutableSliceAt(size_t index) const {
return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
}
/// The total number of bytes held by the SliceBuffer
size_t Length() { return slice_buffer_.length; }

@ -259,8 +259,9 @@ class StaticSlice : public slice_detail::BaseSlice,
}
};
class MutableSlice : public slice_detail::BaseSlice,
public slice_detail::CopyConstructors<MutableSlice> {
class GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND MutableSlice
: public slice_detail::BaseSlice,
public slice_detail::CopyConstructors<MutableSlice> {
public:
MutableSlice() = default;
explicit MutableSlice(const grpc_slice& slice)
@ -417,6 +418,16 @@ template <>
struct SliceCastable<grpc_core::Slice, Slice> {};
template <>
struct SliceCastable<Slice, grpc_core::Slice> {};
template <>
struct SliceCastable<grpc_core::MutableSlice, grpc_slice> {};
template <>
struct SliceCastable<grpc_slice, grpc_core::MutableSlice> {};
template <>
struct SliceCastable<grpc_core::MutableSlice, grpc_core::Slice> {};
template <>
struct SliceCastable<grpc_core::Slice, grpc_core::MutableSlice> {};
} // namespace internal
} // namespace experimental
} // namespace grpc_event_engine

@ -448,6 +448,19 @@ TEST(SliceTest, MutableSliceCastWorks) {
EXPECT_EQ(&slice, &test.c_slice());
slice = grpc_slice_from_static_string("goodbye world!");
EXPECT_EQ(test.as_string_view(), "goodbye world!");
MutableSlice& m_cpp_slice = SliceCast<MutableSlice>(test);
EXPECT_EQ(&m_cpp_slice.c_slice(), &test.c_slice());
m_cpp_slice = MutableSlice::FromCopiedString("hello world again!");
// Change the first byte.
m_cpp_slice[0] = 'e';
EXPECT_EQ(test.as_string_view(), "eello world again!");
MutableSlice& m_c_slice = SliceCast<MutableSlice>(slice);
EXPECT_EQ(&m_c_slice.c_slice(), &slice);
// Restore the first byte.
GRPC_SLICE_START_PTR(slice)[0] = 'h';
EXPECT_EQ(m_c_slice.as_string_view(), "hello world again!");
}
} // namespace

Loading…
Cancel
Save