pub struct Unsigned(_, _);Expand description
Human readable unsigned integer.
Creation
For u8, u16, u32, u64, usize or any NonZeroU8 variant:
- Use
Unsigned::from
f32 or f64 inputs will work, but:
- Signed floats will turn into
0 - Fractional parts will be ignored
- Under/overflows will return
Unsigned::unknown - Special floats like
f64::NANwill returnUnsigned::unknown
For i8 and other signed integers:
- You need to use
Unsigned::try_from Unsigned::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 = Unsigned::from(100_000_u64);
// Copy 'a', use 'b'.
let b = a;
assert!(b == 100_000_u64);
// We can still use 'a'
assert!(a == 100_000_u64);Float Errors
- Inputting
f64::NANreturnsUnsigned::unknown - Inputting
f64::INFINITYreturnsUnsigned::unknown - Inputting
f64::NEG_INFINITYreturnsUnsigned::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:Unsigned::from(1) + Unsigned::from(1) - Or with the inner number itself:
Unsigned::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!(Unsigned::from(10_u64) + 10 == Unsigned::from(20_u64));
assert!(Unsigned::from(10_u64) - 10 == Unsigned::from(0_u64));
assert!(Unsigned::from(10_u64) / 10 == Unsigned::from(1_u64));
assert!(Unsigned::from(10_u64) * 10 == Unsigned::from(100_u64));
assert!(Unsigned::from(10_u64) % 10 == Unsigned::from(0_u64));Overflow example:
let n = Unsigned::from(u64::MAX) + u64::MAX;Examples
// From unsigned integers.
assert!(Unsigned::from(100_u8) == "100");
assert!(Unsigned::from(10_000_u16) == "10,000");
assert!(Unsigned::from(100_000_u32) == "100,000");
assert!(Unsigned::from(1_000_000_u64) == "1,000,000");
// From floats.
assert!(Unsigned::from(-1.0) == "0");
assert!(Unsigned::from(1_000.123) == "1,000");
assert!(Unsigned::from(100_000.123) == "100,000");
assert!(Unsigned::from(100_000.123) == "100,000");
assert!(Unsigned::from(f32::NAN) == "???");
// From signed integers.
assert!(Unsigned::try_from(100_i8) == Ok(Unsigned::from(100_u8)));
assert!(Unsigned::try_from(-100_i8) == Err(Unsigned::unknown()));
assert!(Unsigned::try_from(1_000_000_i64) == Ok(Unsigned::from(1_000_000_u32)));
assert!(Unsigned::try_from(-1_000_000_i64) == Err(Unsigned::unknown()));Implementations§
source§impl Unsigned
impl Unsigned
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 Unsigned
impl<'__de> BorrowDecode<'__de> for Unsigned
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 Unsigned
impl<'de> Deserialize<'de> for Unsigned
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<&NonZeroU16> for Unsigned
impl From<&NonZeroU16> for Unsigned
source§fn from(uint: &NonZeroU16) -> Self
fn from(uint: &NonZeroU16) -> Self
source§impl From<&NonZeroU32> for Unsigned
impl From<&NonZeroU32> for Unsigned
source§fn from(uint: &NonZeroU32) -> Self
fn from(uint: &NonZeroU32) -> Self
source§impl From<&NonZeroU64> for Unsigned
impl From<&NonZeroU64> for Unsigned
source§fn from(uint: &NonZeroU64) -> Self
fn from(uint: &NonZeroU64) -> Self
source§impl From<&NonZeroUsize> for Unsigned
impl From<&NonZeroUsize> for Unsigned
source§fn from(uint: &NonZeroUsize) -> Self
fn from(uint: &NonZeroUsize) -> Self
source§impl From<NonZeroU16> for Unsigned
impl From<NonZeroU16> for Unsigned
source§fn from(uint: NonZeroU16) -> Self
fn from(uint: NonZeroU16) -> Self
source§impl From<NonZeroU32> for Unsigned
impl From<NonZeroU32> for Unsigned
source§fn from(uint: NonZeroU32) -> Self
fn from(uint: NonZeroU32) -> Self
source§impl From<NonZeroU64> for Unsigned
impl From<NonZeroU64> for Unsigned
source§fn from(uint: NonZeroU64) -> Self
fn from(uint: NonZeroU64) -> Self
source§impl From<NonZeroUsize> for Unsigned
impl From<NonZeroUsize> for Unsigned
source§fn from(uint: NonZeroUsize) -> Self
fn from(uint: NonZeroUsize) -> Self
source§impl From<f32> for Unsigned
impl From<f32> for Unsigned
This will silently return Self::unknown
if the input float is NAN, INFINITY, etc.
Self::zero will be returned on negative floats.
source§impl From<f64> for Unsigned
impl From<f64> for Unsigned
This will silently return Self::unknown
if the input float is NAN, INFINITY, etc.
Self::zero will be returned on negative floats.
source§impl Ord for Unsigned
impl Ord for Unsigned
source§impl PartialEq<&Unsigned> for str
impl PartialEq<&Unsigned> for str
source§impl PartialEq<&Unsigned> for u64
impl PartialEq<&Unsigned> for u64
source§impl PartialEq<&str> for Unsigned
impl PartialEq<&str> for Unsigned
source§impl PartialEq<Unsigned> for Unsigned
impl PartialEq<Unsigned> for Unsigned
source§impl PartialEq<Unsigned> for str
impl PartialEq<Unsigned> for str
source§impl PartialEq<Unsigned> for u64
impl PartialEq<Unsigned> for u64
source§impl PartialOrd<&Unsigned> for str
impl PartialOrd<&Unsigned> 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<&Unsigned> for u64
impl PartialOrd<&Unsigned> for u64
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 Unsigned
impl PartialOrd<&str> for Unsigned
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<Unsigned> for Unsigned
impl PartialOrd<Unsigned> for Unsigned
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<Unsigned> for str
impl PartialOrd<Unsigned> 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<Unsigned> for u64
impl PartialOrd<Unsigned> for u64
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 Unsigned
impl PartialOrd<str> for Unsigned
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<u64> for &Unsigned
impl PartialOrd<u64> for &Unsigned
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<u64> for Unsigned
impl PartialOrd<u64> for Unsigned
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<&NonZeroI16> for Unsigned
impl TryFrom<&NonZeroI16> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroI32> for Unsigned
impl TryFrom<&NonZeroI32> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroI64> for Unsigned
impl TryFrom<&NonZeroI64> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroI8> for Unsigned
impl TryFrom<&NonZeroI8> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<&NonZeroIsize> for Unsigned
impl TryFrom<&NonZeroIsize> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroI16> for Unsigned
impl TryFrom<NonZeroI16> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroI32> for Unsigned
impl TryFrom<NonZeroI32> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroI64> for Unsigned
impl TryFrom<NonZeroI64> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroI8> for Unsigned
impl TryFrom<NonZeroI8> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<NonZeroIsize> for Unsigned
impl TryFrom<NonZeroIsize> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<i16> for Unsigned
impl TryFrom<i16> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<i32> for Unsigned
impl TryFrom<i32> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<i64> for Unsigned
impl TryFrom<i64> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<i8> for Unsigned
impl TryFrom<i8> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
source§impl TryFrom<isize> for Unsigned
impl TryFrom<isize> for Unsigned
This will return Self::unknown wrapped
in Result::Err if the conversion fails.
impl Copy for Unsigned
impl Eq for Unsigned
impl StructuralEq for Unsigned
impl StructuralPartialEq for Unsigned
Auto Trait Implementations§
impl RefUnwindSafe for Unsigned
impl Send for Unsigned
impl Sync for Unsigned
impl Unpin for Unsigned
impl UnwindSafe for Unsigned
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