Skip to main content

encode_to_validated

Function encode_to_validated 

Source
pub fn encode_to_validated<T: AsRef<[u8]>>(
    base: Base,
    input: T,
) -> EncodedString
Expand description

Encode with the given byte slice and return a validated EncodedString.

This is a convenience function that combines encode with EncodedString::new, providing type-level guarantees that the returned string is a valid multibase encoding.

§Examples

use multi_base::{Base, encode_to_validated};

let encoded = encode_to_validated(Base::Base58Btc, b"hello");
assert_eq!(encoded.base(), Base::Base58Btc);
assert_eq!(encoded.as_str(), "zCn8eVZg");

// Decode directly from the validated string
let decoded = encoded.decode().unwrap();
assert_eq!(decoded, b"hello");

§Performance

This function has the same performance characteristics as encode. The validation overhead is negligible (just checking the base code).

§Panics

Panics if the encoded string fails validation. Since encode always produces a valid multibase string, this is unreachable in practice.