Skip to content

Commit 1211df1

Browse files
authored
Merge pull request #7 from paritytech/aj-metadata-vnext
Aj metadata vnext
2 parents 4aaf21d + 0c45452 commit 1211df1

File tree

5 files changed

+56
-18898
lines changed

5 files changed

+56
-18898
lines changed

codegen/src/api/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ impl RuntimeGenerator {
127127
"sp_arithmetic::per_things::Perquintill",
128128
parse_quote!(::subxt::sp_arithmetic::per_things::Perquintill),
129129
),
130+
(
131+
"frame_support::traits::misc::WrapperKeepOpaque",
132+
parse_quote!(::subxt::WrapperKeepOpaque),
133+
),
130134
]
131135
.iter()
132136
.map(|(path, substitute): &(&str, syn::TypePath)| {

src/lib.rs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ pub use sp_runtime;
5151

5252
use codec::{
5353
Decode,
54+
DecodeAll,
5455
Encode,
5556
};
56-
use core::fmt::Debug;
57+
use core::{
58+
fmt::Debug,
59+
marker::PhantomData,
60+
};
5761

5862
mod client;
5963
mod config;
@@ -139,6 +143,17 @@ pub trait Event: Decode {
139143
}
140144
}
141145

146+
/// Wraps an already encoded byte vector, prevents being encoded as a raw byte vector as part of
147+
/// the transaction payload
148+
#[derive(Clone, Debug, Eq, PartialEq)]
149+
pub struct Encoded(pub Vec<u8>);
150+
151+
impl codec::Encode for Encoded {
152+
fn encode(&self) -> Vec<u8> {
153+
self.0.to_owned()
154+
}
155+
}
156+
142157
/// A phase of a block's execution.
143158
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
144159
pub enum Phase {
@@ -150,13 +165,41 @@ pub enum Phase {
150165
Initialization,
151166
}
152167

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

158-
impl codec::Encode for Encoded {
159-
fn encode(&self) -> Vec<u8> {
160-
self.0.to_owned()
180+
impl<T: Decode> WrapperKeepOpaque<T> {
181+
/// Try to decode the wrapped type from the inner `data`.
182+
///
183+
/// Returns `None` if the decoding failed.
184+
pub fn try_decode(&self) -> Option<T> {
185+
T::decode_all(&mut &self.data[..]).ok()
186+
}
187+
188+
/// Returns the length of the encoded `T`.
189+
pub fn encoded_len(&self) -> usize {
190+
self.data.len()
191+
}
192+
193+
/// Returns the encoded data.
194+
pub fn encoded(&self) -> &[u8] {
195+
&self.data
196+
}
197+
198+
/// Create from the given encoded `data`.
199+
pub fn from_encoded(data: Vec<u8>) -> Self {
200+
Self {
201+
data,
202+
_phantom: PhantomData,
203+
}
161204
}
162205
}

0 commit comments

Comments
 (0)