dimforge / dimforge/nalgebra

Closed{Add,Sub} trait-bound is unnecessarily strict for Point::{add,sub}

Open
#719 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

As background for this issue, I've implemented two newtypes `Coordinate` and `Distance` to wrap an `f32` and use these as basis for nalgebra's `Point` and `Vector` types:
```
use derive_more::{Add, AddAssign, Display};

#[derive(Clone, Copy, Debug, Display, PartialEq, PartialOrd)]
pub struct Coordinate(pub f32);

#[derive(Add, AddAssign, Clone, Copy, Debug, Display, PartialEq, PartialOrd)]
pub struct Distance(pub f32);

impl std::ops::Mul for Distance { ... }
impl std::ops::MulAssign for Distance { ... }

pub use type Point = nalgebra::Point2;
pub use type Vector = nalgebra::Vector2;
```
I have then encountered that subtracting two `Point`s does not work.

I think requiring `N: ClosedSub` for `impl Sub> for Point` is too strict.
`ClosedSub` requires that `N: Sub + SubAssign`. I see no reason why `SubAssign` is required.
Further, the `Output=N` for `Sub` seems unnecessarily restrictive. The `Sub`-impl returns `VectorSum`, which would require that bound, however if it returned `VectorSum::Output, D, D> (where Sub::Output: Scalar)` then `Output` would be allowed to differ from `N`.

I haven't gotten through the generics for `Point::add`, but adding my `Vector` to a `Point` also fails, so I'm assuming that `Point::add` has a similar situation and could be resolved similarly.

The same restriction seems to apply to other types too, e.g. `Vector`, though that is currently not an issue for me.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.