Skip to content

Add #[mul(forward_and_scalar)] #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Cannedfood
Copy link

@Cannedfood Cannedfood commented Mar 9, 2025

Resolves #361

Synopsis

It currently isn't possible to use #[derive(Mul)] to implement multiplication with itself AND with a scalar at the same time.

Example:

fn without_forward() {
    #[derive(Clone, Copy, Mul)]
    pub struct Vec2 {
        pub x: f32,
        pub y: f32,
    }

    let a = Vec2 { x: 1.0, y: 2.0 };
    let c = a * Vec2 { x: 1.0, y: 1.0 }; // ❌ Doesn't work
    let d = a * 2.0;                     // ✔️ Works
}

fn with_forward() {
    #[derive(Clone, Copy, Mul)]
    #[mul(forward)]
    pub struct Vec2 {
        pub x: f32,
        pub y: f32,
    }

    let a = Vec2 { x: 1.0, y: 2.0 };
    let c = a * Vec2 { x: 1.0, y: 1.0 }; // ✔️ Works
    let d = a * 2.0;                     // ❌ Doesn't work
}

Solution

This commit adds a new attribute forward_and_scalar (feel free to suggest a better name) that enables both:

fn with_forward_and_scalar() {
    #[derive(Clone, Copy, Mul)]
    #[mul(forward_and_scalar)]
    pub struct Vec2 {
        pub x: f32,
        pub y: f32,
    }

    let a = Vec2 { x: 1.0, y: 2.0 };
    let c = a * Vec2 { x: 1.0, y: 1.0 }; // ✔️ Works
    let d = a * 2.0;                     // ✔️ Works
}

Checklist

  • Documentation is updated (if required)
  • Tests are added/updated (if required)
  • CHANGELOG entry is added (if required)

@Cannedfood Cannedfood marked this pull request as ready for review March 9, 2025 12:01
@Cannedfood Cannedfood changed the title Add mul(forward_and_scalar) Add #[mul(forward_and_scalar)] Mar 9, 2025
It currently isn't possible to use `#[derive(Mul)]` to implement multiplication with itself AND with a scalar at the same time.

Example:
```rust
fn without_forward() {
    #[derive(Clone, Copy, Mul)]
    pub struct Vec2 {
        pub x: f32,
        pub y: f32,
    }

    let a = Vec2 { x: 1.0, y: 2.0 };
    let c = a * Vec2 { x: 1.0, y: 1.0 }; // ❌ Doesn't work
    let d = a * 2.0; // ✔️ Works
}

fn with_forward() {
    #[derive(Clone, Copy, Mul)]
    #[mul(forward)]
    pub struct Vec2 {
        pub x: f32,
        pub y: f32,
    }

    let a = Vec2 { x: 1.0, y: 2.0 };
    let c = a * Vec2 { x: 1.0, y: 1.0 }; // ✔️ Works
    let d = a * 2.0; // ❌ Doesn't work
}
```

With this commit, you can have both:
```rust
fn with_forward_and_scalar() {
    #[derive(Clone, Copy, Mul)]
    #[mul(forward_and_scalar)]
    pub struct Vec2 {
        pub x: f32,
        pub y: f32,
    }

    let a = Vec2 { x: 1.0, y: 2.0 };
    let c = a * Vec2 { x: 1.0, y: 1.0 }; // ✔️ Works
    let d = a * 2.0; // ✔️ Works
}
```
@Cannedfood Cannedfood force-pushed the feature/forward_and_scalar branch from 83bdac9 to 786730f Compare April 23, 2025 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Impossible to have both Mul with self and with scalar
1 participant