WIP Add a 'foreign' interface to prost-derive to support scalar encodings defined outside of prost - #1230
WIP Add a 'foreign' interface to prost-derive to support scalar encodings defined outside of prost#1230bickfordb wants to merge 2 commits into
Conversation
|
I think this is a very interesting idea. I had always thought a trait would be required to make something like this work. Prost had an I think we should first clean up and document the |
|
One drawback of the modules is the lack of generics. Therefore, you can't make one encoding module that works for every boxed type. |
|
I'm also encountering this. I have a Being able to specify my own scalar codec is extremely helpful in cases like these, because right now I need to implement For my use case, this would also need an |
|
Also, since the foreign type needs to start with
Doesn't type inference handle that? e.g. pub fn encode<T: prost::Message>(tag: u32, value: &Box<T>, buf: &mut impl prost::bytes::BufMut) {
prost::encoding::message::encode(tag, value, buf);
}
pub fn test() {
let mut buf = Vec::with_capacity(1024);
let box_a = Box::new(1u32);
let box_b = Box::new("hello".to_string());
let box_c = Box::new(TestMessage { test: "world".to_string() });
encode(1u32, &box_a, &mut buf);
encode(2u32, &box_b, &mut buf);
encode(3u32, &box_c, &mut buf);
} |
Motivation
Modern webapps make heavy use of values like UUIDs and DateTimes. These are effectively numeric/string/byte scalers. It's important to make these easy to use for productivity. These are missing support in Prost and can be represented as Message structs, but this is awkward to use with libraries like SeaORM and require unnecessary conversions between protobuf-ish structs and SeaORM structs.
prost::Messagefor types likeuuid::Uuidorchrono::DateTime.MessagetraitExample
my-web-app/src/entity/account.rs
my-web-app/src/account.proto