Skip to content

Provide macro derive for CheckedAdd, CheckedSub, etc. #25

@esoterra

Description

@esoterra

I would like to be able to derive the traits for CheckedAdd, CheckedSub, and the other checked arithmetic for structs whose members implement those traits.

A simple solution would be to generate impl blocks that look something like the following.

struct Position {
   x: u32,
   y: u32
}

impl CheckedSub for Position { 
   fn checked_sub(&self, v: &Self) -> Option<Self> {
      match ( self.x.checked_sub(v.x), self.y.checked_sub(v.y) ) {
         (Some(x), Some(y)) => Some( Position { x, y } ),
         _ => None
      } 
   }
}

I had initially suggested this be added to derive_more, but the owner has indicated that derive_more is meant to provide support for std traits not traits from crates like num-traits.
JelteF/derive_more#77

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions