Struct Multihash

Source
pub struct Multihash<S: Size> { /* private fields */ }
Expand description

A Multihash instance that only supports the basic functionality and no hashing.

With this Multihash implementation you can operate on Multihashes in a generic way, but no hasher implementation is associated with the code.

§Example

use tiny_multihash::{Multihash, U64};

const Sha3_256: u64 = 0x16;
let digest_bytes = [
    0x16, 0x20, 0x64, 0x4b, 0xcc, 0x7e, 0x56, 0x43, 0x73, 0x04, 0x09, 0x99, 0xaa, 0xc8, 0x9e,
    0x76, 0x22, 0xf3, 0xca, 0x71, 0xfb, 0xa1, 0xd9, 0x72, 0xfd, 0x94, 0xa3, 0x1c, 0x3b, 0xfb,
    0xf2, 0x4e, 0x39, 0x38,
];
let mh = Multihash::<U64>::from_bytes(&digest_bytes).unwrap();
assert_eq!(mh.code(), Sha3_256);
assert_eq!(mh.size(), 32);
assert_eq!(mh.digest(), &digest_bytes[2..]);

Implementations§

Source§

impl<S: Size> Multihash<S>

Source

pub fn wrap(code: u64, input_digest: &[u8]) -> Result<Self, Error>

Wraps the digest in a multihash.

Source

pub fn code(&self) -> u64

Returns the code of the multihash.

Source

pub fn size(&self) -> u8

Returns the size of the digest.

Source

pub fn digest(&self) -> &[u8]

Returns the digest.

Source

pub fn read<R: Read>(r: R) -> Result<Self, Error>
where Self: Sized,

Reads a multihash from a byte stream.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
where Self: Sized,

Parses a multihash from a bytes.

You need to make sure the passed in bytes have the correct length. The digest length needs to match the size value of the multihash.

Source

pub fn write<W: Write>(&self, w: W) -> Result<(), Error>

Writes a multihash to a byte stream.

Source

pub fn to_bytes(&self) -> Vec<u8>

Returns the bytes of a multihash.

Trait Implementations§

Source§

impl<S: Clone + Size> Clone for Multihash<S>

Source§

fn clone(&self) -> Multihash<S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + Size> Debug for Multihash<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Default + Size> Default for Multihash<S>

Source§

fn default() -> Multihash<S>

Returns the “default value” for a type. Read more
Source§

impl<S: PartialEq + Size> PartialEq for Multihash<S>

Source§

fn eq(&self, other: &Multihash<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S: Size> Copy for Multihash<S>
where <S as ArrayLength<u8>>::ArrayType: Copy,

Source§

impl<S: Eq + Size> Eq for Multihash<S>

Source§

impl<S: Size> StructuralPartialEq for Multihash<S>

Auto Trait Implementations§

§

impl<S> Freeze for Multihash<S>
where <S as ArrayLength<u8>>::ArrayType: Freeze,

§

impl<S> RefUnwindSafe for Multihash<S>

§

impl<S> Send for Multihash<S>

§

impl<S> Sync for Multihash<S>

§

impl<S> Unpin for Multihash<S>
where <S as ArrayLength<u8>>::ArrayType: Unpin,

§

impl<S> UnwindSafe for Multihash<S>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.