Struct SpanFieldwise

struct SpanFieldwise(2634)

A wrapper for Span that implements the Hash, Eq and PartialEq traits.

A SpanFieldwise is meant to make it easy to compare two spans in a "dumb" way based purely on its unit values, while still providing a speed bump to avoid accidentally doing this comparison on Span directly. This is distinct from something like Span::compare that performs a comparison on the actual elapsed time of two spans.

It is generally discouraged to use SpanFieldwise since spans that represent an equivalent elapsed amount of time may compare unequal. However, in some cases, it is useful to be able to assert precise field values. For example, Jiff itself makes heavy use of fieldwise comparisons for tests.

Construction

While callers may use SpanFieldwise(span) (where span has type Span) to construct a value of this type, callers may find Span::fieldwise more convenient. Namely, Span::fieldwise may avoid the need to explicitly import SpanFieldwise.

Trait implementations

In addition to implementing the Hash, Eq and PartialEq traits, this type also provides PartialEq impls for comparing a Span with a SpanFieldwise. This simplifies comparisons somewhat while still requiring that at least one of the values has an explicit fieldwise comparison type.

Safety

This type is guaranteed to have the same layout in memory as Span.

Example: the difference between SpanFieldwise and Span::compare

In short, SpanFieldwise considers 2 hours and 120 minutes to be distinct values, but Span::compare considers them to be equivalent:

use std::cmp::Ordering;
use jiff::ToSpan;

assert_ne!(120.minutes().fieldwise(), 2.hours().fieldwise());
assert_eq!(120.minutes().compare(2.hours())?, Ordering::Equal);

// These comparisons are allowed between a `Span` and a `SpanFieldwise`.
// Namely, as long as one value is "fieldwise," then the comparison is OK.
assert_ne!(120.minutes().fieldwise(), 2.hours());
assert_ne!(120.minutes(), 2.hours().fieldwise());

# Ok::<(), Box<dyn std::error::Error>>(())

Implementations

impl Clone for SpanFieldwise

fn clone(self: &Self) -> SpanFieldwise

impl Copy for SpanFieldwise

impl Debug for SpanFieldwise

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Default for SpanFieldwise

fn default() -> SpanFieldwise

impl Eq for SpanFieldwise

impl Freeze for SpanFieldwise

impl From for SpanFieldwise

fn from(span: Span) -> SpanFieldwise

impl Hash for SpanFieldwise

fn hash<H: core::hash::Hasher>(self: &Self, state: &mut H)

impl Neg for SpanFieldwise

fn neg(self: Self) -> SpanFieldwise

impl PartialEq for SpanFieldwise

fn eq(self: &Self, rhs: &Span) -> bool

impl PartialEq for SpanFieldwise

fn eq(self: &Self, rhs: &SpanFieldwise) -> bool

impl RefUnwindSafe for SpanFieldwise

impl Send for SpanFieldwise

impl Sync for SpanFieldwise

impl Unpin for SpanFieldwise

impl UnsafeUnpin for SpanFieldwise

impl UnwindSafe for SpanFieldwise

impl<T> Any for SpanFieldwise

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for SpanFieldwise

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for SpanFieldwise

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for SpanFieldwise

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for SpanFieldwise

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for SpanFieldwise

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for SpanFieldwise

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for SpanFieldwise

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for SpanFieldwise

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>