gix_utils::btoi

Function to_signed

source
pub fn to_signed<I: MinNumTraits>(bytes: &[u8]) -> Result<I, ParseIntegerError>
Expand description

Converts a byte slice to an integer.

Like to_unsigned, but numbers may optionally start with a sign (- or +).

§Errors

Returns ParseIntegerError for any of the following conditions:

  • bytes has no digits
  • not all characters of bytes are 0-9, excluding an optional leading sign
  • the number overflows or underflows I

§Panics

Panics in the pathological case that there is no representation of 10 in I.

§Examples

assert_eq!(Ok(123), to_signed(b"123"));
assert_eq!(Ok(123), to_signed(b"+123"));
assert_eq!(Ok(-123), to_signed(b"-123"));

assert!(to_signed::<u8>(b"123456789").is_err()); // overflow
assert!(to_signed::<u8>(b"-1").is_err()); // underflow

assert!(to_signed::<i32>(b" 42").is_err()); // leading space