icerpc / slicec

The Slice compiler library
Apache License 2.0
13 stars 5 forks source link

Split object-unsafe attribute functions into separate trait. #604

Closed InsertCreativityHere closed 1 year ago

InsertCreativityHere commented 1 year ago

Rust does not allow dynamic trait methods to have type parameters, but some of our attribute functions need to be generic.

We worked around this by adding a Self: Sized type bound. This lets the trait still be usable as a type, but means we don't have access to the functions from the trait. For example:

let x: &dyn Attributable = ...;
x.has_attribute(...);

will not work. Even though has_attribute is defined in Attributable, it isn't reachable because of the Self: Sized. Same for Entity, and all the other traits that involve Attributable.

In these places, instead of being able to use the function, we copy/paste the function's implementation instead. This is not great.


This PR gives us the best of both worlds by splitting the Attributable trait into 2 traits:

Finally, we blanket implement AttributeFunctions for Attributable. This means anything that implements Attributable can use these functions, but without restricting the type of Attributable to only Sized types.


TL;DR

Look at the last line of this PR's changes. We can use the attribute functions like normal now.