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

docs: Improve substitute_type rules and alternatives #1884

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mittal-parth
Copy link

  • Clarifies path substitution rules
  • Adds the recommend alternative of type conversion

Closes #1464

@mittal-parth mittal-parth requested a review from a team as a code owner December 12, 2024 20:31
@mittal-parth mittal-parth changed the title docs: Improve docs for substitute_type docs: Improve substitute_type rules and alternatives Dec 12, 2024
///
/// ### Important Notes
/// - The path to replace must be an absolute path (fully qualified)
/// - The replacement type must implement the necessary traits for encoding and decoding
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we can mention the traits here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woudln't that be dynamic?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 181 to +189
/// #[subxt::subxt(
/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
/// substitute_type(path = "sp_arithmetic::per_things::Perbill", with = "crate::Foo")
/// )]
/// mod polkadot {}
///
/// # #[derive(
/// # scale_encode::EncodeAsType,
/// # scale_decode::DecodeAsType,
/// # codec::Encode,
/// # codec::Decode,
/// # Clone,
/// # Debug,
/// # )]
/// // In reality this needs some traits implementing on
/// // it to allow it to be used in place of Perbill:
/// pub struct Foo(u32);
/// # impl codec::CompactAs for Foo {
/// # type As = u32;
/// # fn encode_as(&self) -> &Self::As {
/// # &self.0
/// # }
/// # fn decode_from(x: Self::As) -> Result<Self, codec::Error> {
/// # Ok(Foo(x))
/// # }
/// # }
/// # impl From<codec::Compact<Foo>> for Foo {
/// # fn from(v: codec::Compact<Foo>) -> Foo {
/// # v.0
/// # }
/// # }
/// # fn main() {}
/// mod polkadot {
/// // Custom type to replace Perbill
/// pub struct Foo(u32);
/// // Note: Foo must implement required traits
/// }
Copy link
Collaborator

@jsdw jsdw Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me offhand; the substititue path is "crate::Foo" but then we move the type into crate::polkadot::Foo? I'd just keep the type defined at the top level to avoid anybody thinking that there is a requirement to put the substitute type into this mod with the attr on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unclear path substition rules
3 participants