Struct SpanFieldwise

#[repr(transparent)]
pub struct SpanFieldwise(pub Span);

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>>(())

Fields

0: Span

Trait Implementations

impl Clone for SpanFieldwise

fn clone(&self) -> Self

impl Copy for SpanFieldwise

impl Debug for SpanFieldwise

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

impl Default for SpanFieldwise

fn default() -> Self

impl Eq for SpanFieldwise

impl From<Span> for SpanFieldwise

fn from(span: Span) -> SpanFieldwise

impl Hash for SpanFieldwise

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

impl Neg for SpanFieldwise

type Output = SpanFieldwise;
fn neg(self) -> SpanFieldwise

impl PartialEq for SpanFieldwise

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

impl PartialEq<Span> for SpanFieldwise

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

Auto Trait Implementations

impl Freeze for SpanFieldwise

impl RefUnwindSafe for SpanFieldwise

impl Send for SpanFieldwise

impl Sync for SpanFieldwise

impl Unpin for SpanFieldwise

impl UnsafeUnpin for SpanFieldwise

impl UnwindSafe for SpanFieldwise

Blanket Implementations

impl<T> Any for SpanFieldwise where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for SpanFieldwise where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for SpanFieldwise where T: ?Sized,

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

impl<T> CloneToUninit for SpanFieldwise where T: Clone,

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

impl<T> From<T> for SpanFieldwise

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for SpanFieldwise where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for SpanFieldwise where U: From<T>,

fn into(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<U> for SpanFieldwise where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for SpanFieldwise where U: TryFrom<T>,

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