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§
Sourcefn parse_wrapping(input: &str) -> Result<Self, Error>
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
i64for parsing. - If the target type is unsigned, it uses
u64for 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.hkxto0xfffff300 (-3328)in XML. by wrapping the input hexadecimal number that is greater thani16::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.