[idea] make PartialOrd method returns custom methods

Hello everyone. I have a new idea for redefine PartialOrd methods returns custom type.

I want to write an ORM library that allows users to pass in lambda expressions, parse them into SQL, and actually execute them. likes this:

pub struct UserModel {
  pub id: i32,
  pub name: String,
  pub age: u16,
}
let ret: Vec<UserModel> = conn.Select::<UserModel>().Where(|p| p.age < 18).ToArray();

If the comparison operator can return a custom type, then when I perform comparison operations, I can record which calculations the user has performed and generate SQL correctly. But the reality is that comparative operations can only return boolean types. This poses a challenge to implementation.

I have to use macros to achieve approximation function, which not only destroys intelligent syntax prompts, but also reduces compilation speed, and the biggest problem is that the syntax is no longer concise

Related topics: Make PartialOrd comparable between different types

I think, in general, you want expression templates for this.

Other related threads/comments:

This just won't happen. Rust operators are not meant to carry arbitrary meanings (unlike cpp). Just use a regular method.

3 Likes

If you want custom syntax in Rust, macros should be the way to go.

  1. Rust Analyzer can see through declarative macros, so if the syntax is expr-shaped and the macro emits the input as an unused closure, you'll still get autocompletion hints.
  2. Using procedural macros definitely increases compilation time. That is unfortunate.
  3. I'm not sure what you mean by "not concise", but you can limit the scope of the macro to the function call, which looks OK to me:
    let ret: Vec<_> = conn.Select::<UserModel>().Where(expr!(p.age < 18)).ToArray();
    
    Eventually we might get postfix macros, which would enable
    let ret: Vec<_> = conn.Select::<UserModel>().Where!(p.age < 18).ToArray();
    
2 Likes