diff --git a/crates/ink/codegen/src/generator/metadata.rs b/crates/ink/codegen/src/generator/metadata.rs index 63f477bb232..da21b6fe541 100644 --- a/crates/ink/codegen/src/generator/metadata.rs +++ b/crates/ink/codegen/src/generator/metadata.rs @@ -164,7 +164,7 @@ impl Metadata<'_> { /// Generates the ink! metadata for the given type. fn generate_type_spec(ty: &syn::Type) -> TokenStream2 { fn without_display_name(ty: &syn::Type) -> TokenStream2 { - quote! { ::ink::metadata::TypeSpec::new::<#ty>() } + quote! { ::ink::metadata::TypeSpec::of_type::<#ty>() } } if let syn::Type::Path(type_path) = ty { if type_path.qself.is_some() { diff --git a/crates/metadata/src/layout/mod.rs b/crates/metadata/src/layout/mod.rs index b21efaac099..e39ae101177 100644 --- a/crates/metadata/src/layout/mod.rs +++ b/crates/metadata/src/layout/mod.rs @@ -106,6 +106,14 @@ impl<'a> From<&'a Key> for LayoutKey { } impl LayoutKey { + /// Construct a custom layout key. + pub fn new(key: T) -> Self + where + T: Into, + { + Self { key: key.into() } + } + /// Returns the key of the layout key. pub fn key(&self) -> &Key { &self.key @@ -125,19 +133,6 @@ pub struct RootLayout { layout: Box>, } -impl RootLayout { - /// Creates a new root layout. - pub fn new(root_key: LayoutKey, layout: L) -> Self - where - L: Into, - { - Self { - root_key, - layout: Box::new(layout.into()), - } - } -} - impl IntoPortable for RootLayout { type Output = RootLayout; @@ -153,6 +148,17 @@ impl RootLayout where F: Form, { + /// Creates a new root layout. + pub fn new(root_key: LayoutKey, layout: L) -> Self + where + L: Into>, + { + Self { + root_key, + layout: Box::new(layout.into()), + } + } + /// Returns the root key of the sub-tree. pub fn root_key(&self) -> &LayoutKey { &self.root_key @@ -179,7 +185,7 @@ pub struct LeafLayout { impl LeafLayout { /// Creates a new cell layout. - pub fn new(key: LayoutKey) -> Self + pub fn from_key(key: LayoutKey) -> Self where T: TypeInfo + 'static, { @@ -241,6 +247,10 @@ where pub fn ty(&self) -> &F::Type { &self.ty } + + pub fn new(key: LayoutKey, ty: ::Type) -> Self { + Self { key, ty } + } } /// A hashing layout potentially hitting all cells of the storage. @@ -448,24 +458,22 @@ pub struct StructLayout { fields: Vec>, } -impl StructLayout { +impl StructLayout +where + F: Form, +{ /// Creates a new struct layout. - pub fn new(name: N, fields: F) -> Self + pub fn new(name: N, fields: T) -> Self where - N: Into<&'static str>, - F: IntoIterator, + N: Into, + T: IntoIterator>, { Self { name: name.into(), fields: fields.into_iter().collect(), } } -} -impl StructLayout -where - F: Form, -{ /// Returns the name of the struct. pub fn name(&self) -> &F::String { &self.name @@ -481,7 +489,7 @@ impl IntoPortable for StructLayout { fn into_portable(self, registry: &mut Registry) -> Self::Output { StructLayout { - name: self.name.into(), + name: self.name.to_string(), fields: self .fields .into_iter() @@ -507,24 +515,22 @@ pub struct FieldLayout { layout: Layout, } -impl FieldLayout { - /// Creates a new field layout. +impl FieldLayout +where + F: Form, +{ + /// Creates a new custom field layout. pub fn new(name: N, layout: L) -> Self where - N: Into<&'static str>, - L: Into, + N: Into, + L: Into>, { Self { name: name.into(), layout: layout.into(), } } -} -impl FieldLayout -where - F: Form, -{ /// Returns the name of the field. pub fn name(&self) -> &F::String { &self.name @@ -544,7 +550,7 @@ impl IntoPortable for FieldLayout { fn into_portable(self, registry: &mut Registry) -> Self::Output { FieldLayout { - name: self.name.into_portable(registry), + name: self.name.to_string(), layout: self.layout.into_portable(registry), } } @@ -587,7 +593,7 @@ impl EnumLayout { /// Creates a new enum layout. pub fn new(name: N, dispatch_key: K, variants: V) -> Self where - N: Into<&'static str>, + N: Into<::String>, K: Into, V: IntoIterator, { @@ -624,7 +630,7 @@ impl IntoPortable for EnumLayout { fn into_portable(self, registry: &mut Registry) -> Self::Output { EnumLayout { - name: self.name.into(), + name: self.name.to_string(), dispatch_key: self.dispatch_key, variants: self .variants diff --git a/crates/metadata/src/layout/tests.rs b/crates/metadata/src/layout/tests.rs index 464dd446a73..a62d7376267 100644 --- a/crates/metadata/src/layout/tests.rs +++ b/crates/metadata/src/layout/tests.rs @@ -26,8 +26,8 @@ fn named_fields_struct_layout(key: &Key) -> Layout { StructLayout::new( "Struct", vec![ - FieldLayout::new("a", LeafLayout::new::(LayoutKey::from(key))), - FieldLayout::new("b", LeafLayout::new::(LayoutKey::from(key))), + FieldLayout::new("a", LeafLayout::from_key::(LayoutKey::from(key))), + FieldLayout::new("b", LeafLayout::from_key::(LayoutKey::from(key))), ], ) .into() @@ -73,8 +73,8 @@ fn tuple_struct_layout(key: &Key) -> Layout { StructLayout::new( "(A, B)", vec![ - FieldLayout::new("0", LeafLayout::new::(LayoutKey::from(key))), - FieldLayout::new("1", LeafLayout::new::(LayoutKey::from(key))), + FieldLayout::new("0", LeafLayout::from_key::(LayoutKey::from(key))), + FieldLayout::new("1", LeafLayout::from_key::(LayoutKey::from(key))), ], ) .into() @@ -175,11 +175,11 @@ fn mixed_enum_layout(key: &Key) -> Layout { vec![ FieldLayout::new( "0", - LeafLayout::new::(LayoutKey::from(variant_key)), + LeafLayout::from_key::(LayoutKey::from(variant_key)), ), FieldLayout::new( "1", - LeafLayout::new::(LayoutKey::from(variant_key)), + LeafLayout::from_key::(LayoutKey::from(variant_key)), ), ], ), @@ -194,11 +194,11 @@ fn mixed_enum_layout(key: &Key) -> Layout { vec![ FieldLayout::new( "a", - LeafLayout::new::(LayoutKey::from(variant_key)), + LeafLayout::from_key::(LayoutKey::from(variant_key)), ), FieldLayout::new( "b", - LeafLayout::new::(LayoutKey::from(variant_key)), + LeafLayout::from_key::(LayoutKey::from(variant_key)), ), ], ), @@ -287,7 +287,7 @@ fn unbounded_hashing_layout(key: &Key) -> Layout { b"ink storage hashmap".to_vec(), Vec::new(), ), - LeafLayout::new::<(i32, bool)>(LayoutKey::from(root_key)), + LeafLayout::from_key::<(i32, bool)>(LayoutKey::from(root_key)), ) .into() } diff --git a/crates/metadata/src/layout/validate.rs b/crates/metadata/src/layout/validate.rs index 87d70084810..a3984506aa1 100644 --- a/crates/metadata/src/layout/validate.rs +++ b/crates/metadata/src/layout/validate.rs @@ -112,7 +112,7 @@ mod tests { 0.into(), RootLayout::new( 1.into(), - RootLayout::new(2.into(), LeafLayout::new::(2.into())), + RootLayout::new(2.into(), LeafLayout::from_key::(2.into())), ), ); @@ -159,7 +159,7 @@ mod tests { StructLayout::new( "Struct0", vec![ - FieldLayout::new("d", LeafLayout::new::(root_0)), + FieldLayout::new("d", LeafLayout::from_key::(root_0)), FieldLayout::new( "f", RootLayout::new( @@ -180,7 +180,7 @@ mod tests { "g", RootLayout::new( root_4, - LeafLayout::new::< + LeafLayout::from_key::< BTreeSet, >( root_4 @@ -197,7 +197,7 @@ mod tests { "Second", vec![FieldLayout::new( "0", - LeafLayout::new::(root_2), + LeafLayout::from_key::(root_2), )], ), ), @@ -209,7 +209,7 @@ mod tests { "0", RootLayout::new( root_3, - LeafLayout::new::( + LeafLayout::from_key::( root_3, ), ), @@ -223,10 +223,10 @@ mod tests { ], ), ), - FieldLayout::new("b", LeafLayout::new::(root_0)), + FieldLayout::new("b", LeafLayout::from_key::(root_0)), FieldLayout::new( "c", - RootLayout::new(root_1, LeafLayout::new::>(root_1)), + RootLayout::new(root_1, LeafLayout::from_key::>(root_1)), ), ], ), diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index 74bae41240b..200df171b10 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -96,6 +96,7 @@ pub struct InkProject { } impl InkProject { + /// Create a new ink! project from a layout and a spec. pub fn new(layout: L, spec: S) -> Self where L: Into, @@ -110,9 +111,23 @@ impl InkProject { registry: registry.into(), } } -} -impl InkProject { + /// Create a new portable ink! project. + /// + /// The caller is responsible to register all types into the supplied registry. + pub fn new_portable( + layout: layout::Layout, + spec: ContractSpec, + registry: PortableRegistry, + ) -> Self { + Self { + version: Default::default(), + layout, + spec, + registry, + } + } + /// Returns the metadata version used by the contract. pub fn version(&self) -> &MetadataVersion { &self.version diff --git a/crates/metadata/src/specs.rs b/crates/metadata/src/specs.rs index ad599de8c47..178db13d7f0 100644 --- a/crates/metadata/src/specs.rs +++ b/crates/metadata/src/specs.rs @@ -79,7 +79,7 @@ impl IntoPortable for ContractSpec { .into_iter() .map(|event| event.into_portable(registry)) .collect::>(), - docs: registry.map_into_portable(self.docs), + docs: self.docs.into_iter().map(|s| s.into()).collect(), } } } @@ -114,20 +114,25 @@ pub enum Valid {} /// The message builder is not ready to finalize construction. pub enum Invalid {} -/// A builder for contracts. #[must_use] -pub struct ContractSpecBuilder { +pub struct ContractSpecBuilder +where + F: Form, +{ /// The to-be-constructed contract specification. - spec: ContractSpec, + spec: ContractSpec, /// Marker for compile-time checking of valid contract specifications. marker: PhantomData S>, } -impl ContractSpecBuilder { +impl ContractSpecBuilder +where + F: Form, +{ /// Sets the constructors of the contract specification. - pub fn constructors(self, constructors: C) -> ContractSpecBuilder + pub fn constructors(self, constructors: C) -> ContractSpecBuilder where - C: IntoIterator, + C: IntoIterator>, { debug_assert!(self.spec.constructors.is_empty()); ContractSpecBuilder { @@ -140,11 +145,14 @@ impl ContractSpecBuilder { } } -impl ContractSpecBuilder { +impl ContractSpecBuilder +where + F: Form, +{ /// Sets the messages of the contract specification. pub fn messages(self, messages: M) -> Self where - M: IntoIterator, + M: IntoIterator>, { debug_assert!(self.spec.messages.is_empty()); Self { @@ -159,7 +167,7 @@ impl ContractSpecBuilder { /// Sets the events of the contract specification. pub fn events(self, events: E) -> Self where - E: IntoIterator, + E: IntoIterator>, { debug_assert!(self.spec.events.is_empty()); Self { @@ -174,7 +182,7 @@ impl ContractSpecBuilder { /// Sets the documentation of the contract specification. pub fn docs(self, docs: D) -> Self where - D: IntoIterator, + D: IntoIterator::String>, { debug_assert!(self.spec.docs.is_empty()); Self { @@ -187,9 +195,12 @@ impl ContractSpecBuilder { } } -impl ContractSpecBuilder { +impl ContractSpecBuilder +where + F: Form, +{ /// Finalizes construction of the contract specification. - pub fn done(self) -> ContractSpec { + pub fn done(self) -> ContractSpec { assert!( !self.spec.constructors.is_empty(), "must have at least one constructor" @@ -202,9 +213,12 @@ impl ContractSpecBuilder { } } -impl ContractSpec { +impl ContractSpec +where + F: Form, +{ /// Creates a new contract specification. - pub fn new() -> ContractSpecBuilder { + pub fn new() -> ContractSpecBuilder { ContractSpecBuilder { spec: Self { constructors: Vec::new(), @@ -243,7 +257,7 @@ impl IntoPortable for ConstructorSpec { fn into_portable(self, registry: &mut Registry) -> Self::Output { ConstructorSpec { - label: self.label.into_portable(registry), + label: self.label.to_string(), selector: self.selector, payable: self.payable, args: self @@ -251,7 +265,7 @@ impl IntoPortable for ConstructorSpec { .into_iter() .map(|arg| arg.into_portable(registry)) .collect::>(), - docs: registry.map_into_portable(self.docs), + docs: self.docs.into_iter().map(|s| s.into()).collect(), } } } @@ -296,16 +310,20 @@ where /// compile-time instead of at run-time. This is useful to better /// debug code-gen macros. #[must_use] -pub struct ConstructorSpecBuilder { - spec: ConstructorSpec, +pub struct ConstructorSpecBuilder { + spec: ConstructorSpec, marker: PhantomData (Selector, IsPayable)>, } -impl ConstructorSpec { +impl ConstructorSpec +where + F: Form, +{ /// Creates a new constructor spec builder. pub fn from_label( - label: &'static str, - ) -> ConstructorSpecBuilder, Missing> { + label: ::String, + ) -> ConstructorSpecBuilder, Missing> + { ConstructorSpecBuilder { spec: Self { label, @@ -319,12 +337,15 @@ impl ConstructorSpec { } } -impl

ConstructorSpecBuilder, P> { +impl ConstructorSpecBuilder, P> +where + F: Form, +{ /// Sets the function selector of the message. pub fn selector( self, selector: [u8; 4], - ) -> ConstructorSpecBuilder { + ) -> ConstructorSpecBuilder { ConstructorSpecBuilder { spec: ConstructorSpec { selector: selector.into(), @@ -335,12 +356,15 @@ impl

ConstructorSpecBuilder, P> { } } -impl ConstructorSpecBuilder> { +impl ConstructorSpecBuilder> +where + F: Form, +{ /// Sets if the constructor is payable, thus accepting value for the caller. pub fn payable( self, is_payable: bool, - ) -> ConstructorSpecBuilder { + ) -> ConstructorSpecBuilder { ConstructorSpecBuilder { spec: ConstructorSpec { payable: is_payable, @@ -351,11 +375,14 @@ impl ConstructorSpecBuilder> { } } -impl ConstructorSpecBuilder { +impl ConstructorSpecBuilder +where + F: Form, +{ /// Sets the input arguments of the message specification. pub fn args(self, args: A) -> Self where - A: IntoIterator, + A: IntoIterator>, { let mut this = self; debug_assert!(this.spec.args.is_empty()); @@ -364,23 +391,27 @@ impl ConstructorSpecBuilder { } /// Sets the documentation of the message specification. - pub fn docs(self, docs: D) -> Self + pub fn docs<'a, D>(self, docs: D) -> Self where - D: IntoIterator, + D: IntoIterator, + F::String: From<&'a str>, { let mut this = self; debug_assert!(this.spec.docs.is_empty()); this.spec.docs = docs .into_iter() - .map(trim_extra_whitespace) + .map(|s| trim_extra_whitespace(s).into()) .collect::>(); this } } -impl ConstructorSpecBuilder { +impl ConstructorSpecBuilder +where + F: Form, +{ /// Finishes construction of the constructor. - pub fn done(self) -> ConstructorSpec { + pub fn done(self) -> ConstructorSpec { self.spec } } @@ -430,11 +461,15 @@ mod state { pub struct Returns; } -impl MessageSpec { +impl MessageSpec +where + F: Form, +{ /// Creates a new message spec builder. pub fn from_label( - label: &'static str, + label: ::String, ) -> MessageSpecBuilder< + F, Missing, Missing, Missing, @@ -507,17 +542,23 @@ where /// debug code-gen macros. #[allow(clippy::type_complexity)] #[must_use] -pub struct MessageSpecBuilder { - spec: MessageSpec, +pub struct MessageSpecBuilder +where + F: Form, +{ + spec: MessageSpec, marker: PhantomData (Selector, Mutates, IsPayable, Returns)>, } -impl MessageSpecBuilder, M, P, R> { +impl MessageSpecBuilder, M, P, R> +where + F: Form, +{ /// Sets the function selector of the message. pub fn selector( self, selector: [u8; 4], - ) -> MessageSpecBuilder { + ) -> MessageSpecBuilder { MessageSpecBuilder { spec: MessageSpec { selector: selector.into(), @@ -528,9 +569,15 @@ impl MessageSpecBuilder, M, P, R> { } } -impl MessageSpecBuilder, P, R> { +impl MessageSpecBuilder, P, R> +where + F: Form, +{ /// Sets if the message is mutable, thus taking `&mut self` or not thus taking `&self`. - pub fn mutates(self, mutates: bool) -> MessageSpecBuilder { + pub fn mutates( + self, + mutates: bool, + ) -> MessageSpecBuilder { MessageSpecBuilder { spec: MessageSpec { mutates, @@ -541,12 +588,15 @@ impl MessageSpecBuilder, P, R> { } } -impl MessageSpecBuilder, R> { +impl MessageSpecBuilder, R> +where + F: Form, +{ /// Sets if the message is payable, thus accepting value for the caller. pub fn payable( self, is_payable: bool, - ) -> MessageSpecBuilder { + ) -> MessageSpecBuilder { MessageSpecBuilder { spec: MessageSpec { payable: is_payable, @@ -557,12 +607,15 @@ impl MessageSpecBuilder, R> { } } -impl MessageSpecBuilder> { +impl MessageSpecBuilder> +where + F: Form, +{ /// Sets the return type of the message. pub fn returns( self, - return_type: ReturnTypeSpec, - ) -> MessageSpecBuilder { + return_type: ReturnTypeSpec, + ) -> MessageSpecBuilder { MessageSpecBuilder { spec: MessageSpec { return_type, @@ -573,11 +626,14 @@ impl MessageSpecBuilder> { } } -impl MessageSpecBuilder { +impl MessageSpecBuilder +where + F: Form, +{ /// Sets the input arguments of the message specification. pub fn args(self, args: A) -> Self where - A: IntoIterator, + A: IntoIterator>, { let mut this = self; debug_assert!(this.spec.args.is_empty()); @@ -588,23 +644,28 @@ impl MessageSpecBuilder { /// Sets the documentation of the message specification. pub fn docs(self, docs: D) -> Self where - D: IntoIterator, + D: IntoIterator::String>, { let mut this = self; debug_assert!(this.spec.docs.is_empty()); - this.spec.docs = docs - .into_iter() - .map(trim_extra_whitespace) - .collect::>(); + this.spec.docs = docs.into_iter().collect::>(); this } } -impl - MessageSpecBuilder +impl + MessageSpecBuilder< + F, + state::Selector, + state::Mutates, + state::IsPayable, + state::Returns, + > +where + F: Form, { /// Finishes construction of the message. - pub fn done(self) -> MessageSpec { + pub fn done(self) -> MessageSpec { self.spec } } @@ -614,7 +675,7 @@ impl IntoPortable for MessageSpec { fn into_portable(self, registry: &mut Registry) -> Self::Output { MessageSpec { - label: self.label.into_portable(registry), + label: self.label.to_string(), selector: self.selector, mutates: self.mutates, payable: self.payable, @@ -624,7 +685,7 @@ impl IntoPortable for MessageSpec { .map(|arg| arg.into_portable(registry)) .collect::>(), return_type: self.return_type.into_portable(registry), - docs: registry.map_into_portable(self.docs), + docs: self.docs.into_iter().map(|s| s.into()).collect(), } } } @@ -646,15 +707,21 @@ pub struct EventSpec { /// An event specification builder. #[must_use] -pub struct EventSpecBuilder { - spec: EventSpec, +pub struct EventSpecBuilder +where + F: Form, +{ + spec: EventSpec, } -impl EventSpecBuilder { +impl EventSpecBuilder +where + F: Form, +{ /// Sets the input arguments of the event specification. pub fn args(self, args: A) -> Self where - A: IntoIterator, + A: IntoIterator>, { let mut this = self; debug_assert!(this.spec.args.is_empty()); @@ -665,7 +732,7 @@ impl EventSpecBuilder { /// Sets the input arguments of the event specification. pub fn docs(self, docs: D) -> Self where - D: IntoIterator, + D: IntoIterator::String>, { let mut this = self; debug_assert!(this.spec.docs.is_empty()); @@ -674,7 +741,7 @@ impl EventSpecBuilder { } /// Finalizes building the event specification. - pub fn done(self) -> EventSpec { + pub fn done(self) -> EventSpec { self.spec } } @@ -684,20 +751,23 @@ impl IntoPortable for EventSpec { fn into_portable(self, registry: &mut Registry) -> Self::Output { EventSpec { - label: self.label.into_portable(registry), + label: self.label.to_string(), args: self .args .into_iter() .map(|arg| arg.into_portable(registry)) .collect::>(), - docs: registry.map_into_portable(self.docs), + docs: self.docs.into_iter().map(|s| s.into()).collect(), } } } -impl EventSpec { +impl EventSpec +where + F: Form, +{ /// Creates a new event specification builder. - pub fn new(label: &'static str) -> EventSpecBuilder { + pub fn new(label: ::String) -> EventSpecBuilder { EventSpecBuilder { spec: Self { label, @@ -753,6 +823,14 @@ impl<'de> serde::Deserialize<'de> for Selector { } impl Selector { + /// Create a new custom selector. + pub fn new(bytes: T) -> Self + where + T: Into<[u8; 4]>, + { + Self(bytes.into()) + } + /// Returns the underlying selector bytes. pub fn to_bytes(&self) -> &[u8] { &self.0 @@ -809,6 +887,21 @@ pub struct TypeSpec { display_name: DisplayName, } +impl Default for TypeSpec { + fn default() -> Self { + TypeSpec::of_type::<()>() + } +} + +impl Default for TypeSpec { + fn default() -> Self { + Self { + ty: u32::default().into(), + display_name: Default::default(), + } + } +} + impl IntoPortable for TypeSpec { type Output = TypeSpec; @@ -864,7 +957,13 @@ impl TypeSpec { } /// Creates a new type specification without a display name. - pub fn new() -> Self + /// + /// Example: + /// ```no_run + /// # use ink_metadata::{TypeSpec, ReturnTypeSpec}; + /// ReturnTypeSpec::new(TypeSpec::of_type::()); // return type of `i32` + /// ``` + pub fn of_type() -> Self where T: TypeInfo + 'static, { @@ -888,6 +987,11 @@ where pub fn display_name(&self) -> &DisplayName { &self.display_name } + + /// Creates a new type specification for a given type and display name. + pub fn new(ty: ::Type, display_name: DisplayName) -> Self { + Self { ty, display_name } + } } /// Describes a pair of parameter label and type. @@ -913,35 +1017,33 @@ impl IntoPortable for EventParamSpec { fn into_portable(self, registry: &mut Registry) -> Self::Output { EventParamSpec { - label: self.label.into_portable(registry), + label: self.label.to_string(), indexed: self.indexed, ty: self.ty.into_portable(registry), - docs: registry.map_into_portable(self.docs), + docs: self.docs.into_iter().map(|s| s.into()).collect(), } } } -impl EventParamSpec { +impl EventParamSpec +where + F: Form, + TypeSpec: Default, +{ /// Creates a new event parameter specification builder. - pub fn new(label: &'static str) -> EventParamSpecBuilder { + pub fn new(label: F::String) -> EventParamSpecBuilder { EventParamSpecBuilder { spec: Self { label, // By default event parameters are not indexed. indexed: false, // We initialize every parameter type as `()`. - ty: TypeSpec::new::<()>(), + ty: Default::default(), // We start with empty docs. docs: vec![], }, } } -} - -impl EventParamSpec -where - F: Form, -{ /// Returns the label of the parameter. pub fn label(&self) -> &F::String { &self.label @@ -965,14 +1067,20 @@ where /// Used to construct an event parameter specification. #[must_use] -pub struct EventParamSpecBuilder { +pub struct EventParamSpecBuilder +where + F: Form, +{ /// The built-up event parameter specification. - spec: EventParamSpec, + spec: EventParamSpec, } -impl EventParamSpecBuilder { +impl EventParamSpecBuilder +where + F: Form, +{ /// Sets the type of the event parameter. - pub fn of_type(self, spec: TypeSpec) -> Self { + pub fn of_type(self, spec: TypeSpec) -> Self { let mut this = self; this.spec.ty = spec; this @@ -988,7 +1096,7 @@ impl EventParamSpecBuilder { /// Sets the documentation of the event parameter. pub fn docs(self, docs: D) -> Self where - D: IntoIterator, + D: IntoIterator::String>, { debug_assert!(self.spec.docs.is_empty()); Self { @@ -1000,7 +1108,7 @@ impl EventParamSpecBuilder { } /// Finishes constructing the event parameter spec. - pub fn done(self) -> EventParamSpec { + pub fn done(self) -> EventParamSpec { self.spec } } @@ -1012,6 +1120,7 @@ impl EventParamSpecBuilder { serialize = "F::Type: Serialize, F::String: Serialize", deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned" ))] +#[must_use] pub struct ReturnTypeSpec { #[serde(rename = "type")] opt_type: Option>, @@ -1029,31 +1138,27 @@ impl IntoPortable for ReturnTypeSpec { } } -impl ReturnTypeSpec { +impl ReturnTypeSpec +where + F: Form, +{ /// Creates a new return type specification from the given type or `None`. /// /// # Examples /// /// ```no_run /// # use ink_metadata::{TypeSpec, ReturnTypeSpec}; - /// ReturnTypeSpec::new(None); // no return type; - /// ReturnTypeSpec::new(TypeSpec::new::()); // return type of `i32` + /// >::new(None); // no return type; /// ``` - #[must_use] pub fn new(ty: T) -> Self where - T: Into>, + T: Into>>, { Self { opt_type: ty.into(), } } -} -impl ReturnTypeSpec -where - F: Form, -{ /// Returns the optional return type pub fn opt_type(&self) -> Option<&TypeSpec> { self.opt_type.as_ref() @@ -1079,29 +1184,28 @@ impl IntoPortable for MessageParamSpec { fn into_portable(self, registry: &mut Registry) -> Self::Output { MessageParamSpec { - label: self.label.into_portable(registry), + label: self.label.to_string(), ty: self.ty.into_portable(registry), } } } -impl MessageParamSpec { +impl MessageParamSpec +where + F: Form, + TypeSpec: Default, +{ /// Constructs a new message parameter specification via builder. - pub fn new(label: &'static str) -> MessageParamSpecBuilder { + pub fn new(label: F::String) -> MessageParamSpecBuilder { MessageParamSpecBuilder { spec: Self { label, // Uses `()` type by default. - ty: TypeSpec::new::<()>(), + ty: TypeSpec::default(), }, } } -} -impl MessageParamSpec -where - F: Form, -{ /// Returns the label of the parameter. pub fn label(&self) -> &F::String { &self.label @@ -1115,21 +1219,24 @@ where /// Used to construct a message parameter specification. #[must_use] -pub struct MessageParamSpecBuilder { +pub struct MessageParamSpecBuilder { /// The to-be-constructed message parameter specification. - spec: MessageParamSpec, + spec: MessageParamSpec, } -impl MessageParamSpecBuilder { +impl MessageParamSpecBuilder +where + F: Form, +{ /// Sets the type of the message parameter. - pub fn of_type(self, ty: TypeSpec) -> Self { + pub fn of_type(self, ty: TypeSpec) -> Self { let mut this = self; this.spec.ty = ty; this } /// Finishes construction of the message parameter. - pub fn done(self) -> MessageParamSpec { + pub fn done(self) -> MessageParamSpec { self.spec } } diff --git a/crates/metadata/src/tests.rs b/crates/metadata/src/tests.rs index 6f782407261..43b2634531b 100644 --- a/crates/metadata/src/tests.rs +++ b/crates/metadata/src/tests.rs @@ -63,13 +63,11 @@ fn spec_contract_json() { vec!["i32"].into_iter().map(AsRef::as_ref), )) .done()]) - .docs(Vec::new()) .done(), ConstructorSpec::from_label("default") .selector([2u8, 34u8, 255u8, 24u8]) .payable(Default::default()) .args(Vec::new()) - .docs(Vec::new()) .done(), ]) .messages(vec![ @@ -82,7 +80,6 @@ fn spec_contract_json() { vec!["i32"].into_iter().map(AsRef::as_ref), )) .done()]) - .docs(Vec::new()) .returns(ReturnTypeSpec::new(None)) .done(), MessageSpec::from_label("get") @@ -90,14 +87,12 @@ fn spec_contract_json() { .mutates(false) .payable(false) .args(Vec::new()) - .docs(Vec::new()) .returns(ReturnTypeSpec::new(TypeSpec::with_name_segs::( vec!["i32"].into_iter().map(AsRef::as_ref), ))) .done(), ]) .events(Vec::new()) - .docs(Vec::new()) .done(); let mut registry = Registry::new(); diff --git a/crates/storage/traits/src/layout/impls.rs b/crates/storage/traits/src/layout/impls.rs index bc2d2f71b4f..0513780ec77 100644 --- a/crates/storage/traits/src/layout/impls.rs +++ b/crates/storage/traits/src/layout/impls.rs @@ -48,7 +48,7 @@ macro_rules! impl_storage_layout_for_primitives { $( impl StorageLayout for $name { fn layout(key: &Key) -> Layout { - Layout::Leaf(LeafLayout::new::<$name>(LayoutKey::from(key))) + Layout::Leaf(LeafLayout::from_key::<$name>(LayoutKey::from(key))) } } )* @@ -228,7 +228,7 @@ where T: TypeInfo + 'static + Packed, { fn layout(key: &Key) -> Layout { - Layout::Leaf(LeafLayout::new::(LayoutKey::from(key))) + Layout::Leaf(LeafLayout::from_key::(LayoutKey::from(key))) } } @@ -238,7 +238,7 @@ where V: TypeInfo + 'static + Packed, { fn layout(key: &Key) -> Layout { - Layout::Leaf(LeafLayout::new::(LayoutKey::from(key))) + Layout::Leaf(LeafLayout::from_key::(LayoutKey::from(key))) } } @@ -247,7 +247,7 @@ where T: TypeInfo + 'static + Packed, { fn layout(key: &Key) -> Layout { - Layout::Leaf(LeafLayout::new::(LayoutKey::from(key))) + Layout::Leaf(LeafLayout::from_key::(LayoutKey::from(key))) } } @@ -256,6 +256,6 @@ where T: TypeInfo + 'static + Packed, { fn layout(key: &Key) -> Layout { - Layout::Leaf(LeafLayout::new::(LayoutKey::from(key))) + Layout::Leaf(LeafLayout::from_key::(LayoutKey::from(key))) } }