-
-
Save greister/6d6fa09752d6cf57f09c8ed491e1b3de to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://rosettacode.org/wiki/Define_a_primitive_data_type | |
/// Implements a custom type named CustomInt. | |
/// One problem that I'm not sure how to solve is bounds checking on variable assignments. | |
/// This type only implements a subset of all traits within std::ops. | |
use std::ops; | |
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)] | |
pub struct CustomInt { | |
value: u8, | |
} | |
// custom trait to specify bounds | |
trait Bounded { | |
fn in_bounds(&self); | |
} | |
impl Bounded for CustomInt { | |
fn in_bounds(&self) { | |
if self.value < 1 || self.value > 10 { | |
panic(self.value); | |
} | |
#[cold] #[inline(never)] | |
fn panic(val: u8) -> ! { | |
panic!("CustomInt is out of bounds! {} was value", val); | |
} | |
} | |
} | |
impl ops::Add for CustomInt { | |
type Output = CustomInt; | |
fn add(self, rhs: CustomInt) -> CustomInt { | |
let rval = CustomInt { value: (self.value + rhs.value) }; | |
rval.in_bounds(); | |
rval | |
} | |
} | |
impl ops::Sub for CustomInt { | |
type Output = CustomInt; | |
fn sub(self, rhs: CustomInt) -> CustomInt { | |
let rval = CustomInt { value: (self.value - rhs.value) }; | |
rval.in_bounds(); | |
rval | |
} | |
} | |
impl ops::Mul for CustomInt { | |
type Output = CustomInt; | |
fn mul(self, rhs: CustomInt) -> CustomInt { | |
let rval = CustomInt { value: (self.value * rhs.value) }; | |
rval.in_bounds(); | |
rval | |
} | |
} | |
impl ops::Div for CustomInt { | |
type Output = CustomInt; | |
fn div(self, rhs: CustomInt) -> CustomInt { | |
let rval = CustomInt { value: (self.value / rhs.value) }; | |
rval.in_bounds(); | |
rval | |
} | |
} | |
impl ops::BitAnd for CustomInt { | |
type Output = CustomInt; | |
fn bitand(self, rhs: CustomInt) -> CustomInt { | |
let rval = CustomInt { value: (self.value & rhs.value) }; | |
rval.in_bounds(); | |
rval | |
} | |
} | |
impl ops::BitOr for CustomInt { | |
type Output = CustomInt; | |
fn bitor(self, rhs: CustomInt) -> CustomInt { | |
let rval = CustomInt { value: (self.value | rhs.value) }; | |
rval.in_bounds(); | |
rval | |
} | |
} | |
impl ops::BitXor for CustomInt { | |
type Output = CustomInt; | |
fn bitxor(self, rhs: CustomInt) -> CustomInt { | |
let rval = CustomInt { value: (self.value ^ rhs.value) }; | |
rval.in_bounds(); | |
rval | |
} | |
} | |
fn main() { | |
let cint_2: CustomInt = CustomInt { value: 2 }; | |
let cint_3: CustomInt = CustomInt { value: 3 }; | |
let cint_4: CustomInt = CustomInt { value: 4 }; | |
cint_2 + cint_4; // CustomInt { value: 6 } | |
cint_4 - cint_2; // CustomInt { value: 2 } | |
cint_4 * cint_2; // CustomInt { value: 8 } | |
cint_4 / cint_2; // CustomInt { value: 2 } | |
cint_3 & cint_2; // CustomInt { value: 2 } | |
cint_3 | cint_2; // CustomInt { value: 3 } | |
cint_3 ^ cint_2; // CustomInt { value: 1 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
impl Bounded for CustomInt {
fn in_bounds(&self) {
if self.value < 1 || self.value > 10 {
panic!("CustomInt is out of bounds! {} was value", self.value);
}
}
}