Skip to content

Commit

Permalink
doc: new macros
Browse files Browse the repository at this point in the history
  • Loading branch information
milancermak committed Aug 23, 2024
1 parent c1dc474 commit 312f481
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
62 changes: 60 additions & 2 deletions packages/macros/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
```
16 changes: 8 additions & 8 deletions packages/macros/src/num_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/macros/src/zero_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 312f481

Please sign in to comment.