noir icon indicating copy to clipboard operation
noir copied to clipboard

Implement a trait for types implementing some other trait

Open fcarreiro opened this issue 8 months ago • 0 comments

Aim

Implement a trait for types implementing some other trait.

E.g., SerializeToSlice based on Serialize.

trait SerializeToSlice {
    fn serialize_to_slice(self) -> [Field];
}

impl SerializeToSlice for [Field] {
    fn serialize_to_slice(self) -> [Field] {
        self
    }
}

impl<T,N> SerializeToSlice for T where T: Serialize<N> {
    fn serialize_to_slice(self) -> [Field] {
        Serialize::serialize(self).as_slice()
    }
}

This seems to compile but then

fn use_it<T>(t: T) -> [Field] where T: SerializeToSlice {
    SerializeToSlice::serialize_to_slice(t)
}

triggers

error: No matching impl found for `T: Serialize<_>`
    │
390 │     SerializeToSlice::serialize_to_slice(t)
    │     ------------------------------------ No impl for `T: Serialize<_>`
    │
    = Required by `T: SerializeToSlice`

Expected Behavior

Works

Bug

Not sure exactly what's going on.

To Reproduce

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

discussed with @TomAFrench

cc: @nventuro @Thunkar

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

fcarreiro avatar Jun 06 '24 13:06 fcarreiro