diff --git a/packages/macros/README.md b/packages/macros/README.md index 1a8aefbe..d310fafc 100644 --- a/packages/macros/README.md +++ b/packages/macros/README.md @@ -2,12 +2,70 @@ A collection of useful macros. -- [`pow!`](#pow) +- [Add, Sub, Mul, Div derives](#add-sub-mul-div-derives) +- [AddAssign, SubAssign, MulAssign, DivAssign derives](#addassign-subassign-mulassign-divassign-derives) +- [pow!](#pow) +- [Zero derive](#zero-derive) -## `pow!` +## Add, Sub, Mul, Div derives + +These macros add the ability to use addition `+`, subtraction `-`, multiplication `*` and division `/` operators on the type that derives them. All individual members of the struct must already support a particular operation for a derive to work. + +```rust +#[derive(Add, Sub, Mul, Div)] +struct Point { + x: u32, + y: u32 +} + +let a = Point { x: 6, y: 32 }; +let b = Point { x: 2, y: 2 }; +let c = a + b; // Point { x: 8, y: 34 } +let d = a - b; // Point { x: 4, y: 30 } +let e = a * b; // Point { x: 12, y: 64 } +let f = a / b; // Point { x: 3, y: 16 } +``` + +## AddAssign, SubAssign, MulAssign, DivAssign derives + +These macros add the ability to use add and assign `+=`, subtract and assign `-=`, multiply and assign `*=` and divide and assign `/=` operators on the type that derives them. All individual members of the struct must already support the particular operation for a derive to work. + +```rust +#[derive(AddAssign, SubAssign, MulAssign, DivAssign)] +struct Point { + x: u32, + y: u32 +} + +let mut a = Point { x: 6, y: 32 }; +let b = Point { x: 2, y: 2 }; +a += b; // Point { x: 8, y: 34 } +a -= b; // Point { x: 6, y: 32 } +a *= b; // Point { x: 12, y: 64 } +a /= b; // Point { x: 6, y: 32 }; +``` + +## pow! Power function. Takes two arguments, `x, y`, calculates the value of `x` raised to the power of `y`. ```cairo const MEGABYTE: u64 = pow!(2, 20); // will be set to 1048576 ``` + +## Zero derive + +Adds implementation of the `core::num::traits::Zero` trait. + +All members of the struct must already implement the `Zero` trait. + +```rust +#[derive(Zero, PartialEq, Debug)] +struct Point { + x: u64, + y: u64, +} +assert_eq!(Point { x: 0, y: 0 }, Zero::zero()); +assert!(Point { x: 0, y: 0 }.is_zero()); +assert!(Point { x: 1, y: 0 }.is_non_zero()); +``` diff --git a/packages/macros/src/num_traits.rs b/packages/macros/src/num_traits.rs index a1d670bb..dc455ace 100644 --- a/packages/macros/src/num_traits.rs +++ b/packages/macros/src/num_traits.rs @@ -92,7 +92,7 @@ of core::ops::{1}Assign<{0}{3}, {0}{3}> {{ ) } -/// Adds implementation for the `Add` trait. +/// Adds implementation for the `core::traits::Add` trait. /// /// Allows you to use the `+` oprator on a type. All members of /// the struct must already implement the `Add` trait. @@ -109,7 +109,7 @@ pub fn add(token_stream: TokenStream) -> ProcMacroResult { ProcMacroResult::new(TokenStream::new(generate_op_trait_impl(&op, &s))) } -/// Adds implementation for the `Sub` trait. +/// Adds implementation for the `core::traits::Sub` trait. /// /// Allows you to use the `-` operator on a type. All members of /// the struct must already implement the `Sub` trait. @@ -125,7 +125,7 @@ pub fn sub(token_stream: TokenStream) -> ProcMacroResult { ProcMacroResult::new(TokenStream::new(generate_op_trait_impl(&op, &s))) } -/// Adds implementation for the `Mul` trait. +/// Adds implementation for the `core::traits::Mul` trait. /// /// Allows you to use the `*` operator on a type. All members of /// the struct must already implement the `Mul` trait. @@ -141,7 +141,7 @@ pub fn mul(token_stream: TokenStream) -> ProcMacroResult { ProcMacroResult::new(TokenStream::new(generate_op_trait_impl(&op, &s))) } -/// Adds implementation for the `Div` trait. +/// Adds implementation for the `core::traits::Div` trait. /// /// Allows you to use the `/` operator on a type. All members of /// the struct must already implement the `Div` trait. @@ -157,7 +157,7 @@ pub fn div(token_stream: TokenStream) -> ProcMacroResult { ProcMacroResult::new(TokenStream::new(generate_op_trait_impl(&op, &s))) } -/// Adds implementation for the `AddAssign` trait. +/// Adds implementation for the `core::ops::AddAssign` trait. /// /// Allows you to use the `+=` operator on a type. All members of /// the struct must already implement the `AddAssign` trait. @@ -173,7 +173,7 @@ fn add_assign(token_stream: TokenStream) -> ProcMacroResult { ProcMacroResult::new(TokenStream::new(generate_op_assign_trait_impl(&op, &s))) } -/// Adds implementation for the `SubAssign` trait. +/// Adds implementation for the `core::ops::SubAssign` trait. /// /// Allows you to use the `-=` operator on a type. All members of /// the struct must already implement the `SubAssign` trait. @@ -189,7 +189,7 @@ fn sub_assign(token_stream: TokenStream) -> ProcMacroResult { ProcMacroResult::new(TokenStream::new(generate_op_assign_trait_impl(&op, &s))) } -/// Adds implementation for the `MulAssign` trait. +/// Adds implementation for the `core::ops::MulAssign` trait. /// /// Allows you to use the `*=` operator on a type. All members of /// the struct must already implement the `MulAssign` trait. @@ -205,7 +205,7 @@ fn mul_assign(token_stream: TokenStream) -> ProcMacroResult { ProcMacroResult::new(TokenStream::new(generate_op_assign_trait_impl(&op, &s))) } -/// Adds implementation for the `DivAssign` trait. +/// Adds implementation for the `core::ops::DivAssign` trait. /// /// Allows you to use the `/=` operator on a type. All members of /// the struct must already implement the `DivAssign` trait. diff --git a/packages/macros/src/zero_trait.rs b/packages/macros/src/zero_trait.rs index 3b71d25e..8b46db3a 100644 --- a/packages/macros/src/zero_trait.rs +++ b/packages/macros/src/zero_trait.rs @@ -58,7 +58,7 @@ of core::num::traits::Zero<{0}{2}> {{ ) } -/// Adds implementation of the `Zero` trait. +/// Adds implementation of the `core::num::traits::Zero` trait. /// /// All members of the struct must already implement the `Zero` trait. ///