| 30 | } |
| 31 | |
| 32 | size_t Serializer::writeToBuffer(std::unique_ptr<folly::IOBuf> ioBuf) { |
| 33 | XDCHECK_LT(reinterpret_cast<uintptr_t>(curr_), |
| 34 | reinterpret_cast<uintptr_t>(end_)); |
| 35 | if (!ioBuf) { |
| 36 | throw std::invalid_argument("IOBuf is nullptr"); |
| 37 | } |
| 38 | ioBuf->coalesce(); // coalesece the chain of IOBufs into one single IOBuf |
| 39 | const auto length = ioBuf->length(); |
| 40 | if (bytesRemaining() < length) { |
| 41 | throw std::length_error( |
| 42 | fmt::format("Buffer insufficient for serialization." |
| 43 | "Has {} bytes left. Need {} bytes.", |
| 44 | bytesRemaining(), length)); |
| 45 | } |
| 46 | memcpy(curr_, ioBuf->data(), length); |
| 47 | curr_ += length; |
| 48 | return length; |
| 49 | } |
| 50 | |
| 51 | Deserializer::Deserializer(const uint8_t* begin, const uint8_t* const end) |
| 52 | : curr_(begin), end_(end) {} |