# Allowing calling static methods through trait objects

**URL:** <https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417>\
**Category:** Uncategorized\
**Created:** [June 17, 2019, 12:12pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417 "2019-06-17T12:12:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [June 17, 2019, 12:12pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417/1 "2019-06-17T12:12:54Z")

</div>

Right now apparently static methods cannot be called from trait objects. Is there any inherent reason why this is the case? It seems like trait objects could easily grow vtable slots to include static methods?

My motivating use case is a `size_hint()` method used in Askama `Template` trait. It used to take `&self`, but someone reasonably pointed out that, since the size was computed at compile time, the parameter wasn’t needed. However, I then got an issue that templates can now no longer be used as trait objects, which is desirable in some use cases to prevent code size blowup.

I’m happy to write up an RFC if this turns out to be non-controversial, but maybe there’s something I’m overlooking.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [June 17, 2019, 12:16pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417/2 "2019-06-17T12:16:14Z")

</div>

Quite a lot of previous discussion of the issues in [Idea: object-safe static trait methods](https://internals.rust-lang.org/t/idea-object-safe-static-trait-methods/8332)

EDIT: It also briefly mentions a workaround, provide both a static and dynamic method to allow access either way:

```rust
trait Template {
    fn size_hint() -> usize;
    fn dyn_size_hint(&self) -> usize {
        Self::size_hint()
    }
}

```

---

<div class="post-metadata">

**Author:** ![gbutler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gbutler/32/3670_2.png) [@gbutler](https://internals.rust-lang.org/u/gbutler)\
**Post date:** [June 17, 2019, 12:20pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417/3 "2019-06-17T12:20:50Z")

</div>

This seems to indicate that it could be safely done. It seems like if you are able to implement manually this way, then, the compiler could relatively easily grow a vtable to support it.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [June 17, 2019, 12:25pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417/4 "2019-06-17T12:25:11Z")

</div>

So, my idea of having a default delegating implementation for `dyn_size_hint` [doesn’t actually work](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2c1c1d77f26c91fc621b86e3d7aa04ec), there might be some way to structure this so that you don’t need to implement both methods for every implementation, but best I could come up with quickly is

```rust
trait Template {
    fn size_hint() -> usize where Self: Sized;
    fn dyn_size_hint(&self) -> usize;
}

```

I think that not being able to use a default implementation maybe indicates the issue with the compiler automatically deriving it.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [June 17, 2019, 4:45pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417/5 "2019-06-17T16:45:58Z")

</div>

> [@Nemo157](#):
>
> ```rust
> trait Template {
> fn size_hint() -> usize where Self: Sized;
> fn dyn_size_hint(&self) -> usize;
> }
> 
> ```

You can make it _slightly less_ painful with an additional trait. (This still impacts public API.)

```rust
pub trait Template {
    fn size_hint() -> usize where Self: Sized;
}

// the proper trait to use in trait objects
pub trait TemplateDyn: Template {
    fn dyn_size_hint(&self) -> usize;
}

impl<T: Template> TemplateDyn for T {
    fn dyn_size_hint(&self) -> usize {
        Self::size_hint()
    }
}

```

---

<div class="post-metadata">

**Author:** ![mikeyhew](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mikeyhew/32/2270_2.png) [@mikeyhew](https://internals.rust-lang.org/u/mikeyhew)\
**Post date:** [June 17, 2019, 9:29pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417/6 "2019-06-17T21:29:03Z")

</div>

This two-trait solution looks like an improvement, in particular it means that `size_hint` can be called for all `T: Sized + Template`, and `dyn_size_hint` can be called on any `T: TemplateDyn`, including unsized types like `str` and `[u8]` if they implement both traits.

There’s one case that the above doesn’t support: you can’t call `size_hint` (the static version) on unsized types other than the trait object type, such as `str` and `[u8]`. However, with a couple tweaks this is possible:

```rust
trait Template {
  fn size_hint() -> usize;
}

trait TemplateDyn {
  fn dyn_size_hint(&self) -> usize;
}

impl<T: Template + ?Sized> TemplateDyn for T {
  fn dyn_size_hint(&self) -> usize {
    Self::size_hint()
  }
}

```

Here is a playground link using `str` and `i32` as examples of unsized and sized types implementing `Template`: [https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f35eb24390d1ee8f27b284f9dfa73c67](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f35eb24390d1ee8f27b284f9dfa73c67)

If the `Template` trait has other non-static methods, you can either copy them into `TemplateDyn` and delegate to the `Template` implementation in the `impl<T: Template + ?Sized> TemplateDyn for T` block, or use a common subtrait with a name like `TemplateBase`. The first option would be more work for the library maintainer but would provide a slightly nicer API for implementors.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 15, 2019, 9:43pm UTC](https://internals.rust-lang.org/t/allowing-calling-static-methods-through-trait-objects/10417/7 "2019-09-15T21:43:06Z")

</div>

Closed on September 15, 2019, 9:43pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
