overload-0.1.1/.gitignore000064400000000000000000000000361352142362700135060ustar0000000000000000/target **/*.rs.bk Cargo.lock overload-0.1.1/Cargo.toml.orig000064400000000000000000000005171352356764000144170ustar0000000000000000[package] name = "overload" version = "0.1.1" authors = ["Daniel Salvadori "] description = "Provides a macro to simplify operator overloading." license = "MIT" keywords = ["operator", "overloading", "macro", "op"] categories = ["rust-patterns"] repository = "https://github.com/danaugrs/overload" edition = "2018" overload-0.1.1/Cargo.toml0000644000000015340000000000000107060ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "overload" version = "0.1.1" authors = ["Daniel Salvadori "] description = "Provides a macro to simplify operator overloading." keywords = ["operator", "overloading", "macro", "op"] categories = ["rust-patterns"] license = "MIT" repository = "https://github.com/danaugrs/overload" overload-0.1.1/LICENSE000064400000000000000000000021221352232004400125070ustar0000000000000000MIT License Copyright (c) 2019 Daniel Augusto Rizzi Salvadori Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.overload-0.1.1/logo.png000064400000000000000000000453641352233770400132020ustar0000000000000000PNG  IHDRR>bKGD3'| pHYs.#.#x?vtIME:eStEXtCommentCreated with GIMPW IDATxwUMnz$@- IN@t /(]HAjҫ 7i$=$+p=sΙggYai2gfn """"""""""""""""""""""jP6wǁkYJփ뀹<""""""*w g`'`^DDDDDDD[x{`!`:;?0z*Te QK8mMǃuܛ;Tx:p5)0j5WT8v^qXopZ)T?-"""""")ޫIRsO,""""""Y}A)ι(0z6VlÀzT3,Ss]9Wh{h^Y^XƤ@'XE[Vι}l:~ʹ3k<hk~Կxtc XOj5έ# 6zgPOs =k=UMgZ JQDDDDD|ptzھC`|J} vfaCK, <=5 {q"5W\b6T7虈lyNOd14;c09U Uq;KPFz|;X -0$pFwHn#-%n:n6w-V˹nP uI%[sb)""""">i5&p'%EPa="""""C;%A+Rg`q%(vc!`;l1D&XQa{xHz&ߟW:y8ʼ x>~"!"""""IւXgSȃ1XoI}〱mPU8E06# ȆV8K3.m)* y7`rsj#"""""ޕ9&-gXhǴV%}ԓ%[5[n6"""""" 6$z1lL 4d7`a`[:sGyPC}ϮߏSe۬%۱jT]2#""""" gZ?]Q(uP;.su]{jreT[4 P8.spUn """""r^XjEp;л,xۿYH@k&^,9d`KoZx+y^N󫸶'}o`381`z~iem!lT""""">:˓ȂETYcC Эµubw=رWWg?*`< OXDDDDDp+Q\N}#pn$ܻ_5aX< «Xl8|zE 9`X#ZoLz-|x< L+:OJc@l(c꺇ԍ0lx{BIoT?Za,WPuᶷܮOms[;L78 NYDDDDD|gāԗ1@nsٶ#a^UcQP}{~?V>`!Eײ>V5<Q4X04]MwX`RDb=UcC'l1ЃeN42 ;r19r1ēLc=S"*#.OHN!E[ZU",UgXNNSg# ˅++[HzQZ.9@WeI:'SYoЫ9GM X42ADDDDDBpaT/rs@DDDDDֈm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]| pZDםŖkOe/^Jo՘Я`{ -c&p 0 JP<%;`Cǫηx $P+;Hu>nǰ=%Χ1Se,﶐z{U&Cf-|  K./c?j!nM{.-ZOs(W # k1G~<,s1Ly4#pj'-N>F3*C-TnzFN*=p=rsݎ/VȻI7`M*퀏< J0lZY.jԵjO'e"}[|z6i=NI*x;AC-pvӓ \=./:sV&=JV .O_ Pkm^ԷQ((h}Wiq' Z 58$ٔGR{Ue^v滌k[ȋh~ȚLC6z\?Ln )V@# Ob%I?ї@k@="k`l|O RԾ \=1OaP5mU)"9/?P+.K=ȂıX= U$ݫ"߯_DXz{ 9Ju[w,♛]=zJ暬.Sj.4`;YLG5/ Ư %עVbb0k< ۺ==zgl T.9]Ǩ$Q38؜؝m_P`;lj*I4.9>|w13kplD{leW֤Q19Bb)RAIbC][Y 5Rum~ \Y[yF`4пdi84&qgM n_^89$/^s=6bJ[lk9a!oM/T,"BآaSC:&=|4?K'ؔUXI> Hf7?Ӛ"ɣI>8f!`tS]?czMxlAr0&5 п%89w5}<*""""""IfSyJPo""!*"""""Ⓠ|ڞo1TB}jQ. {}x:4գLwnHlTlLKJ뽥7csqv|=y3$O+o*Ӧ xQkTqGgl5ojߓVιC[90jvh[l{z""""""5Áw9#~[5{n.y?uQ/ly?`+kڂ}y)ι(0چWy*EDDDDD$k֎H;{T EU\)9lKNCZE00_}fq҇N5w]wޭ7W80ex+3@:{E`:yG""""""s-nF0=X0y ,l;%fmg;b`SvHL?2."""""")+)gl{~ ?e^=IIENDB`overload-0.1.1/README.md000064400000000000000000000033171352354672700130130ustar0000000000000000

Provides a macro to simplify operator overloading. See the [documentation](https://docs.rs/overload/) for details and supported operators. ## Example ```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 { v: i32 } 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 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 { type Output = Val; fn add(self, b: &Val) -> Self::Output { let a = self; Val { v: a.v + b.v } } } impl ops::Add 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 { type Output = Val; fn add(self, b: &Val) -> Self::Output { let a = self; Val { v: a.v + b.v } } } ``` We are now able to add `Val`s and `&Val`s in any combination: ```rust assert_eq!(Val{v:3} + Val{v:5}, Val{v:8}); assert_eq!(Val{v:3} + &Val{v:5}, Val{v:8}); assert_eq!(&Val{v:3} + Val{v:5}, Val{v:8}); assert_eq!(&Val{v:3} + &Val{v:5}, Val{v:8}); ``` overload-0.1.1/src/assignment.rs000064400000000000000000000025641352207256100150310ustar0000000000000000#[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! _overload_assignment { (+=, $($t:tt)+) => (_overload_assignment_internal!(AddAssign, add_assign, $($t)+);); (-=, $($t:tt)+) => (_overload_assignment_internal!(SubAssign, sub_assign, $($t)+);); (*=, $($t:tt)+) => (_overload_assignment_internal!(MulAssign, mul_assign, $($t)+);); (/=, $($t:tt)+) => (_overload_assignment_internal!(DivAssign, div_assign, $($t)+);); (%=, $($t:tt)+) => (_overload_assignment_internal!(RemAssign, rem_assign, $($t)+);); (&=, $($t:tt)+) => (_overload_assignment_internal!(BitAndAssign, bitand_assign, $($t)+);); (|=, $($t:tt)+) => (_overload_assignment_internal!(BitOrAssign, bitor_assign, $($t)+);); (^=, $($t:tt)+) => (_overload_assignment_internal!(BitXorAssign, bitxor_assign, $($t)+);); (<<=, $($t:tt)+) => (_overload_assignment_internal!(ShlAssign, shl_assign, $($t)+);); (>>=, $($t:tt)+) => (_overload_assignment_internal!(ShrAssign, shr_assign, $($t)+);); } #[doc(hidden)] #[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 { fn $op_fn(&mut self, $ri: $rt) { let $li = self; $body } } ); } overload-0.1.1/src/binary.rs000064400000000000000000000023451352207231700141410ustar0000000000000000#[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! _overload_binary { (+, $($t:tt)+) => (_overload_binary_internal!(Add, add, $($t)+);); (-, $($t:tt)+) => (_overload_binary_internal!(Sub, sub, $($t)+);); (*, $($t:tt)+) => (_overload_binary_internal!(Mul, mul, $($t)+);); (/, $($t:tt)+) => (_overload_binary_internal!(Div, div, $($t)+);); (%, $($t:tt)+) => (_overload_binary_internal!(Rem, rem, $($t)+);); (&, $($t:tt)+) => (_overload_binary_internal!(BitAnd, bitand, $($t)+);); (|, $($t:tt)+) => (_overload_binary_internal!(BitOr, bitor, $($t)+);); (^, $($t:tt)+) => (_overload_binary_internal!(BitXor, bitxor, $($t)+);); (<<, $($t:tt)+) => (_overload_binary_internal!(Shl, shl, $($t)+);); (>>, $($t:tt)+) => (_overload_binary_internal!(Shr, shr, $($t)+);); } #[doc(hidden)] #[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 { type Output = $out; fn $op_fn(self, $ri: $rt) -> Self::Output { let $li = self; $body } } ); } overload-0.1.1/src/lib.rs000064400000000000000000000242351352340506200134230ustar0000000000000000//! Provides a macro to simplify operator overloading. //! //! To use, include the following: //! ``` //! extern crate overload; //! use overload::overload; //! use std::ops; // <- don't forget this or you'll get nasty errors //! ``` //! //! # Introduction //! //! Suppose we have the following `struct` definition: //! ``` //! #[derive(PartialEq, Debug)] //! struct Val { //! v: i32 //! } //! ``` //! We can overload the addition of `Val`s like so: //! ``` //! # extern crate overload; //! # use overload::overload; //! # use std::ops; //! # #[derive(PartialEq, Debug)] //! # struct Val { //! # v: i32 //! # } //! overload!((a: Val) + (b: Val) -> Val { Val { v: a.v + b.v } }); //! ``` //! The macro call above generates the following code: //! ```ignore //! impl ops::Add for Val { //! type Output = Val; //! fn add(self, b: Val) -> Self::Output { //! let a = self; //! Val { v: a.v + b.v } //! } //! } //! ``` //! We are now able to add `Val`s: //! ``` //! # extern crate overload; //! # use overload::overload; //! # use std::ops; //! # #[derive(PartialEq, Debug)] //! # struct Val { //! # v: i32 //! # } //! # overload!((a: Val) + (b: Val) -> Val { Val { v: a.v + b.v } }); //! assert_eq!(Val{v:3} + Val{v:5}, Val{v:8}); //! ``` //! //! # Owned and borrowed types //! //! If we also wanted to overload addition for the borrowed type `&Val` we could write: //! ``` //! # extern crate overload; //! # use overload::overload; //! # use std::ops; //! # #[derive(PartialEq, Debug)] //! # struct Val { //! # v: i32 //! # } //! overload!((a: &Val) + (b: &Val) -> Val { Val { v: a.v + b.v } }); //! ``` //! We might also want to overload addition between the owned and borrowed types: //! ``` //! # extern crate overload; //! # use overload::overload; //! # use std::ops; //! # #[derive(PartialEq, Debug)] //! # struct Val { //! # v: i32 //! # } //! overload!((a: Val) + (b: &Val) -> Val { Val { v: a.v + b.v } }); //! overload!((a: &Val) + (b: Val) -> Val { Val { v: a.v + b.v } }); //! ``` //! Let's see how we can write these combinations more concisely. //! //! We can include a `?` in front of a type to indicate that it should stand in for both the owned and borrowed type. //! //! To overload addition for all four combinations between `Val` and `&Val` we can therefore simply include a `?` in front of both types: //! ``` //! # extern crate overload; //! # use overload::overload; //! # use std::ops; //! # #[derive(PartialEq, Debug)] //! # struct Val { //! # v: i32 //! # } //! overload!((a: ?Val) + (b: ?Val) -> Val { Val { v: a.v + b.v } }); //! ``` //! The macro call above generates the following code: //! ```ignore //! impl ops::Add 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 { //! type Output = Val; //! fn add(self, b: &Val) -> Self::Output { //! let a = self; //! Val { v: a.v + b.v } //! } //! } //! //! impl ops::Add 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 { //! type Output = Val; //! fn add(self, b: &Val) -> Self::Output { //! let a = self; //! Val { v: a.v + b.v } //! } //! } //! ``` //! We are now able to add `Val`s and `&Val`s in any combination: //! ``` //! # extern crate overload; //! # use overload::overload; //! # use std::ops; //! # #[derive(PartialEq, Debug)] //! # struct Val { //! # v: i32 //! # } //! # overload!((a: ?Val) + (b: ?Val) -> Val { Val { v: a.v + b.v } }); //! assert_eq!(Val{v:3} + Val{v:5}, Val{v:8}); //! assert_eq!(Val{v:3} + &Val{v:5}, Val{v:8}); //! assert_eq!(&Val{v:3} + Val{v:5}, Val{v:8}); //! assert_eq!(&Val{v:3} + &Val{v:5}, Val{v:8}); //! ``` //! //! # Binary operators //! //! The general syntax to overload a binary operator between types `` and `` is: //! ```ignore //! overload!((: ) (: ) -> { /*body*/ }); //! ``` //! Inside the body you can use `` and `` freely to perform any computation. //! //! The last line of the body needs to be an expression (i.e. no `;` at the end of the line) of type ``. //! //! | Operator | Example | Trait | //! |----------|-----------------------------------------------------------------|--------| //! | + | `overload!((a: A) + (b: B) -> C { /*...*/ );` | Add | //! | - | `overload!((a: A) - (b: B) -> C { /*...*/ );` | Sub | //! | * | `overload!((a: A) * (b: B) -> C { /*...*/ );` | Mul | //! | / | `overload!((a: A) / (b: B) -> C { /*...*/ );` | Div | //! | % | `overload!((a: A) % (b: B) -> C { /*...*/ );` | Rem | //! | & | `overload!((a: A) & (b: B) -> C { /*...*/ );` | BitAnd | //! | \| | overload!((a: A) | (b: B) -> C { /\*...*\/ ); | BitOr | //! | ^ | `overload!((a: A) ^ (b: B) -> C { /*...*/ );` | BitXor | //! | << | `overload!((a: A) << (b: B) -> C { /*...*/ );` | Shl | //! | >> | `overload!((a: A) >> (b: B) -> C { /*...*/ );` | Shr | //! //! # Assignment operators //! //! The general syntax to overload an assignment operator between types `` and `` is: //! ```ignore //! overload!((: &mut ) (: ) { /*body*/ }); //! ``` //! Inside the body you can use `` and `` freely to perform any computation and mutate `` as desired. //! //! | Operator | Example | Trait | //! |----------|------------------------------------------------------------------|--------------| //! | += | `overload!((a: &mut A) += (b: B) { /*...*/ );` | AddAssign | //! | -= | `overload!((a: &mut A) -= (b: B) { /*...*/ );` | SubAssign | //! | *= | `overload!((a: &mut A) *= (b: B) { /*...*/ );` | MulAssign | //! | /= | `overload!((a: &mut A) /= (b: B) { /*...*/ );` | DivAssign | //! | %= | `overload!((a: &mut A) %= (b: B) { /*...*/ );` | RemAssign | //! | &= | `overload!((a: &mut A) &= (b: B) { /*...*/ );` | BitAndAssign | //! | \|= | overload!((a: &mut A) |= (b: B) { /\*...*\/ ); | BitOrAssign | //! | ^= | `overload!((a: &mut A) ^= (b: B) { /*...*/ );` | BitXorAssign | //! | <<= | `overload!((a: &mut A) <<= (b: B) { /*...*/ );` | ShlAssign | //! | >>= | `overload!((a: &mut A) >>= (b: B) { /*...*/ );` | ShrAssign | //! //! # Unary operators //! //! The general syntax to overload a unary operator for type `` is: //! ```ignore //! overload!( (: ) -> { /*body*/ }); //! ``` //! Inside the body you can use `` freely to perform any computation. //! //! The last line of the body needs to be an expression (i.e. no `;` at the end of the line) of type ``. //! //! | Operator | Example | Trait | //! |----------|---------------------------------------------------------|-------| //! | - | `overload!(- (a: A) -> B { /*...*/ );` | Neg | //! | ! | `overload!(! (a: A) -> B { /*...*/ );` | Not | //! //! # Notes //! //! Remember that you can only overload operators between one or more types if at least one of the types is defined in the current crate. #[macro_use] mod unary; #[macro_use] mod assignment; #[macro_use] mod binary; /// Overloads an operator. See the [module level documentation](index.html) for more information. #[macro_export(local_inner_macros)] macro_rules! overload { // Unary (both owned and borrowed) ($op:tt ($i:ident : ? $t:ty) -> $out:ty $body:block) => ( _overload_unary!($op, $i, $t, $out, $body); _overload_unary!($op, $i, &$t, $out, $body); ); // Unary (either owned or borrowed) ($op:tt ($i:ident : $t:ty) -> $out:ty $body:block) => ( _overload_unary!($op, $i, $t, $out, $body); ); // Assignment (both owned and borrowed) (($li:ident : &mut $lt:ty) $op:tt ($ri:ident : ? $rt:ty) $body:block) => ( _overload_assignment!($op, $li, $lt, $ri, $rt, $body); _overload_assignment!($op, $li, $lt, $ri, &$rt, $body); ); // Assignment (either owned or borrowed) (($li:ident : &mut $lt:ty) $op:tt ($ri:ident : $rt:ty) $body:block) => ( _overload_assignment!($op, $li, $lt, $ri, $rt, $body); ); // Binary (both - both) (($li:ident : ? $lt:ty) $op:tt ($ri:ident : ? $rt:ty) -> $out:ty $body:block) => ( _overload_binary!($op, $li, $lt, $ri, $rt, $out, $body); _overload_binary!($op, $li, $lt, $ri, &$rt, $out, $body); _overload_binary!($op, $li, &$lt, $ri, $rt, $out, $body); _overload_binary!($op, $li, &$lt, $ri, &$rt, $out, $body); ); // Binary (both - either) (($li:ident : ? $lt:ty) $op:tt ($ri:ident : $rt:ty) -> $out:ty $body:block) => ( _overload_binary!($op, $li, $lt, $ri, $rt, $out, $body); _overload_binary!($op, $li, &$lt, $ri, $rt, $out, $body); ); // Binary (either - both) (($li:ident : $lt:ty) $op:tt ($ri:ident : ? $rt:ty) -> $out:ty $body:block) => ( _overload_binary!($op, $li, $lt, $ri, $rt, $out, $body); _overload_binary!($op, $li, $lt, $ri, &$rt, $out, $body); ); // Binary (either - either) (($li:ident : $lt:ty) $op:tt ($ri:ident : $rt:ty) -> $out:ty $body:block) => ( _overload_binary!($op, $li, $lt, $ri, $rt, $out, $body); ); } overload-0.1.1/src/unary.rs000064400000000000000000000011611352207244400140070ustar0000000000000000#[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! _overload_unary { (-, $($t:tt)+) => (_overload_unary_internal!(Neg, neg, $($t)+);); (!, $($t:tt)+) => (_overload_unary_internal!(Not, not, $($t)+);); } #[doc(hidden)] #[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 { type Output = $out; fn $op_fn(self) -> Self::Output { let $i = self; $body } } ); } overload-0.1.1/tests/assignment.rs000064400000000000000000000032501352340161600153730ustar0000000000000000extern crate overload; use overload::overload; use std::ops; #[derive(PartialEq, Debug)] struct A(i32); #[derive(PartialEq, Debug)] struct B(i32); overload!((a: &mut A) += (b: B) { a.0 += b.0; }); #[test] fn add_assign() { let mut a = A(3); a += B(4); assert_eq!(A(3 + 4), a); } overload!((a: &mut A) -= (b: B) { a.0 -= b.0; }); #[test] fn sub_assign() { let mut a = A(3); a -= B(4); assert_eq!(A(3 - 4), a); } overload!((a: &mut A) *= (b: B) { a.0 *= b.0; }); #[test] fn mul_assign() { let mut a = A(3); a *= B(4); assert_eq!(A(3 * 4), a); } overload!((a: &mut A) /= (b: B) { a.0 /= b.0; }); #[test] fn div_assign() { let mut a = A(6); a /= B(3); assert_eq!(A(6 / 3), a); } overload!((a: &mut A) %= (b: B) { a.0 %= b.0; }); #[test] fn rem_assign() { let mut a = A(6); a %= B(4); assert_eq!(A(6 % 4), a); } overload!((a: &mut A) &= (b: B) { a.0 &= b.0; }); #[test] fn bitand_assign() { let mut a = A(6); a &= B(4); assert_eq!(A(6 & 4), a); } overload!((a: &mut A) |= (b: B) { a.0 |= b.0; }); #[test] fn bitor_assign() { let mut a = A(6); a |= B(4); assert_eq!(A(6 | 4), a); } overload!((a: &mut A) ^= (b: B) { a.0 ^= b.0; }); #[test] fn bitxor_assign() { let mut a = A(6); a ^= B(4); assert_eq!(A(6 ^ 4), a); } overload!((a: &mut A) <<= (b: B) { a.0 <<= b.0; }); #[test] fn shl_assign() { let mut a = A(6); a <<= B(4); assert_eq!(A(6 << 4), a); } overload!((a: &mut A) >>= (b: B) { a.0 >>= b.0; }); #[test] fn shr_assign() { let mut a = A(6); a >>= B(4); assert_eq!(A(6 >> 4), a); } overload-0.1.1/tests/binary.rs000064400000000000000000000025571352340401200145110ustar0000000000000000extern crate overload; use overload::overload; use std::ops; #[derive(PartialEq, Debug)] struct A(i32); #[derive(PartialEq, Debug)] struct B(i32); #[derive(PartialEq, Debug)] struct C(i32); overload!((a: A) + (b: B) -> C { C(a.0 + b.0) }); #[test] fn add() { assert_eq!(A(3) + B(4), C(3 + 4)); } overload!((a: A) - (b: B) -> C { C(a.0 - b.0) }); #[test] fn sub() { assert_eq!(A(3) - B(4), C(3 - 4)); } overload!((a: A) * (b: B) -> C { C(a.0 * b.0) }); #[test] fn mul() { assert_eq!(A(3) * B(4), C(3 * 4)); } overload!((a: A) / (b: B) -> C { C(a.0 / b.0) }); #[test] fn div() { assert_eq!(A(6) / B(3), C(6 / 3)); } overload!((a: A) % (b: B) -> C { C(a.0 % b.0) }); #[test] fn rem() { assert_eq!(A(6) % B(4), C(6 % 4)); } overload!((a: A) & (b: B) -> C { C(a.0 & b.0) }); #[test] fn bitand() { assert_eq!(A(6) & B(4), C(6 & 4)); } overload!((a: A) | (b: B) -> C { C(a.0 | b.0) }); #[test] fn bitor() { assert_eq!(A(6) | B(4), C(6 | 4)); } overload!((a: A) ^ (b: B) -> C { C(a.0 ^ b.0) }); #[test] fn bitxor() { assert_eq!(A(6) ^ B(4), C(6 ^ 4)); } overload!((a: A) << (b: B) -> C { C(a.0 << b.0) }); #[test] fn shl() { assert_eq!(A(6) << B(4), C(6 << 4)); } overload!((a: A) >> (b: B) -> C { C(a.0 >> b.0) }); #[test] fn shr() { assert_eq!(A(6) >> B(4), C(6 >> 4)); } overload-0.1.1/tests/unary.rs000064400000000000000000000005361352355064500143740ustar0000000000000000extern crate overload; use overload::overload; use std::ops; #[derive(PartialEq, Debug)] struct A(i32); #[derive(PartialEq, Debug)] struct B(i32); overload!(- (a: A) -> B { B(-a.0) }); #[test] fn neg() { assert_eq!(-A(3), B(-3)); } overload!(! (a: A) -> B { B(!a.0) }); #[test] fn not() { assert_eq!(!A(3), B(!3)); } overload-0.1.1/.cargo_vcs_info.json0000644000000001120000000000000126770ustar00{ "git": { "sha1": "4a8ec5b58afc53ea04c3a0f9b2f7a2c42208678c" } }