Skip to content

Commit

Permalink
Expose ty for types that use RootLayout (#1784)
Browse files Browse the repository at this point in the history
* Expose `ty` for types that use `RootLayout`

* Remove constrain from generic implementation

* Make clippy happy
  • Loading branch information
xgreenx authored May 19, 2023
1 parent 425d453 commit 6e87ad0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/ink/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Metadata<'_> {
quote_spanned!(storage_span=>
// Wrap the layout of the contract into the `RootLayout`, because
// contract storage key is reserved for all packed fields
::ink::metadata::layout::Layout::Root(::ink::metadata::layout::RootLayout::new(
::ink::metadata::layout::Layout::Root(::ink::metadata::layout::RootLayout::new::<#storage_ident, _>(
#layout_key,
<#storage_ident as ::ink::storage::traits::StorageLayout>::layout(
&#key,
Expand Down
32 changes: 26 additions & 6 deletions crates/metadata/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pub struct RootLayout<F: Form = MetaForm> {
root_key: LayoutKey,
/// The storage layout of the unbounded layout elements.
layout: Box<Layout<F>>,
/// The type of the encoded entity.
ty: <F as Form>::Type,
}

impl IntoPortable for RootLayout {
Expand All @@ -146,25 +148,38 @@ impl IntoPortable for RootLayout {
RootLayout {
root_key: self.root_key,
layout: Box::new(self.layout.into_portable(registry)),
ty: registry.register_type(&self.ty),
}
}
}

impl<F> RootLayout<F>
where
F: Form,
{
impl RootLayout<MetaForm> {
/// Creates a new root layout.
pub fn new<L>(root_key: LayoutKey, layout: L) -> Self
pub fn new<Root, L>(root_key: LayoutKey, layout: L) -> Self
where
L: Into<Layout<F>>,
Root: TypeInfo + 'static,
L: Into<Layout<MetaForm>>,
{
Self {
root_key,
layout: Box::new(layout.into()),
ty: meta_type::<Root>(),
}
}

/// Creates a new root layout with empty root type.
pub fn new_empty<L>(root_key: LayoutKey, layout: L) -> Self
where
L: Into<Layout<MetaForm>>,
{
Self::new::<(), L>(root_key, layout)
}
}

impl<F> RootLayout<F>
where
F: Form,
{
/// Returns the root key of the sub-tree.
pub fn root_key(&self) -> &LayoutKey {
&self.root_key
Expand All @@ -174,6 +189,11 @@ where
pub fn layout(&self) -> &Layout<F> {
&self.layout
}

/// Returns the type of the encoded entity.
pub fn ty(&self) -> &F::Type {
&self.ty
}
}

/// A SCALE encoded cell.
Expand Down
16 changes: 8 additions & 8 deletions crates/metadata/src/layout/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ mod tests {
#[test]
fn valid_layout_tree_only_roots() {
// Root(0) -> Root(1) -> Root(2) -> u32
let layout = RootLayout::new(
let layout = RootLayout::new_empty(
0.into(),
RootLayout::new(
RootLayout::new_empty(
1.into(),
RootLayout::new(2.into(), LeafLayout::from_key::<u32>(2.into())),
RootLayout::new_empty(2.into(), LeafLayout::from_key::<u32>(2.into())),
),
);

Expand Down Expand Up @@ -149,7 +149,7 @@ mod tests {
// Root(4) String
// |
// g:BTreeSet<u64>(4)
let layout = RootLayout::new(
let layout = RootLayout::new_empty(
root_0,
StructLayout::new(
"Contract",
Expand All @@ -162,7 +162,7 @@ mod tests {
FieldLayout::new("d", LeafLayout::from_key::<u128>(root_0)),
FieldLayout::new(
"f",
RootLayout::new(
RootLayout::new_empty(
root_2,
EnumLayout::new(
"Enum",
Expand All @@ -178,7 +178,7 @@ mod tests {
"Struct1",
vec![FieldLayout::new(
"g",
RootLayout::new(
RootLayout::new_empty(
root_4,
LeafLayout::from_key::<
BTreeSet<u64>,
Expand Down Expand Up @@ -207,7 +207,7 @@ mod tests {
"Third",
vec![FieldLayout::new(
"0",
RootLayout::new(
RootLayout::new_empty(
root_3,
LeafLayout::from_key::<String>(
root_3,
Expand All @@ -226,7 +226,7 @@ mod tests {
FieldLayout::new("b", LeafLayout::from_key::<u32>(root_0)),
FieldLayout::new(
"c",
RootLayout::new(root_1, LeafLayout::from_key::<Vec<u8>>(root_1)),
RootLayout::new_empty(root_1, LeafLayout::from_key::<Vec<u8>>(root_1)),
),
],
),
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/src/lazy/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ const _: () = {
KeyType: StorageKey + scale_info::TypeInfo + 'static,
{
fn layout(_: &Key) -> Layout {
Layout::Root(RootLayout::new(
Layout::Root(RootLayout::new::<Self, _>(
LayoutKey::from(&KeyType::KEY),
<V as StorageLayout>::layout(&KeyType::KEY),
))
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/src/lazy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const _: () = {
KeyType: StorageKey + scale_info::TypeInfo + 'static,
{
fn layout(_: &Key) -> Layout {
Layout::Root(RootLayout::new(
Layout::Root(RootLayout::new::<Self, _>(
LayoutKey::from(&KeyType::KEY),
<V as StorageLayout>::layout(&KeyType::KEY),
))
Expand Down

0 comments on commit 6e87ad0

Please sign in to comment.