Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Last active December 10, 2015 18:48
Show Gist options
  • Select an option

  • Save Kimundi/4476636 to your computer and use it in GitHub Desktop.

Select an option

Save Kimundi/4476636 to your computer and use it in GitHub Desktop.
Without macros:
pub fn from_bytes_common<T: Num Zero One Copy>(buf: &[u8], radix: uint,
fractional: bool, exponent: ExponentFormat,
special: bool, empty_zero: bool) -> Option<T> {
match exponent {
ExpDec if radix > DIGIT_E
=> fail fmt!("from_bytes_common: radix %? incompatible with decimal exponent format", radix),
ExpBin if radix > DIGIT_P
=> fail fmt!("from_bytes_common: radix %? incompatible with binary exponent format", radix),
_ if special && radix > DIGIT_I // lowest first digit in "inf" and "NaN"
=> fail fmt!("from_bytes_common: radix %? incompatible with special values", radix),
_ if radix < 2
=> fail fmt!("from_bytes_common: radix %? to low, must lie in the range [2, 36]", radix),
_ if radix > 36
=> fail fmt!("from_bytes_common: radix %? to high, must lie in the range [2, 36]", radix),
_ => () // ICE if missing
}
...
}
With macros:
pub fn from_bytes_common<T: Num Zero One Copy>(buf: &[u8], radix: uint,
fractional: bool, exponent: ExponentFormat,
special: bool, empty_zero: bool) -> Option<T> {
arg_fail!( in ~"from_bytes_common"
if matches!(exponent: ExpDec) && radix > DIGIT_E:
"radix %? incompatible with use of 'e' as decimal exponent", radix;
if matches!(exponent: ExpBin) && radix > DIGIT_P:
"radix %? incompatible with use of 'p' as binary exponent", radix;
if special && radix > DIGIT_I: //lowest first digit in "inf" and "NaN"
"radix %? incompatible with special values", radix;
if radix as int < 2:
"radix %? too low, must lie in the range [2, 36]", radix;
if radix as int > 36:
"radix %? too high, must lie in the range [2, 36]", radix;
);
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment