Struct serial_num::Serial

source ·
pub struct Serial(_);
Expand description

Two-byte serial number with wraparound.

A serial number is an identifier assigned incrementally to an item. In many cases, you can use a u32 or u64 and call it a day, without having to worry about overflow. The niche benefit of this type is that it only uses the space of a u16, with the problem of overflow solved by wraparound.

This is an “opaque” type, similar to Instants. Serial numbers get their significance when being compare to one another, but there is no method to get the “inner counter”. Another similarity is that there is no “maximum” serial number, since every serial number has a successor.

The window used for comparing two serial numbers is half of our number space, (u16::MAX-1)/2 = 32767. If two serial numbers are within that window, we simply compare the numbers as you normally would. If we compare numbers that do not fit into that window, like 5 and 65000, the comparison is flipped, and we say 65000 < 5. This is based on the assumption that we got to 5 by increasing 65000 beyond the point of wraparound at u16::MAX-1 = 65534. The assumption only holds if the items you assign serial numbers to have a short enough lifetime. The ordering of items in your state will get messed up if there is an item that is the 32767th successor of another item.

The final value in our number space, u16::MAX, is reserved for the special NAN value. This is done to save space - you don’t need to wrap this type in an Option if only some items are assigned a serial number.

Implementations§

Special value representing “no serial number”.

By convention, we say this “serial number” is less than any other. It cannot be increased, or added to.

Returns true if this number is NAN.

Increases self with wraparound.

Increases self with wraparound, and returns a copy.

Returns a copy of self, and increases self with wraparound.

Distance with wraparound.

For the signed difference, use Self::diff().

If one of the number is NAN, the maximum distance of 32767 is returned. If both are NAN, we say the distance is 0.

Difference with wraparound.

If self < other, the result is negative, and if self > other, the result is positive.

For the unsigned distance, use Self::dist().

If one of the number is NAN, the maximum difference of (-)32767 is returned. If both are NAN, we say the difference is 0.

Trait Implementations§

Addition with wraparound.

You can add any u16 to the serial number, but be aware that due to the wraparound semantics, adding more than (u16::MAX-1)/2 = 32767 leads to a result that is less than self. Adding u16::MAX will wraparound to the same value.

If self.is_nan(), the returned serial number is also NAN.

The resulting type after applying the + operator.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

Compare with wraparound.

By convention, we say NAN is less than any other serial number.

Based on RFC1982.

Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Partial comparison with wraparound.

Returns None if one of the values is NAN.

Based on RFC1982.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.