pub struct Int(_, _);Expand description
Human readable signed integer.
Creation
For i8, i16, i32, i64, isize or any NonZeroI8 variant:
- Use
Int::from
f32 or f64 inputs will work, but:
- Fractional parts will be ignored
- Under/overflows will return
Int::unknown - Special floats like
f64::NANwill returnInt::unknown
For u8 and other unsigned integers:
- You can use
Int::fromfor anything underu32 - You need to use
Int::try_fromfor anything aboveu32 Int::unknownwill be returned on error
Cloning
Copy is available.
The actual string used internally is not a String,
but a 26 byte array buffer, literally: [u8; 26].
The documentation will still refer to the inner buffer as a String. Anything returned will also be a String.
let a = Int::from(100_000);
// Copy 'a', use 'b'.
let b = a;
assert!(b == 100_000);
// We can still use 'a'
assert!(a == 100_000);Float Errors
- Inputting
f64::NANreturnsInt::unknown - Inputting
f64::INFINITYreturnsInt::unknown - Inputting
f64::NEG_INFINITYreturnsInt::unknown
To disable checks for these, (you are sure you don’t have NaN’s), enable the ignore_nan_inf feature flag.
Math
These operators are overloaded. They will always output a new Self:
Add +Sub -Div /Mul *Rem %
They can either be:
- Combined with another
Self:Int::from(1) + Int::from(1) - Or with the inner number itself:
Int::from(1) + 1
They also have the same panic!() behavior on overflow as the normal ones, because internally,
it is just calling .inner() $OPERATOR $NUMBER.
assert!(Int::from(10) + 10 == Int::from(20));
assert!(Int::from(10) - 10 == Int::from(0));
assert!(Int::from(10) / 10 == Int::from(1));
assert!(Int::from(10) * 10 == Int::from(100));
assert!(Int::from(10) % 10 == Int::from(0));Overflow example:
let n = Int::from(i64::MAX) + i64::MAX;Examples
// From u32.
assert!(Int::from(1_000_u32) == "1,000");
assert!(Int::from(100_000_u32) == "100,000");
assert!(Int::from(1_000_000_u32) == "1,000,000");
// From signed integers.
assert!(Int::from(-1_000) == "-1,000");
assert!(Int::from(-100_000) == "-100,000");
assert!(Int::from(-100_000) == "-100,000");
// From floats.
assert!(Int::from(-1.0) == "-1");
assert!(Int::from(1_000.123) == "1,000");
assert!(Int::from(100_000.123) == "100,000");
assert!(Int::from(100_000.123) == "100,000");Implementations§
source§impl Int
impl Int
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the valid byte slice of the inner String
These bytes can always safely be used for std::str::from_utf8_unchecked.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
sourcepub fn head_dot(&self, len: usize) -> String
pub fn head_dot(&self, len: usize) -> String
Same as Self::head but returns a String ending with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_dot(4) == "2021...");sourcepub fn tail_dot(&self, len: usize) -> String
pub fn tail_dot(&self, len: usize) -> String
Same as Self::tail but returns a String starting with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.tail_dot(5) == "...12-11");sourcepub fn head_tail(&self, head: usize, tail: usize) -> String
pub fn head_tail(&self, head: usize, tail: usize) -> String
Return the first head bytes and last tail
bytes of this string separated with ....
Since all readable types happen to only contain
ASCII characters, all char’s are equal to 1 byte.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_tail(3, 2) == "202...11");
assert!(date.head_tail(3, 3) == "202...-11");
assert!(date.head_tail(3, 5) == "202...12-11");sourcepub const fn to_buf(&self) -> [u8; 26]
pub const fn to_buf(&self) -> [u8; 26]
Return the full inner buffer that represents the String.
These are guaranteed to be valid UTF-8 bytes.
Not all bytes are necessarily used, however.
To find the valid portion of the string, use Self::len.
let u = Unsigned::from(123_u8);
let buffer = u.to_buf();
let valid_bytes = &buffer[0..u.len()];
// SAFETY: These bytes are always be valid UTF-8.
unsafe {
let specified = std::str::from_utf8_unchecked(&valid_bytes);
let all_bytes = std::str::from_utf8_unchecked(&buffer);
// Bunch of trailing `\0\0\0`'s at the end.
assert!(all_bytes != "123");
assert!(specified == "123");
}sourcepub const fn into_buf(self) -> [u8; 26]
pub const fn into_buf(self) -> [u8; 26]
Same as Self::to_buf but consumes self.
sourcepub const fn as_buf(&self) -> &[u8; 26]
pub const fn as_buf(&self) -> &[u8; 26]
Same as Self::to_buf but returns a borrowed array.
sourcepub const fn to_buf_parts(&self) -> ([u8; 26], usize)
pub const fn to_buf_parts(&self) -> ([u8; 26], usize)
Same as Self::to_buf but returns the length as well.
sourcepub const fn into_buf_parts(self) -> ([u8; 26], usize)
pub const fn into_buf_parts(self) -> ([u8; 26], usize)
Same as Self::into_buf but returns the length as well.
Trait Implementations§
source§impl<'__de> BorrowDecode<'__de> for Int
impl<'__de> BorrowDecode<'__de> for Int
source§fn borrow_decode<__D: BorrowDecoder<'__de>>(
decoder: &mut __D
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D ) -> Result<Self, DecodeError>
source§impl<'de> Deserialize<'de> for Int
impl<'de> Deserialize<'de> for Int
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl From<&NonZeroI16> for Int
impl From<&NonZeroI16> for Int
source§fn from(int: &NonZeroI16) -> Self
fn from(int: &NonZeroI16) -> Self
source§impl From<&NonZeroI32> for Int
impl From<&NonZeroI32> for Int
source§fn from(int: &NonZeroI32) -> Self
fn from(int: &NonZeroI32) -> Self
source§impl From<&NonZeroI64> for Int
impl From<&NonZeroI64> for Int
source§fn from(int: &NonZeroI64) -> Self
fn from(int: &NonZeroI64) -> Self
source§impl From<&NonZeroIsize> for Int
impl From<&NonZeroIsize> for Int
source§fn from(int: &NonZeroIsize) -> Self
fn from(int: &NonZeroIsize) -> Self
source§impl From<NonZeroI16> for Int
impl From<NonZeroI16> for Int
source§fn from(int: NonZeroI16) -> Self
fn from(int: NonZeroI16) -> Self
source§impl From<NonZeroI32> for Int
impl From<NonZeroI32> for Int
source§fn from(int: NonZeroI32) -> Self
fn from(int: NonZeroI32) -> Self
source§impl From<NonZeroI64> for Int
impl From<NonZeroI64> for Int
source§fn from(int: NonZeroI64) -> Self
fn from(int: NonZeroI64) -> Self
source§impl From<NonZeroIsize> for Int
impl From<NonZeroIsize> for Int
source§fn from(int: NonZeroIsize) -> Self
fn from(int: NonZeroIsize) -> Self
source§impl From<f32> for Int
impl From<f32> for Int
This will silently return Self::unknown
if the input float is NAN, INFINITY, or under/overflows.
source§impl From<f64> for Int
impl From<f64> for Int
This will silently return Self::unknown
if the input float is NAN, INFINITY, or under/overflows.
source§impl Ord for Int
impl Ord for Int
source§impl PartialEq<&Int> for i64
impl PartialEq<&Int> for i64
source§impl PartialEq<&Int> for str
impl PartialEq<&Int> for str
source§impl PartialEq<&str> for Int
impl PartialEq<&str> for Int
source§impl PartialOrd<&Int> for i64
impl PartialOrd<&Int> for i64
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<&Int> for str
impl PartialOrd<&Int> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<&str> for Int
impl PartialOrd<&str> for Int
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Int> for Int
impl PartialOrd<Int> for Int
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Int> for i64
impl PartialOrd<Int> for i64
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Int> for str
impl PartialOrd<Int> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<i64> for &Int
impl PartialOrd<i64> for &Int
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<i64> for Int
impl PartialOrd<i64> for Int
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<str> for Int
impl PartialOrd<str> for Int
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl TryFrom<&NonZeroU16> for Int
impl TryFrom<&NonZeroU16> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroU32> for Int
impl TryFrom<&NonZeroU32> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroU64> for Int
impl TryFrom<&NonZeroU64> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroU8> for Int
impl TryFrom<&NonZeroU8> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroUsize> for Int
impl TryFrom<&NonZeroUsize> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroU16> for Int
impl TryFrom<NonZeroU16> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroU32> for Int
impl TryFrom<NonZeroU32> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroU64> for Int
impl TryFrom<NonZeroU64> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroU8> for Int
impl TryFrom<NonZeroU8> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroUsize> for Int
impl TryFrom<NonZeroUsize> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<u64> for Int
impl TryFrom<u64> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<usize> for Int
impl TryFrom<usize> for Int
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
impl Copy for Int
impl Eq for Int
impl StructuralEq for Int
impl StructuralPartialEq for Int
Auto Trait Implementations§
impl RefUnwindSafe for Int
impl Send for Int
impl Sync for Int
impl Unpin for Int
impl UnwindSafe for Int
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere T: Display,
source§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more