Struct data_encoding::Encoding
source · pub struct Encoding(/* private fields */);
Expand description
Base-conversion encoding
See Specification for technical details or how to define a new one.
Implementations§
source§impl Encoding
impl Encoding
sourcepub fn encode_len(&self, len: usize) -> usize
pub fn encode_len(&self, len: usize) -> usize
Returns the encoded length of an input of length len
See encode_mut
for when to use it.
sourcepub fn encode_mut(&self, input: &[u8], output: &mut [u8])
pub fn encode_mut(&self, input: &[u8], output: &mut [u8])
Encodes input
in output
§Panics
Panics if the output
length does not match the result of encode_len
for the input
length.
§Examples
use data_encoding::BASE64;
let input = b"Hello world";
let output = &mut buffer[0 .. BASE64.encode_len(input.len())];
BASE64.encode_mut(input, output);
assert_eq!(output, b"SGVsbG8gd29ybGQ=");
sourcepub fn encode_append(&self, input: &[u8], output: &mut String)
pub fn encode_append(&self, input: &[u8], output: &mut String)
Appends the encoding of input
to output
§Examples
use data_encoding::BASE64;
let input = b"Hello world";
let mut output = "Result: ".to_string();
BASE64.encode_append(input, &mut output);
assert_eq!(output, "Result: SGVsbG8gd29ybGQ=");
sourcepub fn new_encoder<'a>(&'a self, output: &'a mut String) -> Encoder<'a>
pub fn new_encoder<'a>(&'a self, output: &'a mut String) -> Encoder<'a>
Returns an object to encode a fragmented input and append it to output
See the documentation of Encoder
for more details and examples.
sourcepub fn encode_write(&self, input: &[u8], output: &mut impl Write) -> Result
pub fn encode_write(&self, input: &[u8], output: &mut impl Write) -> Result
Writes the encoding of input
to output
This allocates a buffer of 1024 bytes on the stack. If you want to control the buffer size
and location, use Encoding::encode_write_buffer()
instead.
§Errors
Returns an error when writing to the output fails.
sourcepub fn encode_write_buffer(
&self,
input: &[u8],
output: &mut impl Write,
buffer: &mut [u8],
) -> Result
pub fn encode_write_buffer( &self, input: &[u8], output: &mut impl Write, buffer: &mut [u8], ) -> Result
sourcepub fn encode(&self, input: &[u8]) -> String
pub fn encode(&self, input: &[u8]) -> String
Returns encoded input
§Examples
use data_encoding::BASE64;
assert_eq!(BASE64.encode(b"Hello world"), "SGVsbG8gd29ybGQ=");
sourcepub fn decode_len(&self, len: usize) -> Result<usize, DecodeError>
pub fn decode_len(&self, len: usize) -> Result<usize, DecodeError>
Returns the decoded length of an input of length len
See decode_mut
for when to use it.
§Errors
Returns an error if len
is invalid. The error kind is Length
and the position is the
greatest valid input length.
sourcepub fn decode_mut(
&self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, DecodePartial>
pub fn decode_mut( &self, input: &[u8], output: &mut [u8], ) -> Result<usize, DecodePartial>
Decodes input
in output
Returns the length of the decoded output. This length may be smaller than the output length if the input contained padding or ignored characters. The output bytes after the returned length are not initialized and should not be read.
§Panics
Panics if the output
length does not match the result of decode_len
for the input
length. Also panics if decode_len
fails for the input
length.
§Errors
Returns an error if input
is invalid. See decode
for more details. The are two
differences though:
Length
may be returned only if the encoding allows ignored characters, because otherwise this is already checked bydecode_len
.- The
read
first bytes of the input have been successfully decoded to thewritten
first bytes of the output.
§Examples
use data_encoding::BASE64;
let input = b"SGVsbA==byB3b3JsZA==";
let output = &mut buffer[0 .. BASE64.decode_len(input.len()).unwrap()];
let len = BASE64.decode_mut(input, output).unwrap();
assert_eq!(&output[0 .. len], b"Hello world");
sourcepub fn decode(&self, input: &[u8]) -> Result<Vec<u8>, DecodeError>
pub fn decode(&self, input: &[u8]) -> Result<Vec<u8>, DecodeError>
Returns decoded input
§Errors
Returns an error if input
is invalid. The error kind can be:
Length
if the input length is invalid. The position is the greatest valid input length.Symbol
if the input contains an invalid character. The position is the first invalid character.Trailing
if the input has non-zero trailing bits. This is only possible if the encoding checks trailing bits. The position is the first character containing non-zero trailing bits.Padding
if the input has an invalid padding length. This is only possible if the encoding uses padding. The position is the first padding character of the first padding of invalid length.
§Examples
use data_encoding::BASE64;
assert_eq!(BASE64.decode(b"SGVsbA==byB3b3JsZA==").unwrap(), b"Hello world");
sourcepub fn is_canonical(&self) -> bool
pub fn is_canonical(&self) -> bool
Returns whether the encoding is canonical
An encoding is not canonical if one of the following conditions holds:
- trailing bits are not checked
- padding is used
- characters are ignored
- characters are translated
sourcepub fn specification(&self) -> Specification
pub fn specification(&self) -> Specification
Returns the encoding specification
Trait Implementations§
impl Eq for Encoding
impl StructuralPartialEq for Encoding
Auto Trait Implementations§
impl Freeze for Encoding
impl RefUnwindSafe for Encoding
impl Send for Encoding
impl Sync for Encoding
impl Unpin for Encoding
impl UnwindSafe for Encoding
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)