dimforge / dimforge/nalgebra

Custom floating point type for Matrices/Vectors

Open
#1,092 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

Hi,
I'm trying to use custom floating-point types (essentially a wrapper that tracks some additional information) but can't seem to figure out which trait I should implement that my `Foo` type can then be used in a Matrix/Vector type. Ideally, I would like the following to be possible:

```rust
#[derive(Debug, Clone, Copy)]
pub struct Foo(T);

fn main() {
type Vector3Foo = Vector3>;

let x = Vector3Foo::new(Foo(2.0), Foo(2.0), Foo(2.0));

let b = x * 2.0;
println!("{}", b);
}
```

For the sake of simplicity, I just focus on the Mul trait here by implementing the following traits:

```rust
impl PartialEq for Foo {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl> std::ops::Mul> for Foo {
type Output = Foo;

fn mul(self, rhs: Foo) -> Self::Output {
Foo(self.0 * rhs.0)
}
}

impl> std::ops::Mul for Foo {
type Output = Foo;

fn mul(self, rhs: T) -> Self::Output {
Foo(self.0 * rhs)
}
}

impl Mul> for f32 {
type Output = Foo;

fn mul(self, rhs: Foo) -> Self::Output {
Foo(self * rhs.0)
}
}

```

The above main (i.e. the multiplication by 2.0) still doesn't compile. Is there a way to achieve what I want without creating yet another wrapper around the `Vector3Foo` and reimplementing all the Mul traits?

Best,
Philippe

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.