Skip to content

Add generic GetInto type converter for Get trait#909

Merged
bkchr merged 7 commits intoparitytech:masterfrom
Doordashcon:DDC-GetInto
Apr 9, 2025
Merged

Add generic GetInto type converter for Get trait#909
bkchr merged 7 commits intoparitytech:masterfrom
Doordashcon:DDC-GetInto

Conversation

@Doordashcon
Copy link

Description

Introduces GetInto<Inner, I, R>, a generic converter for the Get trait that leverages Into for type-safe conversions.

Issue

Currently, type conversions using the Get trait require manual implementation for each specific type pair (e.g., ConvertU16ToU32).

Copy link

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

First thought of this alternative, but seeing how you use it in the downstream PR, I see that a struct is more useful https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=de287e5559e1e1d8bb79c9532ee2bfba

@bkchr
Copy link
Member

bkchr commented Apr 3, 2025

@Doordashcon please format the code :)

@Doordashcon Doordashcon changed the title Add generic GetInto type converter for Get trait Add generic GetInto trait converter for Get trait Apr 6, 2025
@Doordashcon
Copy link
Author

please review @kianenigma @bkchr

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Yeah not sure why you changed this?

@Doordashcon Doordashcon changed the title Add generic GetInto trait converter for Get trait Add generic GetInto type converter for Get trait Apr 7, 2025
@bkchr bkchr merged commit a2b580d into paritytech:master Apr 9, 2025
6 checks passed
@bkontur
Copy link

bkontur commented Apr 17, 2025

@Doordashcon Is this already accessible in polkadot-sdk? Maybe we could use it here: https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/message-queue/src/lib.rs#L1720-L1727 instead of?

pub struct IntoU32<T, O>(core::marker::PhantomData<(T, O)>);
impl<T: Get<O>, O: Into<u32>> Get<u32> for IntoU32<T, O> {
	fn get() -> u32 {
		T::get().into()
	}
}

@bkchr
Copy link
Member

bkchr commented Apr 17, 2025

@bkontur the linked struct is the reason this pr was created. We did not yet release the changes to crates.io. If you need them, go ahead and release a new version :)

@bkontur
Copy link

bkontur commented Apr 17, 2025

@bkontur the linked struct is the reason this pr was created. We did not yet release the changes to crates.io. If you need them, go ahead and release a new version :)

No, I don't need them. I was just reviewing Serban's PR for the message queue, and that IntoU32 reminded me of this GetInto PR

@bkchr
Copy link
Member

bkchr commented Apr 17, 2025

Ahh no we added it for this pr, but yeah the idea is that we will use it in Polkadot sdk :)

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.

4 participants