Skip to content

RFC: Replace built-in compare operations with an iface #2007

@marijnh

Description

@marijnh

(I don't think this is a big priority, but feel it'd be an improvement on the long term.)

Our comparison operators currently use shape information to walk the compared values using a hard-coded algorithm. I feel it would be cleaner to use an interface for this

  • It'd allow people to write custom comparison code (scoped, so you can implement several styles of comparing for a type, and pick the one you want to use using imports)
  • It'd probably be faster, since these would be completely statically resolved, optionally inlined, rather than interpreting shape strings (or using a walker, which still carries an overhead).
  • It'd unify concepts. Comparing in a generic function is very much like other operations on generic types. Using a parameter bound for that seems sensible, and removes a special case in the monomorphization code, which currently has to treat generics that use comparison operators specially.

A potential problem is that, while we can write sensible generic impls for things like pointer, vec, and tuple types, records would have to be implemented per-case. Since we already have #[auto_serialize], it probably makes sense to also provide an #[auto_cmp] attribute for type items, which would generate a default comparision impl for the type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions