Calling trait static method from another static method (rust)

Ant Manelope

Can you call a trait static method implemented by types from another trait static method implemented in the trait? For example:

trait SqlTable {
  fn table_name() -> String;

  fn load(id: i32) -> Something {
    ...
    Self::table_name()    // <-- this is not right
    ...
  }
}

This is now working thanks to Chris and Arjan (see comments/answers below)

fn main() {
  let kiwibank = SqlTable::get_description(15,None::<Account>);
}

trait SqlTable {
    fn table_name(_: Option<Self>) -> String;

    fn get_description(id: i32, _: Option<Self>) -> String {
        println!("Fetching from {} table", SqlTable::table_name(None::<Self>) );
        String::from_str("dummy result")
    }
}

struct Account {
    id: i32,
    name: String,
}
impl SqlTable for Account {
    fn table_name(_: Option<Account>) -> String { String::from_str("account") }
}
Ant Manelope
  • Yes, you can call a trait static method [implemented by types] from another trait static method [implemented in the trait].
  • Static methods are always called on a trait like SomeTrait::some_method().
  • Where there is no Self or self in [a trait] function signature, it is not callable at present. The standard workaround until UFCS comes is to take an argument _: Option<Self> and pass it None::<T>.

See original question for code that (as of today) compiles.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling static method from another java class

From Dev

Calling a non static method from a in a static context

From Dev

Calling non-static method from another non-static method

From Dev

Calling non-static method from another non-static method

From Dev

Call static method from trait on generic type

From Dev

Calling a non-static method from a static method

From Dev

PHP: Get class name of static method calling another static class

From Dev

Calling static method of

From Dev

Call matlab static method from within another static method

From Dev

PHP: Override trait static method

From Dev

PHP: Override trait static method

From Dev

Calling non static method in static method

From Dev

Calling non static method from static method USING instance created in the static method

From Dev

Calling an overridden static method from parent

From Dev

Calling a static void Java method from JNI

From Dev

calling static method from inside the class

From Dev

Calling a static method from NSTimer. Is it possible?

From Dev

Calling a static template method from a template function

From Dev

Calling super static method from designated initializer

From Dev

Calling a static method from cshtml file

From Dev

calling non static method from static class in java with spring autowiring?

From Dev

Calling non-static method from static member in Java

From Dev

Variables wont increase, calling non static methods from static method

From Dev

Calling a static method from a non-static context

From Dev

Is calling a static method from an instance method thread safe?

From Dev

Call non static method from another page

From Dev

Call non static method from another page

From Dev

how to change the value of a static variable from a static method of another class

From Dev

how to change the value of a static variable from a static method of another class

Related Related

  1. 1

    Calling static method from another java class

  2. 2

    Calling a non static method from a in a static context

  3. 3

    Calling non-static method from another non-static method

  4. 4

    Calling non-static method from another non-static method

  5. 5

    Call static method from trait on generic type

  6. 6

    Calling a non-static method from a static method

  7. 7

    PHP: Get class name of static method calling another static class

  8. 8

    Calling static method of

  9. 9

    Call matlab static method from within another static method

  10. 10

    PHP: Override trait static method

  11. 11

    PHP: Override trait static method

  12. 12

    Calling non static method in static method

  13. 13

    Calling non static method from static method USING instance created in the static method

  14. 14

    Calling an overridden static method from parent

  15. 15

    Calling a static void Java method from JNI

  16. 16

    calling static method from inside the class

  17. 17

    Calling a static method from NSTimer. Is it possible?

  18. 18

    Calling a static template method from a template function

  19. 19

    Calling super static method from designated initializer

  20. 20

    Calling a static method from cshtml file

  21. 21

    calling non static method from static class in java with spring autowiring?

  22. 22

    Calling non-static method from static member in Java

  23. 23

    Variables wont increase, calling non static methods from static method

  24. 24

    Calling a static method from a non-static context

  25. 25

    Is calling a static method from an instance method thread safe?

  26. 26

    Call non static method from another page

  27. 27

    Call non static method from another page

  28. 28

    how to change the value of a static variable from a static method of another class

  29. 29

    how to change the value of a static variable from a static method of another class

HotTag

Archive