/// An integer that can be represented by either an `i64` or a `u64`. #[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)] pubstruct Integer {
value: i128,
}
impl Integer { /// Returns the value as an `i64` if it can be represented by that type. pubfn as_signed(self) -> Option<i64> { ifself.value >= i128::from(i64::min_value()) && self.value <= i128::from(i64::max_value())
{
Some(self.value as i64)
} else {
None
}
}
/// Returns the value as a `u64` if it can be represented by that type. pubfn as_unsigned(self) -> Option<u64> { ifself.value >= 0 && self.value <= i128::from(u64::max_value()) {
Some(self.value as u64)
} else {
None
}
}
pub(crate) fn from_str(s: &str) -> Result<Self, ParseIntError> { if s.starts_with("0x") { // NetBSD dialect adds the `0x` numeric objects, // which are always unsigned. // See the `PROP_NUMBER(3)` man page let s = s.trim_start_matches("0x");
u64::from_str_radix(s, 16).map(Into::into)
} else { // Match Apple's implementation in CFPropertyList.h - always try to parse as an i64 first. // TODO: Use IntErrorKind once stable and retry parsing on overflow only.
Ok(match s.parse::<i64>() {
Ok(v) => v.into(),
Err(_) => s.parse::<u64>()?.into(),
})
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.