[types] Implement Add and Mul on Int
This commit is contained in:
		
							parent
							
								
									1dfc6823f0
								
							
						
					
					
						commit
						0ed4aa3ae5
					
				
					 3 changed files with 47 additions and 3 deletions
				
			
		|  | @ -9,4 +9,5 @@ pub use object::Obj; | |||
| pub use pair::Pair; | ||||
| pub use sym::Sym; | ||||
| 
 | ||||
| pub use self::number::Number; | ||||
| pub use self::number::Int; | ||||
|  |  | |||
|  | @ -4,11 +4,33 @@ | |||
| 
 | ||||
| use std::any::Any; | ||||
| use std::fmt; | ||||
| use std::ops::{Add, Mul}; | ||||
| use number::Number; | ||||
| use object::{Obj, Object}; | ||||
| 
 | ||||
| #[derive(Debug, Eq, PartialEq)] | ||||
| pub struct Int(i64); | ||||
| #[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||||
| pub struct Int(pub i64); | ||||
| 
 | ||||
| impl Add for Int { | ||||
|     type Output = Int; | ||||
|     fn add(self, rhs: Self) -> Self::Output { | ||||
|         Int(self.0 + rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<'a> Add<Int> for &'a Int { | ||||
|     type Output = Int; | ||||
|     fn add(self, rhs: Int) -> Self::Output { | ||||
|         Int(self.0 + rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<'a, 'b> Add<&'a Int> for &'b Int { | ||||
|     type Output = Int; | ||||
|     fn add(self, rhs: &Int) -> Self::Output { | ||||
|         Int(self.0 + rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl fmt::Display for Int { | ||||
|     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||
|  | @ -25,6 +47,27 @@ impl Number for Int { | |||
|     fn as_int(&self) -> Option<&Int> { Some(self) } | ||||
| } | ||||
| 
 | ||||
| impl Mul for Int { | ||||
|     type Output = Int; | ||||
|     fn mul(self, rhs: Self) -> Self::Output { | ||||
|         Int(self.0 * rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<'a> Mul<Int> for &'a Int { | ||||
|     type Output = Int; | ||||
|     fn mul(self, rhs: Int) -> Self::Output { | ||||
|         Int(self.0 * rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<'a, 'b> Mul<&'a Int> for &'b Int { | ||||
|     type Output = Int; | ||||
|     fn mul(self, rhs: &Int) -> Self::Output { | ||||
|         Int(self.0 * rhs.0) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl PartialEq<Obj> for Int { | ||||
|     fn eq<'a>(&self, rhs: &'a Obj) -> bool { | ||||
|         match rhs.obj().and_then(Object::as_num) { | ||||
|  |  | |||
|  | @ -18,7 +18,7 @@ use object::Object; | |||
| pub use self::integer::Int; | ||||
| 
 | ||||
| pub trait Number:  | ||||
|     Object | ||||
|     Object 
 | ||||
| { | ||||
|     /// Cast this Number to an Int if possible.
 | ||||
|     fn as_int(&self) -> Option<&Int> { None } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue