Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep for v0.6.0 release #20

Merged
merged 2 commits into from
Feb 16, 2024
Merged

Prep for v0.6.0 release #20

merged 2 commits into from
Feb 16, 2024

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented Feb 16, 2024

[v0.6.0] - 2024-02-16

Up until now, scale-info has been the library that gives us the information needed to know how to SCALE encode values to the correct shape. In this release, we remove it from our dependency tree and replace it with scale-type-resolver, which provides a generic TypeResolver trait whose implementations are able to provide the information needed to encode/decode types. So now, rather than taking in a scale_info::PortableRegistry, the EncodeAsType and EncodeAsFields traits take a generic R: scale_type_resolver::TypeResolver value. scale_info::PortableRegistry implements TypeResolver, and so it can continue to be used similarly to before (though now, type_id is passed as a reference), but now we are generic over where the type information we need comes from.

To be more concrete, EncodeAsType used to look roughly like this:

pub trait EncodeAsType {
    fn encode_as_type_to(
        &self,
        type_id: u32,
        types: scale_info::PortableRegistry,
        out: &mut Vec<u8>,
    ) -> Result<(), Error>;
}

And now it looks like this:

pub trait EncodeAsType {
    fn encode_as_type_to<R: TypeResolver>(
        &self,
        type_id: &R::TypeId,
        types: &R,
        out: &mut Vec<u8>,
    ) -> Result<(), Error>;
}

One effect that this has is that EncodeAsType and EncodeAsFields are no longer object safe (since the method they expose accepts a generic type now). Internally this led us to also change how scale_encode::Composite works slightly (see the docs for that for more information). if you need object safety, and know the type resolver that you want to use, then you can make a trait + blanket impl like this which is object safe and is implemented for anything which implements EncodeAsType:

trait EncodeAsTypeWithResolver<R: TypeResolver> {
    fn encode_as_type_with_resolver_to(
        &self,
        type_id: &R::TypeId,
        types: &R,
        out: &mut Vec<u8>,
    ) -> Result<(), Error>;
}
impl<T: EncodeAsType, R: TypeResolver> EncodeAsTypeWithResolver<R> for T {
    fn encode_as_type_with_resolver_to(
        &self,
        type_id: &R::TypeId,
        types: &R,
        out: &mut Vec<u8>,
    ) -> Result<(), Error> {
        self.encode_as_type_to(type_id, types, out)
    }
}

We can now have &dyn EncodeAsTypeWithResolver<SomeConcreteResolver> instances.

The full PR is here:

  • Enable generic type encoding via TypeResolver and remove dependency on scale-info (#19).

Copy link

@tadeohepperle tadeohepperle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jsdw jsdw merged commit b9e12c5 into main Feb 16, 2024
9 checks passed
@jsdw jsdw deleted the release-v0.6.0 branch February 16, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants