You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm researching capnproto with intention to use it soon. From what I can see there is no support for newtypes. I believe it'd be useful.
Examples of some newtypes I have in mind:
structAmount(u64);structHash([u8;32]);
I imagine there could be a way to annotate fields to use newtypes with specific validation function. Reader would call this function and return the newtype or error:
// defined by consumer or the crate providing Amountfnvalidate_amount(buf:&[Word]) -> Result<&Amount>{if buf.len() != 1{returnErr(Error{kind:ErrorKind::Fail,message:"length is not 1".to_owned()});}ifcast_word_to_u64(buf[0]) > LIMIT{returnErr(Error{kind:ErrorKind::Fail,message:"value over limit".to_owned()});}Ok(unsafe{&*(buf.as_ptr()as*constAmount)})}// generated codeimplReader<'_>{pubfnget_amount(&self) -> Result<&Amount>{// validates pointers etclet words = self.internal_get_amount();validate_amount(words)}}
The consumers would just call get_amount() and get the validated newtype out of the box.
Similarly, serialization could be implemented.
The text was updated successfully, but these errors were encountered:
This seems like it would be a reasonable feature. To support it, we would add an annotation to rust.capnp
I wonder whether there might be any common ground between this and proposals for allowing automatic conversion between capnp messages and native rust structs, as described in #136
I'm researching capnproto with intention to use it soon. From what I can see there is no support for newtypes. I believe it'd be useful.
Examples of some newtypes I have in mind:
I imagine there could be a way to annotate fields to use newtypes with specific validation function.
Reader
would call this function and return the newtype or error:The consumers would just call
get_amount()
and get the validated newtype out of the box.Similarly, serialization could be implemented.
The text was updated successfully, but these errors were encountered: