3

I have seen .into() used several times like frame_system::RawOrigin::Signed(who).into(). From what I understand it does some conversion but the docs for into do not make it clear (to me) how to know what it is converting to. For context a specific example I am interested in comes from sudo_as() in the sudo module.

        fn sudo_as(origin, who: <T::Lookup as StaticLookup>::Source, call: Box<<T as Trait>::Call>) {
            // This is a public call, so we ensure that the origin is some signed account.
            let sender = ensure_signed(origin)?;
            ensure!(sender == Self::key(), Error::<T>::RequireSudo);

            let who = T::Lookup::lookup(who)?;

            let res = match call.dispatch(frame_system::RawOrigin::Signed(who).into()) {
                Ok(_) => true,
                Err(e) => {
                    sp_runtime::print(e);
                    false
                }
            };

            Self::deposit_event(RawEvent::SudoAsDone(res));
        }
zeke
  • 320
  • 1
  • 6

1 Answers1

4

You are correct that into will return "the right" type depending on the context from which it was called. In the example you provide, you can look at the docs for the dispatch function and see that it requires an Origin as a parameter. However, as you can see the who variable is being used to create a RawOrigin type. So the into function is being used to convert the provided type (RawOrigin) to whatever type is needed (Origin, in this case).

Dan Forbes
  • 2,734
  • 3
  • 30
  • 60
  • I think you can go a little further and see that under [**Blanket Implementations**](https://substrate.dev/rustdocs/v2.0.0-alpha.7/frame_system/enum.RawOrigin.html#blanket-implementations) the `RawOrigin` type implements an `Into` trait for `T`, where `T` is an `AccountId`. – Dan Forbes May 10 '20 at 18:13
  • 1
    And even further, anything that implements `From` also implement `Into`. https://doc.rust-lang.org/std/convert/trait.From.html `One should always prefer implementing From over Into because implementing From automatically provides one with an implementation of Into thanks to the blanket implementation in the standard library.` – Shawn Tabrizi May 10 '20 at 19:07
  • 1
    Great, thanks! You guys explaining the docs helps me get a better understanding of how to read them. – zeke May 10 '20 at 21:58