Skip to content

Commit

Permalink
Improve ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
danaugrs committed Nov 10, 2024
1 parent a05015d commit 1237489
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 20 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Provides a macro to simplify operator overloading. See the [documentation](https
```rust
extern crate overload;
use overload::overload;
use std::ops; // <- don't forget this or you'll get nasty errors

#[derive(PartialEq, Debug)]
struct Val {
Expand All @@ -24,28 +23,28 @@ overload!((a: ?Val) + (b: ?Val) -> Val { Val { v: a.v + b.v } });
The macro call in the snippet above generates the following code:

```rust
impl ops::Add<Val> for Val {
impl std::ops::Add<Val> for Val {
type Output = Val;
fn add(self, b: Val) -> Self::Output {
let a = self;
Val { v: a.v + b.v }
}
}
impl ops::Add<&Val> for Val {
impl std::ops::Add<&Val> for Val {
type Output = Val;
fn add(self, b: &Val) -> Self::Output {
let a = self;
Val { v: a.v + b.v }
}
}
impl ops::Add<Val> for &Val {
impl std::ops::Add<Val> for &Val {
type Output = Val;
fn add(self, b: Val) -> Self::Output {
let a = self;
Val { v: a.v + b.v }
}
}
impl ops::Add<&Val> for &Val {
impl std::ops::Add<&Val> for &Val {
type Output = Val;
fn add(self, b: &Val) -> Self::Output {
let a = self;
Expand Down
2 changes: 1 addition & 1 deletion src/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ macro_rules! _overload_assignment {
#[macro_export(local_inner_macros)]
macro_rules! _overload_assignment_internal {
($op_trait:ident, $op_fn:ident, $li:ident, $lt:ty, $ri:ident, $rt:ty, $body:block) => (
impl ops::$op_trait<$rt> for $lt {
impl std::ops::$op_trait<$rt> for $lt {
fn $op_fn(&mut self, $ri: $rt) {
let $li = self;
$body
Expand Down
2 changes: 1 addition & 1 deletion src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ macro_rules! _overload_binary {
#[macro_export(local_inner_macros)]
macro_rules! _overload_binary_internal {
($op_trait:ident, $op_fn:ident, $li:ident, $lt:ty, $ri:ident, $rt:ty, $out:ty, $body:block) => (
impl ops::$op_trait<$rt> for $lt {
impl std::ops::$op_trait<$rt> for $lt {
type Output = $out;
fn $op_fn(self, $ri: $rt) -> Self::Output {
let $li = self;
Expand Down
11 changes: 2 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! ```
//! extern crate overload;
//! use overload::overload;
//! use std::ops; // <- don't forget this or you'll get nasty errors
//! ```
//!
//! # Introduction
Expand All @@ -20,7 +19,6 @@
//! ```
//! # extern crate overload;
//! # use overload::overload;
//! # use std::ops;
//! # #[derive(PartialEq, Debug)]
//! # struct Val {
//! # v: i32
Expand All @@ -41,7 +39,6 @@
//! ```
//! # extern crate overload;
//! # use overload::overload;
//! # use std::ops;
//! # #[derive(PartialEq, Debug)]
//! # struct Val {
//! # v: i32
Expand All @@ -56,7 +53,6 @@
//! ```
//! # extern crate overload;
//! # use overload::overload;
//! # use std::ops;
//! # #[derive(PartialEq, Debug)]
//! # struct Val {
//! # v: i32
Expand All @@ -67,7 +63,6 @@
//! ```
//! # extern crate overload;
//! # use overload::overload;
//! # use std::ops;
//! # #[derive(PartialEq, Debug)]
//! # struct Val {
//! # v: i32
Expand All @@ -83,7 +78,6 @@
//! ```
//! # extern crate overload;
//! # use overload::overload;
//! # use std::ops;
//! # #[derive(PartialEq, Debug)]
//! # struct Val {
//! # v: i32
Expand Down Expand Up @@ -128,7 +122,6 @@
//! ```
//! # extern crate overload;
//! # use overload::overload;
//! # use std::ops;
//! # #[derive(PartialEq, Debug)]
//! # struct Val {
//! # v: i32
Expand All @@ -150,8 +143,8 @@
//!
//! The last line of the body needs to be an expression (i.e. no `;` at the end of the line) of type `<out_type>`.
//!
//! | Operator | Example | Trait |
//! |----------|-----------------------------------------------------------------|--------|
//! | Operator | Example | Trait |
//! |----------|------------------------------------------------------------------|--------|
//! | + | `overload!((a: A) + (b: B) -> C { /*...*/ });` | Add |
//! | - | `overload!((a: A) - (b: B) -> C { /*...*/ });` | Sub |
//! | * | `overload!((a: A) * (b: B) -> C { /*...*/ });` | Mul |
Expand Down
2 changes: 1 addition & 1 deletion src/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! _overload_unary {
#[macro_export(local_inner_macros)]
macro_rules! _overload_unary_internal {
($op_trait:ident, $op_fn:ident, $i:ident, $t:ty, $out:ty, $body:block) => (
impl ops::$op_trait for $t {
impl std::ops::$op_trait for $t {
type Output = $out;
fn $op_fn(self) -> Self::Output {
let $i = self;
Expand Down
1 change: 0 additions & 1 deletion tests/assignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate overload;
use overload::overload;
use std::ops;

#[derive(PartialEq, Debug)]
struct A(i32);
Expand Down
1 change: 0 additions & 1 deletion tests/binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate overload;
use overload::overload;
use std::ops;

#[derive(PartialEq, Debug)]
struct A(i32);
Expand Down
1 change: 0 additions & 1 deletion tests/unary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate overload;
use overload::overload;
use std::ops;

#[derive(PartialEq, Debug)]
struct A(i32);
Expand Down

0 comments on commit 1237489

Please sign in to comment.