Struct bs58::encode::EncodeBuilder

source ·
pub struct EncodeBuilder<'a, I: AsRef<[u8]>> { /* private fields */ }
Expand description

A builder for setting up the alphabet and output of a base58 encode.

Implementations§

source§

impl<'a, I: AsRef<[u8]>> EncodeBuilder<'a, I>

source

pub fn new(input: I, alpha: &'a Alphabet) -> EncodeBuilder<'a, I>

Setup encoder for the given string using the given alphabet. Preferably use bs58::encode instead of this directly.

source

pub fn with_alphabet(self, alpha: &'a Alphabet) -> EncodeBuilder<'a, I>

Change the alphabet that will be used for encoding.

§Examples
let input = [0x60, 0x65, 0xe7, 0x9b, 0xba, 0x2f, 0x78];
assert_eq!(
    "he11owor1d",
    bs58::encode(input)
        .with_alphabet(bs58::Alphabet::RIPPLE)
        .into_string());
source

pub fn with_check(self) -> EncodeBuilder<'a, I>

Available on crate feature check only.

Include checksum calculated using the Base58Check algorithm when encoding.

§Examples
let input = [0x60, 0x65, 0xe7, 0x9b, 0xba, 0x2f, 0x78];
assert_eq!(
    "QuT57JNzzWTu7mW",
    bs58::encode(input)
        .with_check()
        .into_string());
source

pub fn with_check_version(self, expected_ver: u8) -> EncodeBuilder<'a, I>

Available on crate feature check only.

Include checksum calculated using the Base58Check algorithm and version when encoding.

§Examples
let input = [0x60, 0x65, 0xe7, 0x9b, 0xba, 0x2f, 0x78];
assert_eq!(
    "oP8aA4HEEyFxxYhp",
    bs58::encode(input)
        .with_check_version(42)
        .into_string());
source

pub fn as_cb58(self, expected_ver: Option<u8>) -> EncodeBuilder<'a, I>

Available on crate feature cb58 only.

Include checksum calculated using the CB58 algorithm and version (if specified) when encoding.

§Examples
let input = [0x60, 0x65, 0xe7, 0x9b, 0xba, 0x2f, 0x78];
assert_eq!(
    "oP8aA4HEEyChXhM2",
    bs58::encode(input)
        .as_cb58(Some(42))
        .into_string());
source

pub fn into_string(self) -> String

Available on crate feature alloc only.

Encode into a new owned string.

§Examples
let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
assert_eq!("he11owor1d", bs58::encode(input).into_string());
source

pub fn into_vec(self) -> Vec<u8>

Available on crate feature alloc only.

Encode into a new owned vector.

§Examples
let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
assert_eq!(b"he11owor1d", &*bs58::encode(input).into_vec());
source

pub fn onto(self, output: impl EncodeTarget) -> Result<usize>

Encode onto the given buffer.

Returns the length written onto the buffer.

If the buffer is resizeable it will be extended and the new data will be written to the end of it, otherwise the data will be overwritten from the start.

If the buffer is not resizeable bytes after the final character will be left alone, except up to 3 null bytes may be written to an &mut str to overwrite remaining characters of a partially overwritten multi-byte character.

See the documentation for bs58::encode for an explanation of the errors that may occur.

§Examples
§Vec<u8>
let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
let mut output = b"goodbye world ".to_vec();
bs58::encode(input).onto(&mut output)?;
assert_eq!(b"goodbye world he11owor1d", output.as_slice());
§&mut [u8]
let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
let mut output = b"goodbye world".to_owned();
bs58::encode(input).onto(&mut output[..])?;
assert_eq!(b"he11owor1drld", output.as_ref());
§String
let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
let mut output = "goodbye world ".to_owned();
bs58::encode(input).onto(&mut output)?;
assert_eq!("goodbye world he11owor1d", output);
§&mut str
let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
let mut output = "goodbye world".to_owned();
bs58::encode(input).onto(output.as_mut_str())?;
assert_eq!("he11owor1drld", output);
§Clearing partially overwritten characters
let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
let mut output = "goodbye w®ld".to_owned();
bs58::encode(input).onto(output.as_mut_str())?;
assert_eq!("he11owor1d\0ld", output);

Auto Trait Implementations§

§

impl<'a, I> Freeze for EncodeBuilder<'a, I>
where I: Freeze,

§

impl<'a, I> RefUnwindSafe for EncodeBuilder<'a, I>
where I: RefUnwindSafe,

§

impl<'a, I> Send for EncodeBuilder<'a, I>
where I: Send,

§

impl<'a, I> Sync for EncodeBuilder<'a, I>
where I: Sync,

§

impl<'a, I> Unpin for EncodeBuilder<'a, I>
where I: Unpin,

§

impl<'a, I> UnwindSafe for EncodeBuilder<'a, I>
where I: 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> 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> Same for T

§

type Output = T

Should always be Self
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.