pub struct Runtime(_, _);Expand description
Human readable “audio/video runtime” in H:M:S format.
Runtime::from input can be:
Integer inputs are presumed to be in seconds.
Errors
The max input is 359999 seconds, or: 99:59:59.
If the input is larger than MAX_RUNTIME, UNKNOWN_RUNTIME is returned.
Inlining
If the feature flag inline_runtime is enabled, ALL possible outputs of
Runtime are inlined as static bytes and any Runtime::from call will return
said static bytes.
Warning: This feature is disabled by default. While it increases speed, it also heavily increases build time and binary size.
Formatting rules
secondsalways has leading0.minutesonly has a leading zero ifhoursisn’t0.hoursnever has a leading0.
Cloning
Copy is available.
The actual string used internally is not a String,
but a 8 byte array buffer, literally: [u8; 8].
Since the max valid runtime is: 99:59:59 (8 characters, 359999 seconds), an 8 byte
buffer is used and enables this type to have Copy.
The documentation will still refer to the inner buffer as a String. Anything returned will also be a String.
let a = Runtime::from(100_000_u64);
// Copy 'a', use 'b'.
let b = a;
assert!(b == 100_000_u32);
// We can still use 'a'
assert!(a == 100_000_u32);Exceptions
- Inputting
f64::NAN,f64::INFINITY,f64::NEG_INFINITYor thef32variants returns errors
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:Runtime::from(1) + Runtime::from(1) - Or with the inner number itself:
Runtime::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!(Runtime::from(10_u32) + 10 == Runtime::from(20_u32));
assert!(Runtime::from(10_u32) - 10 == Runtime::from(0_u32));
assert!(Runtime::from(10_u32) / 10 == Runtime::from(1_u32));
assert!(Runtime::from(10_u32) * 10 == Runtime::from(100_u32));
assert!(Runtime::from(10_u32) % 10 == Runtime::from(0_u32));Overflow example (won’t panic, will return unknown):
let n = Runtime::from(u32::MAX) + u32::MAX;
assert!(n == Runtime::unknown());Examples
// Always round down.
assert!(Runtime::from(11.1111) == "0:11");
assert!(Runtime::from(11.9999) == "0:11");
assert!(Runtime::from(111.111) == "1:51");
assert!(Runtime::from(111.999) == "1:51");
assert!(Runtime::from(11111.1) == "3:05:11");
assert!(Runtime::from(11111.9) == "3:05:11");
assert!(Runtime::from(0.0) == "0:00");
assert!(Runtime::from(1.0) == "0:01");
assert!(Runtime::from(1.9) == "0:01");
assert!(Runtime::from(2.0) == "0:02");
assert!(Runtime::from(f32::NAN) == "?:??");
assert!(Runtime::from(f64::INFINITY) == "?:??");Implementations§
source§impl Runtime
impl Runtime
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; 8]
pub const fn to_buf(&self) -> [u8; 8]
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; 8]
pub const fn into_buf(self) -> [u8; 8]
Same as Self::to_buf but consumes self.
sourcepub const fn as_buf(&self) -> &[u8; 8]
pub const fn as_buf(&self) -> &[u8; 8]
Same as Self::to_buf but returns a borrowed array.
sourcepub const fn to_buf_parts(&self) -> ([u8; 8], usize)
pub const fn to_buf_parts(&self) -> ([u8; 8], usize)
Same as Self::to_buf but returns the length as well.
sourcepub const fn into_buf_parts(self) -> ([u8; 8], usize)
pub const fn into_buf_parts(self) -> ([u8; 8], usize)
Same as Self::into_buf but returns the length as well.
sourcepub const fn unknown() -> Self
pub const fn unknown() -> Self
assert!(Runtime::unknown() == 0);
assert!(Runtime::unknown() == "?:??");sourcepub const fn zero() -> Self
pub const fn zero() -> Self
assert!(Runtime::zero() == 0);
assert!(Runtime::zero() == "0:00");sourcepub const fn second() -> Self
pub const fn second() -> Self
assert!(Runtime::second() == 1);
assert!(Runtime::second() == "0:01");
assert!(Runtime::second() == Runtime::from(1.0));sourcepub const fn minute() -> Self
pub const fn minute() -> Self
assert!(Runtime::minute() == 60);
assert!(Runtime::minute() == "1:00");
assert!(Runtime::minute() == Runtime::from(60.0));Trait Implementations§
source§impl<'__de> BorrowDecode<'__de> for Runtime
impl<'__de> BorrowDecode<'__de> for Runtime
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 Runtime
impl<'de> Deserialize<'de> for Runtime
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 Ord for Runtime
impl Ord for Runtime
source§impl PartialEq<&Runtime> for str
impl PartialEq<&Runtime> for str
source§impl PartialEq<&Runtime> for u32
impl PartialEq<&Runtime> for u32
source§impl PartialEq<&str> for Runtime
impl PartialEq<&str> for Runtime
source§impl PartialEq<Runtime> for Runtime
impl PartialEq<Runtime> for Runtime
source§impl PartialEq<Runtime> for str
impl PartialEq<Runtime> for str
source§impl PartialEq<Runtime> for u32
impl PartialEq<Runtime> for u32
source§impl PartialOrd<&Runtime> for str
impl PartialOrd<&Runtime> 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<&Runtime> for u32
impl PartialOrd<&Runtime> for u32
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 Runtime
impl PartialOrd<&str> for Runtime
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<Runtime> for Runtime
impl PartialOrd<Runtime> for Runtime
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<Runtime> for str
impl PartialOrd<Runtime> 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<Runtime> for u32
impl PartialOrd<Runtime> for u32
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 Runtime
impl PartialOrd<str> for Runtime
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<u32> for &Runtime
impl PartialOrd<u32> for &Runtime
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<u32> for Runtime
impl PartialOrd<u32> for Runtime
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 moreimpl Copy for Runtime
impl Eq for Runtime
impl StructuralEq for Runtime
impl StructuralPartialEq for Runtime
Auto Trait Implementations§
impl RefUnwindSafe for Runtime
impl Send for Runtime
impl Sync for Runtime
impl Unpin for Runtime
impl UnwindSafe for Runtime
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