[board] Implement some TryInto traits for Square for u8, u16, u32, and u64
This commit is contained in:
		
							parent
							
								
									3a15fca10a
								
							
						
					
					
						commit
						f27d22d57f
					
				
					 1 changed files with 17 additions and 0 deletions
				
			
		| 
						 | 
					@ -237,6 +237,23 @@ impl FromStr for Square {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					macro_rules! try_from_integer {
 | 
				
			||||||
 | 
					    ($int_type:ident) => {
 | 
				
			||||||
 | 
					        impl TryFrom<$int_type> for Square {
 | 
				
			||||||
 | 
					            type Error = SquareOutOfBoundsError;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            fn try_from(value: $int_type) -> Result<Self, Self::Error> {
 | 
				
			||||||
 | 
					                Square::try_index(value as usize).ok_or(SquareOutOfBoundsError)
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					try_from_integer!(u8);
 | 
				
			||||||
 | 
					try_from_integer!(u16);
 | 
				
			||||||
 | 
					try_from_integer!(u32);
 | 
				
			||||||
 | 
					try_from_integer!(u64);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl fmt::Display for Square {
 | 
					impl fmt::Display for Square {
 | 
				
			||||||
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 | 
					    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 | 
				
			||||||
        write!(
 | 
					        write!(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue