From 3e6c244041c8ec91b57684eb6118f2197a45799b Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Sun, 10 Mar 2024 20:26:51 +0100 Subject: [PATCH] serialise --- light-clients/cf-guest-cw/src/msg.rs | 6 +--- .../cf-guest-cw/src/serialisation.rs | 36 ++++++++++--------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/light-clients/cf-guest-cw/src/msg.rs b/light-clients/cf-guest-cw/src/msg.rs index daf6972ae..ce84d4ba6 100644 --- a/light-clients/cf-guest-cw/src/msg.rs +++ b/light-clients/cf-guest-cw/src/msg.rs @@ -16,11 +16,7 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; use cosmwasm_std::Uint64; -use crate::{ - ibc, - serialisation, - state, -}; +use crate::{ibc, serialisation, state}; #[cw_serde] pub struct InstantiateMsg { diff --git a/light-clients/cf-guest-cw/src/serialisation.rs b/light-clients/cf-guest-cw/src/serialisation.rs index 36ec081eb..881b7acd1 100644 --- a/light-clients/cf-guest-cw/src/serialisation.rs +++ b/light-clients/cf-guest-cw/src/serialisation.rs @@ -1,4 +1,3 @@ -use alloc::borrow::Cow; use core::{fmt, marker::PhantomData, str::FromStr}; use cosmwasm_std::Binary; @@ -21,9 +20,9 @@ pub struct AsStr; /// A Serde serialisation implementation for [`ibc::Height`]. /// -/// We need it because we the type to implement `JsonSchema`. ibc-rs does +/// We need it because we need this to implement `JsonSchema`. ibc-rs does /// support schema with a `schema` feature however that brings in `std` and we -/// don’t want that. So, we need to define our own serialisation for height +/// don’t want that. As a result, we need to define our own serialisation for /// IBC height. pub struct Height; @@ -31,8 +30,7 @@ pub struct Height; impl Base64 { pub fn serialize(obj: &T, ser: S) -> Result { - let bytes = obj.to_bytes()?; - Base64Bytes(bytes.as_ref()).serialize(ser) + Base64Bytes(obj.to_bytes()?.as_ref()).serialize(ser) } pub fn deserialize<'de, T: BytesConv, D: Deserializer<'de>>(de: D) -> Result { @@ -76,13 +74,18 @@ impl Serialize for Base64Bytes<'_> { /// Trait implementing conversion to and from bytes used by [`Base64`] and /// [`OptBase64`]. pub trait BytesConv: Sized { - fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result, E>; + type Bytes<'a>: AsRef<[u8]> + where + Self: 'a; + + fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result, E>; fn from_bytes(bytes: Vec) -> Result; } impl BytesConv for Vec { - fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result, E> { - Ok(Cow::Borrowed(self.as_slice())) + type Bytes<'a> = &'a [u8]; + fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result<&'a [u8], E> { + Ok(self.as_slice()) } fn from_bytes(bytes: Vec) -> Result { @@ -91,8 +94,9 @@ impl BytesConv for Vec { } impl BytesConv for ibc::CommitmentProofBytes { - fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result, E> { - Ok(Cow::Borrowed(self.as_ref())) + type Bytes<'a> = &'a [u8]; + fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result<&'a [u8], E> { + Ok(self.as_ref()) } fn from_bytes(bytes: Vec) -> Result { @@ -103,8 +107,10 @@ impl BytesConv for ibc::CommitmentProofBytes { macro_rules! conv_via_any { ($msg:ty) => { impl BytesConv for $msg { - fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result, E> { - Ok(Cow::Owned(ibc::proto::Any::from(self).encode_to_vec())) + type Bytes<'a> = Vec; + + fn to_bytes<'a, E: serde::ser::Error>(&'a self) -> Result, E> { + Ok(ibc::proto::Any::from(self).encode_to_vec()) } fn from_bytes(bytes: Vec) -> Result { @@ -219,11 +225,7 @@ impl Height { /// The core IBC height type, which represents the height of a chain, which /// typically is the number of blocks since genesis (or more generally, since /// the last revision/hard upgrade). -#[derive( - serde::Serialize, - serde::Deserialize, - schemars::JsonSchema, -)] +#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)] pub struct RawHeight { /// Previously known as "epoch" #[serde(default, skip_serializing_if = "is_zero")]