ParseNumber

Trait ParseNumber 

Source
pub trait ParseNumber: Sized {
    // Required methods
    fn parse(input: &str) -> Result<Self, Error>;
    fn parse_wrapping(input: &str) -> Result<Self, Error>;
}
Expand description

A trait for parsing numeric values from strings.

Fast string parsing of radix(0b, 0o, 0x) with separator(_) using lexical crate.

Required Methods§

Source

fn parse(input: &str) -> Result<Self, Error>

Parses a string into the target numeric type.

§Errors

Returns lexical::Error if parsing fails.

§Examples
use havok_types::parse_int::ParseNumber;
let value: i64 = <i64 as ParseNumber>::parse("123").unwrap();
assert_eq!(value, 123);
Source

fn parse_wrapping(input: &str) -> Result<Self, Error>

Parses a string as i64 or u64 and casts it to the target type using as.

§Behavior
  • If the target type is signed, it uses i64 for parsing.
  • If the target type is unsigned, it uses u64 for parsing.
  • The result is cast to the target type with as, allowing for wrapping behavior.
§Errors

Returns lexical::Error if parsing fails.

§Examples
use havok_types::parse_int::ParseNumber;
let value: i8 = <i8 as ParseNumber>::parse_wrapping("300").unwrap();
assert_eq!(value, 44);  // Wrapping overflow
§Why do we need this?

hkFlag is wrapped if a value greater than i16 comes in, and is stringified to hexadecimal as u32 at XML time. This method exists to reproduce that behavior.

  • Example: hkxcmd is setting the RoleFlags of cow/behaviors/quadrupedbehavior.hkx to 0xfffff300 (-3328) in XML. by wrapping the input hexadecimal number that is greater than i16::MAX
use havok_types::parse_int::ParseNumber;
const OVERFLOW_U32_STR: &str = "0xFFFFF300";
assert_eq!(
    <i16 as ParseNumber>::parse_wrapping(OVERFLOW_U32_STR).unwrap(),
    -3328
);
assert_eq!(format!("{:#X}", (-3328_i16) as u32), OVERFLOW_U32_STR);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ParseNumber for i8

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for i16

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for i32

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for i64

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for i128

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for isize

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for u8

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for u16

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for u32

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for u64

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for u128

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Source§

impl ParseNumber for usize

Source§

fn parse(input: &str) -> Result<Self, Error>

Source§

fn parse_wrapping(input: &str) -> Result<Self, Error>

Implementors§