pub struct OneBasedUsize(/* private fields */);Expand description
Represents 1-based index of usize.
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.
OneBasedUsize provides ergonomics to handle user provided 1-baed index safely.
// Creates from 1-based index
let v = OneBasedUsize::from_one_based(5).unwrap();
assert_eq!(v.as_zero_based(), 4);
// Creates from 0-based index
let v = OneBasedUsize::from_zero_based(0).unwrap();
assert_eq!(v.as_one_based().get(), 1);Implementations§
Source§impl OneBasedUsize
impl OneBasedUsize
Sourcepub const BITS: u32 = 64u32
pub const BITS: u32 = 64u32
The size of this one-based integer type in bits.
This value is equal to usize::BITS.
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this one-based integer type, from_one_based(1).
Sourcepub const MAX: Self
pub const MAX: Self
The largest value that can be represented by this one-based integer type, equal to from_one_based(usize::MAX).
Sourcepub const fn from_one_based(v: usize) -> Option<Self>
pub const fn from_one_based(v: usize) -> Option<Self>
Creates a one-based integer from 1-based index value.
Returns None if the given index is zero.
Note you can define a constant easily given Option::unwrap() or Option::expect() are also const.
const ONE_BASED_TEN: OneBasedUsize = OneBasedUsize::from_one_based(10).expect("10 is non zero");
assert_eq!(ONE_BASED_TEN.as_zero_based(), 9);Sourcepub const unsafe fn from_one_based_unchecked(v: usize) -> Self
pub const unsafe fn from_one_based_unchecked(v: usize) -> Self
Creates a one-based integer from 1-based index value without check.
§Safety
Input must be greater than zero.
Sourcepub const fn from_one_based_nonzero(v: NonZeroUsize) -> Self
pub const fn from_one_based_nonzero(v: NonZeroUsize) -> Self
Creates a one-based integer from non-zero 1-based index value.
Given the underlying value is guaranteed to be non-zero, this will always succeed.
Sourcepub const fn from_zero_based(v: usize) -> Option<Self>
pub const fn from_zero_based(v: usize) -> Option<Self>
Creates a one-based integer from 0-based index value.
Returns None if the given index is MAX value,
as that would cause overflow when converted to 1-based.
Sourcepub const unsafe fn from_zero_based_unchecked(v: usize) -> Self
pub const unsafe fn from_zero_based_unchecked(v: usize) -> Self
Creates a one-based integer from 0-based index value without check.
§Safety
This function results in undefined behavior when v == usize::MAX.
// This should cause undefined behavior
unsafe {
OneBasedUsize::from_zero_based_unchecked(usize::MAX);
}Sourcepub const fn as_zero_based(self) -> usize
pub const fn as_zero_based(self) -> usize
Returns regular 0-based index.
Sourcepub const fn as_one_based(self) -> NonZeroUsize
pub const fn as_one_based(self) -> NonZeroUsize
Returns 1-based index.
Sourcepub const fn checked_add(self, other: usize) -> Option<Self>
pub const fn checked_add(self, other: usize) -> Option<Self>
Adds an unsigned integer to a one-based integer value. Checks for overflow and returns None on overflow.
let one = OneBasedUsize::from_zero_based(1).unwrap();
let two = OneBasedUsize::from_zero_based(2).unwrap();
let max = OneBasedUsize::MAX;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));Sourcepub const fn saturating_add(self, other: usize) -> Self
pub const fn saturating_add(self, other: usize) -> Self
Adds an unsigned integer to a one-based integer value. Returns Self::MAX on overflow.
let one = OneBasedUsize::from_zero_based(1).unwrap();
let two = OneBasedUsize::from_zero_based(2).unwrap();
let max = OneBasedUsize::MAX;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));Trait Implementations§
Source§impl Clone for OneBasedUsize
impl Clone for OneBasedUsize
Source§fn clone(&self) -> OneBasedUsize
fn clone(&self) -> OneBasedUsize
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OneBasedUsize
impl Debug for OneBasedUsize
Source§impl Display for OneBasedUsize
impl Display for OneBasedUsize
Source§impl FromStr for OneBasedUsize
impl FromStr for OneBasedUsize
Source§impl Ord for OneBasedUsize
impl Ord for OneBasedUsize
Source§fn cmp(&self, other: &OneBasedUsize) -> Ordering
fn cmp(&self, other: &OneBasedUsize) -> 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 OneBasedUsize
impl PartialEq for OneBasedUsize
Source§impl PartialOrd for OneBasedUsize
impl PartialOrd for OneBasedUsize
Source§impl TryFrom<OneBasedU128> for OneBasedUsize
impl TryFrom<OneBasedU128> for OneBasedUsize
Source§fn try_from(value: OneBasedU128) -> Result<Self, Self::Error>
fn try_from(value: OneBasedU128) -> Result<Self, Self::Error>
Attempts to convert OneBasedU128 to OneBasedUsize.
Source§type Error = TryFromIntError
type Error = TryFromIntError
Source§impl TryFrom<OneBasedU16> for OneBasedUsize
impl TryFrom<OneBasedU16> for OneBasedUsize
Source§fn try_from(value: OneBasedU16) -> Result<Self, Self::Error>
fn try_from(value: OneBasedU16) -> Result<Self, Self::Error>
Attempts to convert OneBasedU16 to OneBasedUsize.
Source§type Error = TryFromIntError
type Error = TryFromIntError
Source§impl TryFrom<OneBasedU32> for OneBasedUsize
impl TryFrom<OneBasedU32> for OneBasedUsize
Source§fn try_from(value: OneBasedU32) -> Result<Self, Self::Error>
fn try_from(value: OneBasedU32) -> Result<Self, Self::Error>
Attempts to convert OneBasedU32 to OneBasedUsize.
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<OneBasedU8> for OneBasedUsize
impl TryFrom<OneBasedU8> for OneBasedUsize
Source§fn try_from(value: OneBasedU8) -> Result<Self, Self::Error>
fn try_from(value: OneBasedU8) -> Result<Self, Self::Error>
Attempts to convert OneBasedU8 to OneBasedUsize.
Source§type Error = TryFromIntError
type Error = TryFromIntError
Source§impl TryFrom<OneBasedUsize> for OneBasedU128
impl TryFrom<OneBasedUsize> for OneBasedU128
Source§fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
Attempts to convert OneBasedUsize to OneBasedU128.
Source§type Error = TryFromIntError
type Error = TryFromIntError
Source§impl TryFrom<OneBasedUsize> for OneBasedU16
impl TryFrom<OneBasedUsize> for OneBasedU16
Source§fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
Attempts to convert OneBasedUsize to OneBasedU16.
Source§type Error = TryFromIntError
type Error = TryFromIntError
Source§impl TryFrom<OneBasedUsize> for OneBasedU32
impl TryFrom<OneBasedUsize> for OneBasedU32
Source§fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
Attempts to convert OneBasedUsize to OneBasedU32.
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.
Source§type Error = TryFromIntError
type Error = TryFromIntError
Source§impl TryFrom<OneBasedUsize> for OneBasedU8
impl TryFrom<OneBasedUsize> for OneBasedU8
Source§fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
fn try_from(value: OneBasedUsize) -> Result<Self, Self::Error>
Attempts to convert OneBasedUsize to OneBasedU8.