Traits: how to specify the origin of a returned ref / pointer

I want to define a trait with a function that returns a reference or a pointer. However, a returned ref or pointer always need an origin.

I can do things like

trait Trait:
    fn fun(self) -> ref [MutableAnyOrigin] Int:
        ...

trait Trait2:
    fn fun_ptr(self) -> Pointer[Int, MutableAnyOrigin]:
        ...

However, I find it hard to conform to this trait when implementing a struct, as the origin always has a specific value.

If I try

@value
struct Struct(Trait):
    var a: Int
    fn fun(self) -> ref [MutableAnyOrigin] Int:
        return self.a

I get an incompatible origin error. If I instead do

@value
struct Struct(Trait):
    var a: Int
    fn fun(self) -> ref [self.a] Int:
        return self.a

I break trait conformance. How do I do this properly?

1 Like