Unexpected `Sized` requirement


#1

The following code snippet:

fn main() {
}

trait Inner {
    fn inner_add(&self, &Outer);
}

trait Outer {
    fn inner(&self) -> &Inner;

    fn add(&self) {
        self.inner().inner_add(self); 
    } 
}

produces this error:

src/main.rs:13:32: 13:36 error: the trait `core::marker::Sized` is not implemented for the type `Self` [E0277]
src/main.rs:13         self.inner().inner_add(self);
                                              ^~~~

Playground link

I don’t understand why the Sized is needed, since I’m only ever using references.

A little background, I’m trying to create an object safe wrapper for something whose implementation can either be memory based, or use an SQL database. The Outer is needed to hold the database transaction (which has lifetime requirements that were removed from this example).


#2

Outer is the name of a trait, so &Outer is the type that is a reference to the object type of that trait. self instead is a reference to a type that implements Outer. The conversion from reference to type that implements Outer to &Outer requires that the type is Sized.


#3

Let me make sure I understand. Is this because &self isn’t necessarily implemented as a simple pointer? For example a slice reference will also include a length.


#4

Yes that’s right. If self is any fat pointer, including for example another trait object reference.