[][src]Struct multihash::MultihashGeneric

pub struct MultihashGeneric<T: TryFrom<u64>> { /* fields omitted */ }

Representation of a valid multihash. This enforces validity on construction, so it can be assumed this is always a valid multihash.

This generic type can be used with your own code table.

Example

use multihash::{wrap, MultihashGeneric};
use std::convert::TryFrom;

#[derive(Debug)]
pub enum MyCodeTable {
    Foo = 0x01,
    Bar = 0x02,
}

impl TryFrom<u64> for MyCodeTable {
    type Error = String;

    fn try_from(raw: u64) -> Result<Self, Self::Error> {
        match raw {
            0x01 => Ok(Self::Foo),
            0x02 => Ok(Self::Bar),
            _ => Err("invalid code".to_string()),
        }
    }
}

impl From<MyCodeTable> for u64 {
    fn from(code: MyCodeTable) -> Self {
        code as u64
    }
}

#[derive(Clone, Debug)]
struct SameHash;
impl SameHash {
    pub const CODE: MyCodeTable = MyCodeTable::Foo;
    /// Hash some input and return the sha1 digest.
    pub fn digest(_data: &[u8]) -> MultihashGeneric<MyCodeTable> {
        let digest = b"alwaysthesame";
        wrap(Self::CODE, digest)
    }
}

let my_hash = SameHash::digest(b"abc");
assert_eq!(my_hash.digest(), b"alwaysthesame");

This mechanism can also be used if you want to extend the existing code table

Example

use multihash::Code;
use std::convert::TryFrom;

#[derive(Debug, PartialEq)]
enum ExtendedCode {
    Foo,
    Bar,
    NormalCode(Code),
}

impl TryFrom<u64> for ExtendedCode {
    type Error = String;

    /// Return the `Code` based on the integer value
    fn try_from(raw: u64) -> Result<Self, Self::Error> {
        match raw {
            0x01 => Ok(Self::Foo),
            0x02 => Ok(Self::Bar),
            // Fallback to the default values
            _ => match Code::try_from(raw) {
                Ok(code) => Ok(Self::NormalCode(code)),
                Err(_) => Err("invalid code".to_string()),
            }, //_ => Err("invalid code".to_string()),
        }
    }
}

impl From<ExtendedCode> for u64 {
    fn from(code: ExtendedCode) -> Self {
        match code {
            ExtendedCode::Foo => 0x01,
            ExtendedCode::Bar => 0x02,
            ExtendedCode::NormalCode(normal_code) => normal_code.into(),
        }
    }
}

impl TryFrom<ExtendedCode> for Code {
    type Error = String;

    fn try_from(extended: ExtendedCode) -> Result<Self, Self::Error> {
        match extended {
            ExtendedCode::NormalCode(code) => Ok(code),
            _ => Err("Not a default code".to_string()),
        }
    }
}

assert_eq!(ExtendedCode::try_from(0x02).unwrap(), ExtendedCode::Bar);
assert_eq!(
    ExtendedCode::try_from(0x12).unwrap(),
    ExtendedCode::NormalCode(Code::Sha2_256)
);
assert_eq!(
    Code::try_from(ExtendedCode::try_from(0x12).unwrap()).unwrap(),
    Code::Sha2_256
);

Implementations

impl<T: TryFrom<u64>> MultihashGeneric<T>[src]

pub fn from_bytes(
    bytes: Vec<u8>
) -> Result<MultihashGeneric<T>, DecodeOwnedError>
[src]

Creates a new Multihash from a Vec<u8>, consuming it. If the input data is not a valid multihash an error is returned.

Example

use multihash::{Multihash, Sha2_256};

let mh = Sha2_256::digest(b"hello world");

// valid multihash
let mh2 = Multihash::from_bytes(mh.into_bytes()).unwrap();

// invalid multihash
assert!(Multihash::from_bytes(vec![1, 2, 3]).is_err());

pub fn into_bytes(self) -> Vec<u8>[src]

Returns the bytes representation of the multihash.

pub fn to_vec(&self) -> Vec<u8>[src]

Returns the bytes representation of the multihash.

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

Returns the bytes representation of this multihash.

pub fn as_ref(&self) -> MultihashRefGeneric<'_, T>[src]

Builds a MultihashRef corresponding to this Multihash.

pub fn algorithm(&self) -> T[src]

Returns the algorithm used in this multihash.

Example

use multihash::{Code, Sha2_256};

let mh = Sha2_256::digest(b"hello world");
assert_eq!(mh.algorithm(), Code::Sha2_256);

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

Returns the hashed data.

Trait Implementations

impl<T: TryFrom<u64>> AsRef<[u8]> for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> Borrow<[u8]> for MultihashGeneric<T>[src]

impl<T: Clone + TryFrom<u64>> Clone for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> Debug for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> Deref for MultihashGeneric<T>[src]

type Target = [u8]

The resulting type after dereferencing.

impl<T: TryFrom<u64>> Eq for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> Hash for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> Into<Vec<u8>> for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> Ord for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> PartialEq<MultihashGeneric<T>> for MultihashGeneric<T>[src]

impl<'a, T: TryFrom<u64>> PartialEq<MultihashGeneric<T>> for MultihashRefGeneric<'a, T>[src]

impl<'a, T: TryFrom<u64>> PartialEq<MultihashRefGeneric<'a, T>> for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> PartialOrd<MultihashGeneric<T>> for MultihashGeneric<T>[src]

impl<T: TryFrom<u64>> TryFrom<Vec<u8>> for MultihashGeneric<T>[src]

type Error = DecodeOwnedError

The type returned in the event of a conversion error.

Auto Trait Implementations

impl<T> RefUnwindSafe for MultihashGeneric<T> where
    T: RefUnwindSafe

impl<T> Send for MultihashGeneric<T> where
    T: Send

impl<T> Sync for MultihashGeneric<T> where
    T: Sync

impl<T> Unpin for MultihashGeneric<T> where
    T: Unpin

impl<T> UnwindSafe for MultihashGeneric<T> where
    T: UnwindSafe

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> From<T> for T[src]

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

impl<T> Same<T> for T

type Output = T

Should always be Self

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

type Owned = T

The resulting type after obtaining ownership.

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.