Skip to content

Remove Add requirement for Zero. Remove Mul requirement for One. - #388

Closed
jevgarrido wants to merge 1 commit into
rust-num:mainfrom
jevgarrido:main
Closed

jevgarrido wants to merge 1 commit into
rust-num:mainfrom
jevgarrido:main

Conversation

@jevgarrido

@jevgarrido jevgarrido commented Nov 2, 2026 •

Copy link
Copy Markdown

Removing requirement Add from trait Zero.
Removing requirement Mul from trait One.

Broadens domain of applicability for traits Zero and One.
Semver-compatible modification

See #364

Visibility on consumer side

Zero does not imply Add anymore

This will now not compile

fn my_fun( input: T ) -> T
   where T: Zero
{
    input + T::zero()   
}

But this will

fn my_fun( input: T ) -> T
   where T: Zero + Add< T, Output = T >
{ //               ^^^^^^^^^^^^^^^^^^^^
    input + T::zero()   
}

So the Add and Mul operations become more explicit.

In practice this is not really noticeable because everyone just uses Float or Num if they want to add or multiply generically.

Can implement One without Mul (and Zero without Add)

This will now compile:

#[derive(Debug)]
struct MyStruct<T>(T);

use num_traits::One;

impl<T: One> One for MyStruct<T> {
    fn one() -> Self {
        Self( T::one() )
    }
}

// no need to implement Mul.

fn foo() {
    let recurse : MyStruct< MyStruct<f64> > = MyStruct( MyStruct::one() );

    println!( "{:?}", recurse );
}

This way we are able to use the Zero and One constructors right away, without worrying about the logic of adding or multiplying upfront.


Closes #364

@cuviper

cuviper commented Nov 2, 2026

Copy link
Copy Markdown
Member

Semver-compatible modification

It is not, as already explained on the issue.

This will now not compile
[...]
In practice this is not really noticeable

No, we're not going to just hand-wave that away.

@cuviper cuviper closed this Nov 2, 2026
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.

Why does One require Mul?

2 participants