Expand description
Human-readable runtime formatting
This module includes various Runtime types meant for audio/video style formatting (HH:MM:SS).
The basic type is Runtime which formats strings to what you would expect from most audio/video players, e.g:
assert_eq!(Runtime::from(0), "0:00");
assert_eq!(Runtime::from(60), "1:00");
assert_eq!(Runtime::from(119), "1:59");
assert_eq!(Runtime::from(3599), "59:59");
assert_eq!(Runtime::from(3600), "1:00:00");
assert_eq!(Runtime::max(), "99:59:59");Here’s a diagram of:
- What the type’s formatting look like
- What their sub/super-set relationship is
Input
From input can be:
- Any unsigned integer
u8,usize, etc - Any signed integer
i8,isize, etc f32orf64std::time::Durationstd::time::Instant- Other
Runtimetypes
Integer inputs are presumed to be in seconds.
From other Runtime types
All Runtime types support lossless conversion with each other using From.
For example, the millisecond data will not be lost even if you
go from RuntimeMilli -> Runtime -> RuntimeMilli
// Millisecond data.
let milli = RuntimeMilli::from(1.555);
assert_eq!(milli, "00:00:01.555");
// Convert to `Runtime`.
let runtime = Runtime::from(milli);
assert_eq!(runtime, "0:01");
// Convert to `RuntimePad`.
let full = RuntimePad::from(runtime);
assert_eq!(full, "00:00:01");
// Convert back losslessly to [`RuntimeMilli`].
let milli2 = RuntimeMilli::from(full);
assert_eq!(milli2, "00:00:01.555");
assert_eq!(milli, milli2);
assert_eq!(milli2.inner(), 1.555);This is because the inner f32 stored is simply copied,
only the formatted string is different.
Errors
The max input is 359999 seconds, or: anything over 99:59:59.
A Runtime::unknown() (or the runtime variant’s version of it) will be returned if the input is:
- A negative integer
- Larger than
MAX_RUNTIME_F32 f32::NAN,f32::INFINITY,f32::NEG_INFINITY(or thef64versions)
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, e.g:Runtime::from(1.0) + Runtime::from(1.0) - Or with the inner number itself:
RuntimePad::from(1.0) + 1.0
let runtime = Runtime::from(1.0) + Runtime::from(1.0);
assert_eq!(runtime, Runtime::from(2.0));
assert_eq!(runtime, "0:02");
assert_eq!(runtime, 2.0);
let pad = RuntimePad::from(1.5) + 1.5;
assert_eq!(pad, RuntimePad::from(3.0));
assert_eq!(pad, "00:00:03");
assert_eq!(pad, 3.0);
// Floating point error!
let milli = RuntimeMilli::from(2.0) + 1.555;
assert_eq!(milli.inner(), 3.5549998);
// Use 1 more decimal to make sure
// weird rounding doesn't happen.
let milli = RuntimeMilli::from(2.0) + 1.5551;
assert_eq!(milli, RuntimeMilli::from(3.5551));
assert_eq!(milli, "00:00:03.555");
assert_eq!(milli, 3.5551);Copy
Copy is available for all Runtime types.
The actual strings used internally are not String’s,
but byte array buffer(s). See the specific type for more details.
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.0);
// Copy 'a', use 'b'.
let b = a;
assert_eq!(b, 100_000.0);
// We can still use 'a'
assert_eq!(a, 100_000.0);Structs
- Human readable “audio/video runtime” in
HH:MM:SSformat. RuntimePadbut with millisecondsRuntimebut always full length and padded with zeros- All
Runtimetypes combined
Constants
strreturned when usingRuntime::hourf32returned when callingRuntime::hourstrreturned when usingRuntimeMilli::hourstrreturned when usingRuntimePad::hour- The max length of
Runtime’s andRuntimePad’s string. - The max length of
RuntimeMilli’s string. - The max length of
RuntimePad’s string. - Input greater to
Runtimewill make it returnMAX_RUNTIME strfor the max timeRuntimeMillican handlestrfor the max timeRuntimePadcan handlestrreturned when usingRuntime::minutef32returned when callingRuntime::minutestrreturned when usingRuntimeMilli::minutestrreturned when usingRuntimePad::minutestrreturned when usingRuntime::secondf32returned when callingRuntime::secondstrreturned when usingRuntimeMilli::secondstrreturned when usingRuntimePad::secondstrreturned when usingRuntime::unknownstrreturned when usingRuntimeMilli::unknownstrreturned when usingRuntimePad::unknownstrreturned when usingRuntime::zerof32returned when callingRuntime::zerostrreturned when usingRuntimeMilli::zerostrreturned when usingRuntimePad::zero