pub struct OneBasedU64(/* private fields */);Expand description
Represents 1-based index of u64.
To describe configuration by humans, often 1-based index is easier than 0-based to understand.
On the other hand, 0-based index is easier to use in the programming.
Also, it’s quite hard to track if the index is 0-based or 1-based.
$name provides ergonomics to handle user provided 1-baed index safely.
 // Creates from 1-based index
 let v = OneBasedU64::from_one_based(5)?;
 assert_eq!(v.as_zero_based(), 4);
 // Creates from 0-based index
 let v = OneBasedU64::from_zero_based(0)?;
 assert_eq!(v.as_one_based().get(), 1);Implementations§
Source§impl OneBasedU64
 
impl OneBasedU64
Sourcepub const fn from_one_based(v: u64) -> Result<Self, OneBasedError>
 
pub const fn from_one_based(v: u64) -> Result<Self, OneBasedError>
Creates $name from 1-based index value.
Returns error if the given index is zero.
Sourcepub const unsafe fn from_one_based_unchecked(v: u64) -> Self
 
pub const unsafe fn from_one_based_unchecked(v: u64) -> Self
Sourcepub const fn from_one_based_nonzero(v: NonZeroU64) -> Self
 
pub const fn from_one_based_nonzero(v: NonZeroU64) -> Self
Creates $name from 1-based index value as [$nonzerotype].
This will always succeed.
Sourcepub const fn from_zero_based(v: u64) -> Result<Self, OneBasedError>
 
pub const fn from_zero_based(v: u64) -> Result<Self, OneBasedError>
Creates $name from 0-based index value.
Returns error if the given index is MAX value,
as that would case overflow when converted to 1-based.
Sourcepub const unsafe fn from_zero_based_unchecked(v: u64) -> Self
 
pub const unsafe fn from_zero_based_unchecked(v: u64) -> Self
Creates $name from 0-based index value without check.
§Safety
This function results in undefined behavior when v == u64::MAX.
// This should cause undefined behavior
unsafe {
     OneBasedU64::from_zero_based_unchecked(u64::MAX);
}Sourcepub const fn as_zero_based(&self) -> u64
 
pub const fn as_zero_based(&self) -> u64
Returns regular 0-based index.
Sourcepub const fn as_one_based(&self) -> NonZeroU64
 
pub const fn as_one_based(&self) -> NonZeroU64
Returns 1-based index.
Trait Implementations§
Source§impl Clone for OneBasedU64
 
impl Clone for OneBasedU64
Source§fn clone(&self) -> OneBasedU64
 
fn clone(&self) -> OneBasedU64
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
 
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OneBasedU64
 
impl Debug for OneBasedU64
Source§impl Display for OneBasedU64
 
impl Display for OneBasedU64
Source§impl From<OneBasedU16> for OneBasedU64
 
impl From<OneBasedU16> for OneBasedU64
Source§fn from(value: OneBasedU16) -> Self
 
fn from(value: OneBasedU16) -> Self
Converts OneBasedU16 to OneBasedU64.
Source§impl From<OneBasedU32> for OneBasedU64
 
impl From<OneBasedU32> for OneBasedU64
Source§fn from(value: OneBasedU32) -> Self
 
fn from(value: OneBasedU32) -> Self
Converts OneBasedU32 to OneBasedU64.
Source§impl From<OneBasedU64> for OneBasedU128
 
impl From<OneBasedU64> for OneBasedU128
Source§fn from(value: OneBasedU64) -> Self
 
fn from(value: OneBasedU64) -> Self
Converts OneBasedU64 to OneBasedU128.
Source§impl From<OneBasedU8> for OneBasedU64
 
impl From<OneBasedU8> for OneBasedU64
Source§fn from(value: OneBasedU8) -> Self
 
fn from(value: OneBasedU8) -> Self
Converts OneBasedU8 to OneBasedU64.
Source§impl FromStr for OneBasedU64
 
impl FromStr for OneBasedU64
Source§impl Ord for OneBasedU64
 
impl Ord for OneBasedU64
Source§fn cmp(&self, other: &OneBasedU64) -> Ordering
 
fn cmp(&self, other: &OneBasedU64) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
    Self: Sized,
 
fn max(self, other: Self) -> Selfwhere
    Self: Sized,
Source§impl PartialEq for OneBasedU64
 
impl PartialEq for OneBasedU64
Source§impl PartialOrd for OneBasedU64
 
impl PartialOrd for OneBasedU64
Source§impl TryFrom<OneBasedU128> for OneBasedU64
 
impl TryFrom<OneBasedU128> for OneBasedU64
Source§fn try_from(value: OneBasedU128) -> Result<Self, Self::Error>
 
fn try_from(value: OneBasedU128) -> Result<Self, Self::Error>
Attempts to convert OneBasedU128 to OneBasedU64.
Source§type Error = TryFromIntError
 
type Error = TryFromIntError
Source§impl TryFrom<OneBasedU64> for OneBasedU16
 
impl TryFrom<OneBasedU64> for OneBasedU16
Source§fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
 
fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
Attempts to convert OneBasedU64 to OneBasedU16.
Source§type Error = TryFromIntError
 
type Error = TryFromIntError
Source§impl TryFrom<OneBasedU64> for OneBasedU32
 
impl TryFrom<OneBasedU64> for OneBasedU32
Source§fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
 
fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
Attempts to convert OneBasedU64 to OneBasedU32.
Source§type Error = TryFromIntError
 
type Error = TryFromIntError
Source§impl TryFrom<OneBasedU64> for OneBasedU8
 
impl TryFrom<OneBasedU64> for OneBasedU8
Source§fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
 
fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
Attempts to convert OneBasedU64 to OneBasedU8.
Source§type Error = TryFromIntError
 
type Error = TryFromIntError
Source§impl TryFrom<OneBasedU64> for OneBasedUsize
 
impl TryFrom<OneBasedU64> for OneBasedUsize
Source§fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
 
fn try_from(value: OneBasedU64) -> Result<Self, Self::Error>
Attempts to convert OneBasedU64 to OneBasedUsize.
Source§type Error = TryFromIntError
 
type Error = TryFromIntError
Source§impl TryFrom<OneBasedUsize> for OneBasedU64
 
impl TryFrom<OneBasedUsize> for OneBasedU64
Source§fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
 
fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
Attempts to convert OneBasedUsize to OneBasedU64.