Skip to content

Feature Request: is it possible to delay running expression until type matches? #35

@clouds56

Description

@clouds56

I'm writing a function use match_type to improve performance, the main idea looks like this

trait MyConvert {
  type Raw: 'static;
  fn into_raw(self) -> Self::Raw;
  fn into_f64(self) -> f64;

  fn into_i64(self) -> i64 {
    match_type!(self.into_raw(), {
      i64 as a => return a,
      i32 as a => return a as i64,
      i128 as a => return a as i64,
      _ => {},
    });
    self.into_f64() as i64
  }
}

The into_raw and into_f64 are required method in trait.
Now this code couldn't compile without Self: Copy, shall we improve it like this

match_type!(self.into_raw(): Self::Raw, {
  i64 as a => return a,
  i32 as a => return a as i64,
  i128 as a => return a as i64
}

and check CastToken::<Self::Raw>::of() == CastToken::<i32>::of() first before self.into_raw(), so that self wouldn't be consumed in unmatched arms.

We could keep both 2 usages:

  • if explicit type exists, we do the lazy evaluation,
  • when it presence, keep it as is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions