# Allow \<instance as Trait\>::foo()

**URL:** <https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683>\
**Category:** Uncategorized\
**Created:** [October 26, 2018, 12:42pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683 "2018-10-26T12:42:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [October 26, 2018, 12:42pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/1 "2018-10-26T12:42:46Z")

</div>

Allow

```rust
let x = foo;
let y = <x as Foo>::bar();
let z: dyn Foo = &foo;
let w = <z as Foo>::bar();

```

This would need a new autotrait or something, `RequiresObjectAsTraitSyntax`, that is also implicitly disallowed if your function uses `<Type as Foo>` or another function that does so.

it’d be fine to do things like `<Type as Bar>` (where `Bar` doesn’t ROATS) and still accept `dyn Foo`, but anyway the point of this is to be backwards compatible and just allow more than we currently allow.

---

<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:** [October 26, 2018, 12:53pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/2 "2018-10-26T12:53:32Z")

</div>

> [@Soni](#):
>
> ```rust
> let z: dyn Foo = &foo;
> let w = <z as Foo>::bar();
> 
> ```

This specifically seems to require expanding the vtable to include static functions, which could be really useful for some cases.

I'm not sure what the new autotrait is for? The non-dynamic variant seems to just be compiler sugar (which I could definitely see being useful in a few cases to reduce accidentally changing one place when you need to change two, similar to using `sizeof(value)` instead of `sizeof(struct foo)` in C), while the dynamic variant would be just expanding the vtable; where does the autotrait come into those?

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [October 26, 2018, 12:59pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/3 "2018-10-26T12:59:46Z")

</div>

I want these to work:

```rust
#[macro_use]
extern crate eventbus;

use eventbus::{Event, EventBus};

trait MyEvent : Event {
    fn get_i(&self) -> i32;
    fn set_i(&mut self, i32);
}
        
struct MyEventImpl {
    i: i32
}   

impl Event for MyEventImpl {
}
impl MyEvent for MyEventImpl {
    fn get_i(&self) -> i32 {
        self.i
    }
    fn set_i(&mut self, i: i32) {
        self.i = i;
    }
}

fn add_handler(e: &mut MyEvent) {
    /* adds 1 */
    let i = e.get_i();
    e.set_i(i+1);
}

fn no_handler(e: &mut MyEvent) {
    /* does nothing */
}

#[test]
fn test_basic_usage() {
    let event_bus = EventBus::new();
    //let handler_id = event_bus.register(add_handler, 0);
    register_hook!(&event_bus, 0, dyn MyEvent, add_handler);
    let mut event = MyEventImpl { i: 3 };
    assert_eq!(event.i, 3);
    post_event!(&event_bus, dyn MyEvent, &mut event);
    assert_eq!(event.i, 4);
    register_hook!(&event_bus, 1, dyn MyEvent, no_handler);
    post_event!(&event_bus, dyn MyEvent, &mut event);
    assert_eq!(event.i, 5);
    //event_bus.unregister(handler_id);
    post_event!(&event_bus, dyn MyEvent, &mut event);
    //assert_eq!(event.i, 5);
}

```

The autotrait is used for creating bounds, e.g.:

```rust
fn foo1<T: Bar + ?Sized>(x: &T) {
    <T as Bar>::baz(); // T gets an implicit bound `+ !ROATS`
}

fn foo2<T: Bar + ?Sized>(x: &T) {
    <x as Bar>::baz(); // T gets an implicit bound `+ ?ROATS`
}

```

(This is more useful with macros)

---

<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:** [October 26, 2018, 1:17pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/4 "2018-10-26T13:17:19Z")

</div>

> [@Soni](#):
>
> The autotrait is used for creating bounds

But what do those bounds _mean_, those two functions would be identical after compiler desugaring of `<*x as Bar>` to `<T as Bar>` (I assume `<x as Bar>` is a typo since `&T` isn't guaranteed to implement `Bar` in `foo2`) so I don't see what the bounds are meant to change.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [October 26, 2018, 1:18pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/5 "2018-10-26T13:18:46Z")

</div>

it’s meant to use the vtable rather than using the type.

`&dyn Bar` has a vtable, unlike the original `&BarImpl`, and that’s the main difference between them. we just need to make the vtable’s static methods nameable and callable.

---

<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:** [October 26, 2018, 1:21pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/6 "2018-10-26T13:21:37Z")

</div>

Sure, in the case of `dyn Trait` it needs to use a vtable. For statically known types it doesn’t need to though, and since it’s trivial to distinguish the two cases I don’t see why you would still go through a vtable and give the optimizer more work.

Even if it always used a vtable, why does that need an extra bound? I would assume static methods would just be added to all vtables (since it’s dynamic and you can’t know whether they would be needed) and it would just always work.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [October 26, 2018, 1:23pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/7 "2018-10-26T13:23:57Z")

</div>

Because in foo1 there’s no vtable when `T: &dyn Bar` because that’s the type and not the value.

Note that I explicitly propose changing the semantics of `<type>::method()`, if `type` is derived from an object.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [October 26, 2018, 2:50pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/8 "2018-10-26T14:50:59Z")

</div>

> [@Soni](#):
>
> Note that I explicitly propose changing the semantics of `<type>::method()` , if `type` is derived from an object.

Change in what sense? What else should the expression `Foo::bar()` **ever** do other than calling the method `bar` on (an object of) type `Foo`?

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [October 26, 2018, 3:35pm UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/9 "2018-10-26T15:35:46Z")

</div>

It should use the vtable.

---

<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:** [March 25, 2019, 8:17am UTC](https://internals.rust-lang.org/t/allow-instance-as-trait-foo/8683/10 "2019-03-25T08:17:03Z")

</div>

Closed on March 25, 2019, 8:17am UTC

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