chenBright opened a new pull request, #3341:
URL: https://github.com/apache/brpc/pull/3341
### What problem does this PR solve?
Issue Number: resolve
Problem Summary:
`IOBuf` integrates with protobuf via `IOBufAsZeroCopy{Input,Output}Stream`,
but
there is no direct bridge to the `std::iostream` hierarchy. As a result, any
library that consumes/produces `std::istream&` / `std::ostream&` — most
notably
`nlohmann::json` — cannot read from or write to an `IOBuf` without an
intermediate `std::string`:
```cpp
auto j = nlohmann::json::parse(req->request_attachment().to_string());
```
This is an obstacle to adopting nlohmann::json in bRPC.
### What is changed and the side effects?
Changed:
Introduce four classes in `src/butil/iobuf.{h,cpp}` that bridge IOBuf and
the standard iostream hierarchy:
- `IOBufAsInputStreamBuf` — read-only `std::streambuf` over an IOBuf,
walking the backing blocks via the public `backing_block()` API. Overrides
`underflow`, `xsgetn`(bulk memcpy across block boundaries to avoid the
default per-byte sbumpc loop), and `showmanyc` (saturating sum of
remaining block sizes).
- `IOBufInputStream` — `std::istream` view that wires the streambuf above.
- `IOBufAsOutputStreamBuf` — append-only `std::streambuf` that delegates to
the existing `IOBufAsZeroCopyOutputStream` for block allocation and the
three-branch `BackUp` (including the shared-block degradation that protects
IOBufs sharing blocks from being corrupted by an in-flight write).
Overrides
`overflow`, `xsputn` (block-sized memcpy instead of per-byte sputc),
and `sync` (forwards to shrink()).
- `IOBufOutputStream` — `std::ostream` view that wires the streambuf above;
accepts an optional block_size to allocate dedicated blocks instead of
drawing from the per-thread TLS pool.
Usage with nlohmann::json:
```c++
// Parse: zero string copy
butil::IOBufInputStream in(request_body);
auto j = nlohmann::json::parse(in);
// Serialize: bytes flow straight into IOBuf blocks
butil::IOBufOutputStream os(response_body);
os << j;
```
Side effects:
- Performance effects:
- Breaking backward compatibility:
---
### Check List:
- Please make sure your changes are compilable.
- When providing us with a new feature, it is best to add related tests.
- Please follow [Contributor Covenant Code of
Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]