[][src]Struct cosmwasm_std::Binary

pub struct Binary(pub Vec<u8>);

Binary is a wrapper around Vec to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.

This is only needed as serde-json-{core,wasm} has a horrible encoding for Vec

Implementations

impl Binary[src]

pub fn from_base64(encoded: &str) -> StdResult<Self>[src]

take an (untrusted) string and decode it into bytes. fails if it is not valid base64

pub fn to_base64(&self) -> String[src]

encode to base64 string (guaranteed to be success as we control the data inside). this returns normalized form (with trailing = if needed)

pub fn as_slice(&self) -> &[u8][src]

pub fn to_array<A>(&self) -> StdResult<A> where
    A: ByteArray
[src]

Copies content into fixed-sized array. The result type A: ByteArray is a workaround for the missing const-generics. A is a fixed-sized array like [u8; 8].

ByteArray is implemented for [u8; 0] to [u8; 32], such that we are limited by 32 bytes for now.

Examples

Copy to array of explicit length

let binary = Binary::from(&[0xfb, 0x1f, 0x37]);
let array: [u8; 3] = binary.to_array().unwrap();
assert_eq!(array, [0xfb, 0x1f, 0x37]);

Copy to integer

let binary = Binary::from(&[0x8b, 0x67, 0x64, 0x84, 0xb5, 0xfb, 0x1f, 0x37]);
let num = u64::from_be_bytes(binary.to_array().unwrap());
assert_eq!(num, 10045108015024774967);

Trait Implementations

impl Clone for Binary[src]

impl Debug for Binary[src]

impl Default for Binary[src]

impl Deref for Binary[src]

Just like Vec, Binary is a smart pointer to u8. This implements *binary for us and allows us to do &*binary, returning a &[u8] from a &Binary. With deref coercions, this allows us to use &binary whenever a &[u8] is required.

type Target = [u8]

The resulting type after dereferencing.

impl<'de> Deserialize<'de> for Binary[src]

Deserializes as a base64 string

impl Display for Binary[src]

impl Eq for Binary[src]

impl<'_> From<&'_ [u8; 0]> for Binary[src]

impl<'_> From<&'_ [u8; 1]> for Binary[src]

impl<'_> From<&'_ [u8; 10]> for Binary[src]

impl<'_> From<&'_ [u8; 11]> for Binary[src]

impl<'_> From<&'_ [u8; 12]> for Binary[src]

impl<'_> From<&'_ [u8; 13]> for Binary[src]

impl<'_> From<&'_ [u8; 14]> for Binary[src]

impl<'_> From<&'_ [u8; 15]> for Binary[src]

impl<'_> From<&'_ [u8; 16]> for Binary[src]

impl<'_> From<&'_ [u8; 17]> for Binary[src]

impl<'_> From<&'_ [u8; 18]> for Binary[src]

impl<'_> From<&'_ [u8; 19]> for Binary[src]

impl<'_> From<&'_ [u8; 2]> for Binary[src]

impl<'_> From<&'_ [u8; 20]> for Binary[src]

impl<'_> From<&'_ [u8; 21]> for Binary[src]

impl<'_> From<&'_ [u8; 22]> for Binary[src]

impl<'_> From<&'_ [u8; 23]> for Binary[src]

impl<'_> From<&'_ [u8; 24]> for Binary[src]

impl<'_> From<&'_ [u8; 25]> for Binary[src]

impl<'_> From<&'_ [u8; 26]> for Binary[src]

impl<'_> From<&'_ [u8; 27]> for Binary[src]

impl<'_> From<&'_ [u8; 28]> for Binary[src]

impl<'_> From<&'_ [u8; 29]> for Binary[src]

impl<'_> From<&'_ [u8; 3]> for Binary[src]

impl<'_> From<&'_ [u8; 30]> for Binary[src]

impl<'_> From<&'_ [u8; 31]> for Binary[src]

impl<'_> From<&'_ [u8; 32]> for Binary[src]

impl<'_> From<&'_ [u8; 4]> for Binary[src]

impl<'_> From<&'_ [u8; 5]> for Binary[src]

impl<'_> From<&'_ [u8; 6]> for Binary[src]

impl<'_> From<&'_ [u8; 7]> for Binary[src]

impl<'_> From<&'_ [u8; 8]> for Binary[src]

impl<'_> From<&'_ [u8; 9]> for Binary[src]

impl<'_> From<&'_ [u8]> for Binary[src]

impl From<[u8; 0]> for Binary[src]

impl From<[u8; 1]> for Binary[src]

impl From<[u8; 10]> for Binary[src]

impl From<[u8; 11]> for Binary[src]

impl From<[u8; 12]> for Binary[src]

impl From<[u8; 13]> for Binary[src]

impl From<[u8; 14]> for Binary[src]

impl From<[u8; 15]> for Binary[src]

impl From<[u8; 16]> for Binary[src]

impl From<[u8; 17]> for Binary[src]

impl From<[u8; 18]> for Binary[src]

impl From<[u8; 19]> for Binary[src]

impl From<[u8; 2]> for Binary[src]

impl From<[u8; 20]> for Binary[src]

impl From<[u8; 21]> for Binary[src]

impl From<[u8; 22]> for Binary[src]

impl From<[u8; 23]> for Binary[src]

impl From<[u8; 24]> for Binary[src]

impl From<[u8; 25]> for Binary[src]

impl From<[u8; 26]> for Binary[src]

impl From<[u8; 27]> for Binary[src]

impl From<[u8; 28]> for Binary[src]

impl From<[u8; 29]> for Binary[src]

impl From<[u8; 3]> for Binary[src]

impl From<[u8; 30]> for Binary[src]

impl From<[u8; 31]> for Binary[src]

impl From<[u8; 32]> for Binary[src]

impl From<[u8; 4]> for Binary[src]

impl From<[u8; 5]> for Binary[src]

impl From<[u8; 6]> for Binary[src]

impl From<[u8; 7]> for Binary[src]

impl From<[u8; 8]> for Binary[src]

impl From<[u8; 9]> for Binary[src]

impl From<Binary> for Vec<u8>[src]

impl From<Vec<u8>> for Binary[src]

impl Hash for Binary[src]

impl JsonSchema for Binary[src]

impl<'_> PartialEq<&'_ [u8]> for Binary[src]

Implement Binary == &[u8]

impl PartialEq<Binary> for Binary[src]

impl PartialEq<Binary> for Vec<u8>[src]

Implement std::vec::Vec<u8> == encoding::Binary

impl<'_> PartialEq<Binary> for &'_ [u8][src]

Implement &[u8] == Binary

impl PartialEq<Vec<u8>> for Binary[src]

Implement encoding::Binary == std::vec::Vec<u8>

impl Serialize for Binary[src]

Serializes as a base64 string

impl StructuralEq for Binary[src]

impl StructuralPartialEq for Binary[src]

Auto Trait Implementations

impl RefUnwindSafe for Binary

impl Send for Binary

impl Sync for Binary

impl Unpin for Binary

impl UnwindSafe for Binary

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.