pub struct DigitSequence(/* private fields */);
Expand description

Immutable sequence of u8 digits.

§Creation

A digit sequence can be created via conversions - which are not always infallible.

§Infallible conversions

  • from any unsigned number:

    use digit_sequence::{Result, DigitSequence};
    
    let sequence: DigitSequence = 985u32.into();
    
    assert_eq!(sequence, [9, 8, 5]);

§Fallible conversions

  • from any signed number - which fails if the number is negative:

    use digit_sequence::{Result, DigitSequence};
    
    let sequence: DigitSequence = 3791i32.try_into()?;
    
    assert_eq!(sequence, [3, 7, 9, 1]);
  • from &str - which fails in case of non-digit (radix 10) characters:

    use digit_sequence::{Result, DigitSequence};
    
    let sequence: DigitSequence = "09240".parse()?;
    
    assert_eq!(sequence, [0, 9, 2, 4, 0]);
  • from a &[u8] - which fails if the numbers are not 0-9 digits:

    use digit_sequence::{Result, DigitSequence};
    
    let source = [0, 9, 2, 4, 0];
    let sequence: DigitSequence = (&source).try_into()?;
    
    assert_eq!(sequence, source);

§Usage

This data structure implements IntoIterator and also provides the iter method - thus enabling standard iterations as well as the Iterator methods.

§for-iterations

§read-only

use digit_sequence::DigitSequence;

let sequence: DigitSequence = 1234567890u128.into();
   
let mut even_vec = vec![];

for &digit in &sequence {
  if digit % 2 == 0 {
    even_vec.push(digit);
  }
}

assert_eq!(even_vec, vec![2, 4, 6, 8, 0]);

§consuming

use digit_sequence::DigitSequence;

let sequence: DigitSequence = 1234567890u128.into();
   
let mut even_vec = vec![];

for digit in sequence {
  if digit % 2 == 0 {
    even_vec.push(digit);
  }
}

assert_eq!(even_vec, vec![2, 4, 6, 8, 0]);

§Iterator methods

§read-only

use digit_sequence::DigitSequence;

let sequence: DigitSequence = 1234567890u128.into();

let even_vec: Vec<u8> = sequence.iter()
  .filter(|&digit| digit % 2 == 0)
  .map(|&digit|digit)
  .collect();

assert_eq!(even_vec, vec![2, 4, 6, 8, 0]);

§consuming

use digit_sequence::DigitSequence;

let sequence: DigitSequence = 1234567890u128.into();

let even_vec: Vec<u8> = sequence.into_iter()
  .filter(|digit| digit % 2 == 0)
  .collect();

assert_eq!(even_vec, vec![2, 4, 6, 8, 0]);

§Conversion to String

  • the Debug trait is derived

  • for the Display trait, the sequence is formatted just by joining its digits

§Serialization

When the serde feature is enabled for this crate, Self implements the [serde::Serialize] and [serde::Deserialize] traits.

Implementations§

source§

impl DigitSequence

source

pub fn iter(&self) -> Iter<'_, u8>

Convenience method for iterating over references to the digits.

Trait Implementations§

source§

impl Clone for DigitSequence

source§

fn clone(&self) -> DigitSequence

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for DigitSequence

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for DigitSequence

source§

fn default() -> DigitSequence

Returns the “default value” for a type. Read more
source§

impl Display for DigitSequence

source§

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

Formats the value using the given formatter. Read more
source§

impl From<u128> for DigitSequence

source§

fn from(value: u128) -> DigitSequence

Converts to this type from the input type.
source§

impl From<u16> for DigitSequence

source§

fn from(value: u16) -> DigitSequence

Converts to this type from the input type.
source§

impl From<u32> for DigitSequence

source§

fn from(value: u32) -> DigitSequence

Converts to this type from the input type.
source§

impl From<u64> for DigitSequence

source§

fn from(value: u64) -> DigitSequence

Converts to this type from the input type.
source§

impl From<u8> for DigitSequence

source§

fn from(value: u8) -> DigitSequence

Converts to this type from the input type.
source§

impl FromStr for DigitSequence

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl Hash for DigitSequence

source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> IntoIterator for &'a DigitSequence

§

type Item = &'a u8

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, u8>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for DigitSequence

§

type Item = u8

The type of the elements being iterated over.
§

type IntoIter = IntoIter<<DigitSequence as IntoIterator>::Item>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl Ord for DigitSequence

source§

fn cmp(&self, other: &DigitSequence) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<&[u8]> for DigitSequence

source§

fn eq(&self, other: &&[u8]) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const N: usize> PartialEq<&[u8; N]> for DigitSequence

source§

fn eq(&self, other: &&[u8; N]) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const N: usize> PartialEq<[u8; N]> for DigitSequence

source§

fn eq(&self, other: &[u8; N]) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Vec<u8>> for DigitSequence

source§

fn eq(&self, other: &Vec<u8>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for DigitSequence

source§

fn eq(&self, other: &DigitSequence) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for DigitSequence

source§

fn partial_cmp(&self, other: &DigitSequence) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl TryFrom<&[u8]> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(digits: &[u8]) -> Result<Self>

Performs the conversion.
source§

impl<const N: usize> TryFrom<&[u8; N]> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(digits: &[u8; N]) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<&Vec<u8>> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: &Vec<u8>) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<i128> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: i128) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<i16> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: i16) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<i32> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: i32) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<i64> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: i64) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<i8> for DigitSequence

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: i8) -> Result<Self>

Performs the conversion.
source§

impl Eq for DigitSequence

source§

impl StructuralPartialEq for DigitSequence

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

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

source§

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

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.