Skip to content

impl on type aliases #32357

Closed
Closed
@calebmer

Description

@calebmer

To provide a nice interface to users I would like to be able to add some impl functions to a type alias. For example, I have the following code:

type Schema = Box<SchemaTrait + 'static>;

trait SchemaTrait {}

struct SchemaNumber {}
impl SchemaTrait for SchemaNumber {}

I would like to add the following:

impl Schema {
  fn new_number() -> Schema {
    Box::new(SchemaNumber::new())
  }
}

So the following would work:

Schema::new_number()

But this would not:

Box::new_number() // Fails, method is only for the type alias.

Another use case is I have a map representing a JSON object (BTreeMap<Key, Value>) that I would like to add custom methods to without having to write a struct that redefines all of the BTreeMap methods. This currently cannot be done.

As a side note, this feels a little like classical inheritance…

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions