+1 that there is no need to involve a test interface in this case, not to mention a sync method.
Currently only the generated types of structs have Deserialize()/Serialize() defined. I could trivially add those methods to unions, too. However, the same way won't apply when it comes to enums. Because we generate C++ enums which don't have methods.
So I am thinking to also expose general-purpose serialization functions.
namespace mojo {
// |VectorType| could be either std::vector<> or WTF::Vector<>.
template <typename MojomDataViewType,
typename UserType,
template<typename...> class VectorType = std::vector>
VectorType<uint8_t> Serialize(const UserType& input,
VectorType<ScopedMojoHandle>* handles = nullptr);
template <typename MojomDataViewType,
typename UserType,
template<typename...> class VectorType = std::vector>
bool Deserialize(const VectorType<uint8_t>& input, UserType* output, VectorType<ScopedMojoHandle>* handles = nullptr);
}
// For structs and unions. (They are a little more verbose. But we could also keep the Foo::Serialize()/Deserialize() convenient helpers.)
auto data = mojo::Serialize<mojom::FooDataView>(foo);
bool result = mojo::Deserialize<mojom::FooDataView>(data, &out_foo);
The corresponding MojomDataViewType types for other types:
- Enums: mojom::Foo
- Strings: mojo::StringDataView
- Arrays: mojo::ArrayDataView<ElementDataViewType>
- Maps: mojo::MapDataView<KeyDataViewType, MapDataViewType>
Why do we want to use this weird type, say, mojo::StringDataView, instead of std::string or WTF::String?
That is because *DataView is the unambiguous type for the wire format of a mojom type; while things like std::string / WTF::String are not. For example, both built-in mojom string and mojo.common.mojom.String16 are mapped to WTF::String.
WDYT? Thanks!