Struct BaseCustom

Source
pub struct BaseCustom<T> {
    pub base: u64,
    /* private fields */
}
Expand description

The BaseCustom struct holds the information to perform number conversions via the gen and decimal methods.

A new instance of BaseCustom can be created with either

  • BaseCustom::<char>::new(Vec<char>)
  • BaseCustom::<char>::from_ordinal_range(Range)
  • BaseCustom::<String>::new(String, Option<char>)

If you are going to provide a delimiter you need to use the <String> implementation. A delimiter is optional.

The primitives for BaseCustom get built from the provides characters or string groups and conversion methods are available to use then. String groupings will be single character strings if no delimiter is given, otherwise they may be strings of any length split only by the delimiter provided.

Fields§

§base: u64

The size of the base

Implementations§

Source§

impl BaseCustom<u8>

Source

pub fn new(bytes: &[u8]) -> BaseCustom<u8>

‘new’ creates a new BaseCustom instance and propogates the values for converting numeric bases.

new for BaseCustom<u8> requires a &[u8] as its parameters and units for measuring the custom numeric base will only be one u8 long each.

Source

pub fn gen(&self, input_val: u64) -> Vec<u8>

gen returns a byte sequence computed from positional values the given u64 parameter evalutes to for your custom base

§Example
use base_custom::BaseCustom;

let base2 = BaseCustom::<u8>::new(&[0x00, 0x01]);
assert_eq!(base2.gen(3), vec![0x01, 0x01]);
§Output
vec![0x01, 0x01]
Source

pub fn decimal(&self, input_val: &[u8]) -> u64

decimal returns a u64 value on computed from the units that form the custom base.

§Example
use base_custom::BaseCustom;

let base2 = BaseCustom::<u8>::new(b"01");
assert_eq!(base2.decimal(b"00011"), 3);
§Output
3
Source

pub fn zero(&self) -> u8

Returns the zero value of your custom base

Source

pub fn one(&self) -> u8

Returns the one value of your custom base

Source

pub fn nth(&self, pos: usize) -> Option<u8>

Returns the nth value of your custom base

Like most indexing operations, the count starts from zero, so nth(0) returns the first value, nth(1) the second, and so on.

Source§

impl BaseCustom<char>

Source

pub fn new(chars: Vec<char>) -> BaseCustom<char>

‘new’ creates a new BaseCustom instance and propogates the values for converting numeric bases.

new for BaseCustom<char> requires a Vec<char> as its parameters and units for measuring the custom numeric base will only be one character long each.

Source

pub fn gen(&self, input_val: u64) -> String

gen returns a String computed from the character mapping and positional values the given u64 parameter evalutes to for your custom base

§Example
use base_custom::BaseCustom;

let base2 = BaseCustom::<char>::new(vec!['0','1']);
assert_eq!(base2.gen(3), "11");
§Output
"11"
Source

pub fn char(&self, input_val: usize) -> Option<char>

char returns a char straight from the character mapping. decimal value must be within character range for a Some result.

§Example
use base_custom::BaseCustom;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
assert_eq!(base10.char(9), Some('9'));
§Output
'9'
Source

pub fn decimal<S>(&self, input_val: S) -> u64
where S: Into<String>,

decimal returns a u64 value on computed from the units that form the custom base.

§Example
use base_custom::BaseCustom;

let base2 = BaseCustom::<char>::new(vec!['0','1']);
assert_eq!(base2.decimal("00011"), 3);
§Output
3
Source

pub fn zero(&self) -> &char

Returns the zero value of your custom base

Source

pub fn one(&self) -> &char

Returns the one value of your custom base

Source

pub fn nth(&self, pos: usize) -> Option<&char>

Returns the nth value of your custom base

Like most indexing operations, the count starts from zero, so nth(0) returns the first value, nth(1) the second, and so on.

Source

pub fn from_ordinal_range(range: Range<u32>) -> BaseCustom<char>

Create a custom numeric base from an ascii range of ordinal values

This method currently restricts the ascii character range of the 95 typical characters starting from 32 and ending with 127. If you’d like to use characters outside of this range please use the new method.

Source§

impl BaseCustom<String>

Source

pub fn new<S>(chars: S, delim: Option<char>) -> BaseCustom<String>
where S: Into<String>,

‘new’ creates a new BaseCustom instance and propogates the values for converting numeric bases.

new for BaseCustom<String> requires a String as its first parameter and units for measuring the custom numeric base can be one character long, or many in length. The second parameter is of Option<char> is a delimiter option for determining whether to split the string into single character length strings or possibly multiple length if the delimiter is partitioning the string in such a way.

Source

pub fn gen(&self, input_val: u64) -> String

gen returns a String computed from the character mapping and positional values the given u64 parameter evalutes to for your custom base

§Example
use base_custom::BaseCustom;

let base2 = BaseCustom::<String>::new("01", None);
assert_eq!(base2.gen(3), "11");
§Output
"11"
Source

pub fn decimal<S>(&self, input_val: S) -> u64
where S: Into<String>,

decimal returns a u64 value on computed from the units that form the custom base.

§Example
use base_custom::BaseCustom;

let base2 = BaseCustom::<String>::new("01", None);
assert_eq!(base2.decimal("00011"), 3);
§Output
3
Source

pub fn zero(&self) -> &str

Returns the zero value of your custom base

Source

pub fn one(&self) -> &str

Returns the one value of your custom base

Source

pub fn nth(&self, pos: usize) -> Option<&str>

Returns the nth value of your custom base

Like most indexing operations, the count starts from zero, so nth(0) returns the first value, nth(1) the second, and so on.

Trait Implementations§

Source§

impl<T: Clone> Clone for BaseCustom<T>

Source§

fn clone(&self) -> BaseCustom<T>

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 BaseCustom<String>

Source§

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

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

impl Debug for BaseCustom<char>

Source§

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

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

impl Debug for BaseCustom<u8>

Source§

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

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

impl PartialEq for BaseCustom<String>

Source§

fn eq(&self, other: &BaseCustom<String>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for BaseCustom<char>

Source§

fn eq(&self, other: &BaseCustom<char>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for BaseCustom<u8>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T> Freeze for BaseCustom<T>

§

impl<T> RefUnwindSafe for BaseCustom<T>
where T: RefUnwindSafe,

§

impl<T> Send for BaseCustom<T>
where T: Send,

§

impl<T> Sync for BaseCustom<T>
where T: Sync,

§

impl<T> Unpin for BaseCustom<T>
where T: Unpin,

§

impl<T> UnwindSafe for BaseCustom<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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