Convert

Struct Convert 

Source
pub struct Convert { /* private fields */ }
Expand description

Convert the radix (base) of digits stored in a vector.

Implementations§

Source§

impl Convert

Source

pub fn new(from: u64, to: u64) -> Self

Create a new converter with from and to bases.

Examples found in repository?
examples/convert.rs (line 4)
3fn main () {
4  let mut base = Convert::new(4,500);
5  let output = base.convert::<u8,u16>(&vec![1,1,1,1,2,2,1,0,2,2,0,0,2,1]);
6  println!["{:?}", output];
7}
More examples
Hide additional examples
examples/perf.rs (line 5)
4fn main () {
5  let mut base = Convert::new(243,9);
6  let start = Time::now();
7  let input: Vec<u8> = (0..5_000_000).map(|i| (i%243u64) as u8).collect();
8  let output = base.convert::<u8,u8>(&input);
9  println!["{}",start.elapsed().as_secs_f64()];
10  println!["{} items", output.len()];
11}
examples/high.rs (line 4)
3fn main () {
4  let mut base = Convert::new(4000000000,700);
5  let output = base.convert::<u32,u16>(&vec![
6    3900000000, 3500004500, 3000000000, 2500000000,
7    2000000000, 1500000000, 1000003300, 2500000000,
8    3000000000, 3700050000, 2400000000, 1250000052
9  ]);
10  println!["{:?}", output];
11}
Source

pub fn new_unaligned(from: u64, to: u64) -> Self

Create a new converter but don’t test for alignment.

Source

pub fn convert<Input, Output>(&mut self, input: &[Input]) -> Vec<Output>
where Output: Copy + Into<u64> + From<u8> + FromU64 + Add<Output, Output = Output> + Div<Output, Output = Output> + Rem<Output, Output = Output>, Input: Copy + Into<u64>,

Perform the conversion on input which contains digits in base self.from. You should specify the Output type so that the target base (self.to) fits. There are no checks to ensure the Output type has room.

For input and output vectors, the least significant digit is at the beginning of the array.

Examples found in repository?
examples/convert.rs (line 5)
3fn main () {
4  let mut base = Convert::new(4,500);
5  let output = base.convert::<u8,u16>(&vec![1,1,1,1,2,2,1,0,2,2,0,0,2,1]);
6  println!["{:?}", output];
7}
More examples
Hide additional examples
examples/perf.rs (line 8)
4fn main () {
5  let mut base = Convert::new(243,9);
6  let start = Time::now();
7  let input: Vec<u8> = (0..5_000_000).map(|i| (i%243u64) as u8).collect();
8  let output = base.convert::<u8,u8>(&input);
9  println!["{}",start.elapsed().as_secs_f64()];
10  println!["{} items", output.len()];
11}
examples/high.rs (lines 5-9)
3fn main () {
4  let mut base = Convert::new(4000000000,700);
5  let output = base.convert::<u32,u16>(&vec![
6    3900000000, 3500004500, 3000000000, 2500000000,
7    2000000000, 1500000000, 1000003300, 2500000000,
8    3000000000, 3700050000, 2400000000, 1250000052
9  ]);
10  println!["{:?}", output];
11}

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.