Skip to content

allow types to specify the kind of serializer they should be used with or carry state #3740

Closed
@nikomatsakis

Description

@nikomatsakis

Right now, the serialization interface requires that the type to be serialized be generic with respect to any sort of serializer:

trait Serializable { fn serialize<S: Serializer>(&self, s: &s); }

Unfortunately, some types require context to be serialized, such as ty::t or span. When doing the original design I had intended that this state would be carried in the serializer, but of course if you must write the serialization function generically that didn't work, so we ended up with ast_encode.rs which does some manual serialization for any type that touches ty::t. At first this wasn't a lot of stuff but of course it's growing and it's annoying.

One way to solve this is to modify the Serializable trait as follows:

trait Serializable<S: Serializer> { fn serialize(&self, s: &s); }

This allows a type to implement Serializable only for specific serializers:

impl ty::t: Serializable<RustcSerializer> { ... }

To really make this work, though, it is also necessary for the auto_serialize code to permit generation of an impl that is specialized to a particular serializer, since any struct which contains ty::t values will also only work with RustcSerializer.

Another option would be to extend the Serializable trait with some kind of state and carry that around, but I think that winds up just being more type parameters without really adding any sort of flexibility.

@erickt has done preliminary work but it's hitting an ICE somewhere in the static methods implementation. I haven't tracked down the cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions