Skip to content
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

Aj metadata vnext #7

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl RuntimeGenerator {
"sp_arithmetic::per_things::Perquintill",
parse_quote!(::subxt::sp_arithmetic::per_things::Perquintill),
),
(
"frame_support::traits::misc::WrapperKeepOpaque",
parse_quote!(::subxt::WrapperKeepOpaque),
),
]
.iter()
.map(|(path, substitute): &(&str, syn::TypePath)| {
Expand Down
59 changes: 51 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ pub use sp_runtime;

use codec::{
Decode,
DecodeAll,
Encode,
};
use core::fmt::Debug;
use core::{
fmt::Debug,
marker::PhantomData,
};

mod client;
mod config;
Expand Down Expand Up @@ -139,6 +143,17 @@ pub trait Event: Decode {
}
}

/// Wraps an already encoded byte vector, prevents being encoded as a raw byte vector as part of
/// the transaction payload
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Encoded(pub Vec<u8>);

impl codec::Encode for Encoded {
fn encode(&self) -> Vec<u8> {
self.0.to_owned()
}
}

/// A phase of a block's execution.
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
pub enum Phase {
Expand All @@ -150,13 +165,41 @@ pub enum Phase {
Initialization,
}

/// Wraps an already encoded byte vector, prevents being encoded as a raw byte vector as part of
/// the transaction payload
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Encoded(pub Vec<u8>);
/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
///
/// This type is similar to [`WrapperOpaque`], but it differs in the way it stores the type `T`.
/// While [`WrapperOpaque`] stores the decoded type, the [`WrapperKeepOpaque`] stores the type only
/// in its opaque format, aka as a `Vec<u8>`. To access the real type `T` [`Self::try_decode`] needs
/// to be used.
#[derive(Debug, Eq, PartialEq, Default, Clone, Decode, Encode)]
pub struct WrapperKeepOpaque<T> {
data: Vec<u8>,
_phantom: PhantomData<T>,
}

impl codec::Encode for Encoded {
fn encode(&self) -> Vec<u8> {
self.0.to_owned()
impl<T: Decode> WrapperKeepOpaque<T> {
/// Try to decode the wrapped type from the inner `data`.
///
/// Returns `None` if the decoding failed.
pub fn try_decode(&self) -> Option<T> {
T::decode_all(&mut &self.data[..]).ok()
}

/// Returns the length of the encoded `T`.
pub fn encoded_len(&self) -> usize {
self.data.len()
}

/// Returns the encoded data.
pub fn encoded(&self) -> &[u8] {
&self.data
}

/// Create from the given encoded `data`.
pub fn from_encoded(data: Vec<u8>) -> Self {
Self {
data,
_phantom: PhantomData,
}
}
}
Loading