-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
imp(types): glue code between domain types and their corresponding protobuf types #1070
Comments
This raises a question to me, do we really need to hardcode constants such as this,
as they can be retrieved by <TmClientState as ToProto<RawTmClientState>>::type_url() If we want to make sure that these types of URLs are the ones we expect, we can add unit tests. |
Actually, we don't need
The compilation error source, after removing these
can be refactored as use ibc::primitives::ToVec;
Any::from(self).to_vec() |
While the usability with the associated type is better, we do have a use for the parameter on the |
It'd be good if we have a trait to enforce a trait ProtoValidate {
fn validate(&self) -> bool;
}
trait ToProto: ProtoValidate { ... } |
I recently learned about associated trait ToProto {
type ProtoType;
type Error;
const TYPE_URL: &'static str;
fn to_any(&self) -> Any;
fn try_from_any(any: Any) -> Result<Self, Self::Error>;
} So we can rewrite the fn to_any(self) -> Any {
Any {
type_url: Self::TYPE_URL.to_owned(),
value: ProtoType::from(self).encode_to_vec(),
}
}
fn try_from_any(any: Any) -> Result<Self, Self::Error> {
match any.type_url.as_str() {
Self::TYPE_URL => ProtoType::decode(any.value.as_slice()).and_then(Self::try_from),
_ => Err(DecodeError::new("Invalid type_url"))
}
} |
Feature Summary
We introduced
ToProto
trait in #995. It did streamline a lot of code, but we can streamline even more.Proposal
ToProto
trait withtry_from_any()
method and replace the generic with an associated type.By removing the generic
P
fromToProto
, we can access the corresponding protobuf type ofDomainType
at<DomainType as ToProto>::ProtoType
.Side-note: we may want to rename
ToProto
to something likeProtoLike
.DomainType
with a correspondingProtoType
, we/users only maintain the following code. Note the derive-macroToProto(ProtoType)
forDomainType
.With this, we can simplify the implementation of most of the current domain types. For example, the following codes can be removed from the Tendermint client state domain type.
ibc-rs/ibc-clients/ics07-tendermint/src/client_state.rs
Line 65 in 4a0990f
ibc-rs/ibc-clients/ics07-tendermint/src/client_state.rs
Lines 81 to 95 in 4a0990f
The text was updated successfully, but these errors were encountered: