Add API to get C slice from C++ Slice.
@ -77,6 +77,9 @@ class Slice GRPC_FINAL {
/// Raw pointer to the end (one byte \em past the last element) of the slice.
const uint8_t* end() const { return GPR_SLICE_END_PTR(slice_); }
/// Raw C slice. Caller needs to call gpr_slice_unref when done.
gpr_slice c_slice() const { return gpr_slice_ref(slice_); }
private:
friend class ByteBuffer;
@ -68,6 +68,16 @@ TEST_F(SliceTest, Empty) {
CheckSlice(empty_slice, "");
}
TEST_F(SliceTest, Cslice) {
gpr_slice s = gpr_slice_from_copied_string(kContent);
Slice spp(s, Slice::STEAL_REF);
CheckSlice(spp, kContent);
gpr_slice c_slice = spp.c_slice();
EXPECT_EQ(GPR_SLICE_START_PTR(s), GPR_SLICE_START_PTR(c_slice));
EXPECT_EQ(GPR_SLICE_END_PTR(s), GPR_SLICE_END_PTR(c_slice));
gpr_slice_unref(c_slice);
} // namespace
} // namespace grpc