Trait data_encoding::base::Base
[−]
[src]
pub trait Base { fn pad(&self) -> u8; fn val(&self, x: u8) -> Option<u8>; fn bit(&self) -> usize { ... } fn sym(&self, x: u8) -> u8 { ... } }
Generic interface.
A base implementation needs to define at least its padding pad
and its symbol-to-value function val
. Its power of two bit
and
its value-to-symbol function sym
are uniquely defined from the
pad
and val
functions. However, for performance reasons, all
functions may be defined directly.
Vocabulary
We call ascii a 7-bit code represented as a u8
. In other
words, any u8
value with most significant bit cleared (or
equivalently, any u8
value between 0 and 127 inclusive) is an
ascii, and reciprocally.
We call value a b-bit code represented as a u8
, where b is
the power of two of the base, i.e. 4 for base16
and 6 for
base64
for instance. The values for base64
are thus any u8
value from 0 to 63, and similarly the values for base16
are any
u8
value from 0 to 15. More generally, values are any u8
value
between 0 and 2b - 1 inclusive. Each symbol is uniquely
associated to a value.
We call symbol the symbols represented as ascii (as such, only
ascii symbols are allowed). For instance, in base64
, the symbols
are the ascii from A
to Z
, the ascii from a
to z
, the
ascii from 0
to 9
, the ascii +
, and the ascii /
in value
order. And the base16
symbols are the ascii from 0
to 9
and
the ascii from A
to F
.
We call padding the padding represented as ascii (as such, only
ascii padding is allowed). For instance, the ascii =
is used as
the padding for base64
and base16
.
Constraints
The base interface comes with invariants which must be satisfied
by all implementations. Although it cannot be checked, all
implementations must be deterministic, i.e. they never return
two different outputs for the same input and they never panic
(unless specified otherwise). The other constraints are described
in the ValidError
enum and may be
checked by the valid
function. Implementations
should also be pure.
Required Methods
fn pad(&self) -> u8
Returns the padding.
fn val(&self, x: u8) -> Option<u8>
Returns the value of a symbol.
This function defines what the symbols are, to which value they are associated, and the base size:
- A symbol is an input that does not return
None
. - The value of a symbol is its associated output.
- The base size is the number of symbols.
In other words, when val(s)
returns:
Some(v)
:s
is a symbol with valuev
.None
:s
is not a symbol.
Provided Methods
fn bit(&self) -> usize
Returns the power of two of the base.
fn sym(&self, x: u8) -> u8
Returns the symbol of a value.
This function must only be called on values.
Panics
May panic when input is not a value.