ibc_primitives/traits/
proto.rs

1use core::fmt::Display;
2
3use ibc_proto::google::protobuf::Any;
4use ibc_proto::Protobuf;
5
6use crate::prelude::*;
7
8/// Types that implement this trait can be converted to
9/// a raw Protobuf `Any` type.
10pub trait ToProto<P>: Protobuf<P>
11where
12    P: From<Self> + prost::Message + prost::Name + Default,
13    <Self as TryFrom<P>>::Error: Display,
14{
15    fn type_url() -> String {
16        P::type_url()
17    }
18
19    fn to_any(self) -> Any {
20        Any {
21            type_url: P::type_url(),
22            value: self.encode_vec(),
23        }
24    }
25}
26
27impl<T, P> ToProto<P> for T
28where
29    T: Protobuf<P>,
30    P: From<Self> + prost::Message + prost::Name + Default,
31    <Self as TryFrom<P>>::Error: Display,
32{
33}
34
35/// Convenient trait for converting types to a raw Protobuf `Vec<u8>`.
36pub trait ToVec {
37    fn to_vec(&self) -> Vec<u8>;
38}
39
40impl<T: prost::Message> ToVec for T {
41    fn to_vec(&self) -> Vec<u8> {
42        self.encode_to_vec()
43    }
44}