Commit e2dd39b3 authored by Kenton Varda's avatar Kenton Varda

Extend previous change for flat arrays as well.

parent ae225aa7
......@@ -74,6 +74,14 @@ TEST(Serialize, FlatArray) {
EXPECT_EQ(serialized.end(), reader.getEnd());
}
{
MallocMessageBuilder builder2;
auto remaining = initMessageBuilderFromFlatArrayCopy(serialized, builder2);
checkTestMessage(builder2.getRoot<TestAllTypes>());
EXPECT_EQ(serialized.end(), remaining.begin());
EXPECT_EQ(serialized.end(), remaining.end());
}
kj::Array<word> serializedWithSuffix = kj::heapArray<word>(serialized.size() + 5);
memcpy(serializedWithSuffix.begin(), serialized.begin(), serialized.size() * sizeof(word));
......@@ -82,6 +90,14 @@ TEST(Serialize, FlatArray) {
checkTestMessage(reader.getRoot<TestAllTypes>());
EXPECT_EQ(serializedWithSuffix.end() - 5, reader.getEnd());
}
{
MallocMessageBuilder builder2;
auto remaining = initMessageBuilderFromFlatArrayCopy(serializedWithSuffix, builder2);
checkTestMessage(builder2.getRoot<TestAllTypes>());
EXPECT_EQ(serializedWithSuffix.end() - 5, remaining.begin());
EXPECT_EQ(serializedWithSuffix.end(), remaining.end());
}
}
TEST(Serialize, FlatArrayOddSegmentCount) {
......
......@@ -90,6 +90,13 @@ kj::ArrayPtr<const word> FlatArrayMessageReader::getSegment(uint id) {
}
}
kj::ArrayPtr<const word> initMessageBuilderFromFlatArrayCopy(
kj::ArrayPtr<const word> array, MessageBuilder& target, ReaderOptions options) {
FlatArrayMessageReader reader(array, options);
target.setRoot(reader.getRoot<AnyPointer>());
return kj::arrayPtr(reader.getEnd(), array.end());
}
kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
kj::Array<word> result = kj::heapArray<word>(computeSerializedSizeInWords(segments));
......
......@@ -73,6 +73,21 @@ private:
const word* end;
};
kj::ArrayPtr<const word> initMessageBuilderFromFlatArrayCopy(
kj::ArrayPtr<const word> array, MessageBuilder& target,
ReaderOptions options = ReaderOptions());
// Convenience function which reads a message using `FlatArrayMessageReader` then copies the
// content into the target `MessageBuilder`, verifying that the message structure is valid
// (although not necessarily that it matches the desired schema).
//
// Returns an ArrayPtr containing any words left over in the array after consuming the whole
// message. This is useful when reading multiple messages that have been concatenated. See also
// FlatArrayMessageReader::getEnd().
//
// (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one
// of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not
// safe to use on untrusted input. Therefore, we do not provide a convenience method for it.)
kj::Array<word> messageToFlatArray(MessageBuilder& builder);
// Constructs a flat array containing the entire content of the given message.
//
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment