Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add matches_prefix function to MultiLocation and Junctions #4827

Closed
wants to merge 1 commit into from

Conversation

apopiak
Copy link
Contributor

@apopiak apopiak commented Feb 1, 2022

What it says on the tin 😉

Motivated by paritytech/cumulus#936 (comment)

Key motivation: match_and_split only allows a single Junction after the prefix but it can be useful to be able to just check for a prefix and ignore what comes after.

Possible extension: We could introduce a corresponding match_and_split_tail (or similar, just a naming suggestion) to allow splitting off arbitrary suffixes.

@apopiak apopiak added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit. T6-XCM This PR/Issue is related to XCM. labels Feb 1, 2022
@apopiak apopiak requested a review from KiChjang February 1, 2022 16:39
@apopiak apopiak requested a review from shawntabrizi February 1, 2022 16:48
/// assert!(!m.matches_prefix(&X1(PalletInstance(3))));
/// # }
/// ```
pub fn matches_prefix(&self, prefix: &MultiLocation) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pub fn matches_prefix(&self, prefix: &MultiLocation) -> bool {
pub fn starts_with(&self, prefix: &MultiLocation) -> bool {

Sounds more like the other methods available in rust

if self.len() < prefix.len() {
return false
}
for i in 0..prefix.len() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for i in 0..prefix.len() {
prefix.iter().zip(self.iter()).all(|l, r| l == r)

This could be used to replace the rest of the function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's how I implemented it in Cumulus, but here I figured I'd keep to the imperative style of match_and_split 😄
Can definitely do.

/// # Example
/// ```rust
/// # use xcm::v0::{Junction::*, MultiLocation::*};
/// # fn main() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// # fn main() {

Rust does this automatically

/// assert!(m.matches_prefix(&m));
/// assert!(!m.matches_prefix(&X2(Parent, GeneralIndex(99))));
/// assert!(!m.matches_prefix(&X1(PalletInstance(3))));
/// # }
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// # }

Copy link
Contributor

@KiChjang KiChjang left a comment

Choose a reason for hiding this comment

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

Code looks good, but was wondering if match_and_split can use match_prefix/starts_with underneath as well.

@KiChjang
Copy link
Contributor

KiChjang commented Feb 2, 2022

Code should also be made against the XCMv3 branch instead of master, i.e. against gav-xcm-v3. cc @gavofyork

Copy link
Member

@gavofyork gavofyork left a comment

Choose a reason for hiding this comment

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

Should be against XCM v3 branch. Also the various suggested by @bkchr

@gavofyork gavofyork added A5-grumble and removed A0-please_review Pull request needs code review. labels Feb 2, 2022
@apopiak
Copy link
Contributor Author

apopiak commented Feb 2, 2022

suderseded by #4835

@apopiak apopiak closed this Feb 2, 2022
@bkchr bkchr deleted the apopiak/match-prefix branch February 2, 2022 22:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit. T6-XCM This PR/Issue is related to XCM.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants