[−][src]Struct multihash::MultihashGeneric
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]
bytes: Vec<u8>
) -> Result<MultihashGeneric<T>, DecodeOwnedError>
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]
fn clone(&self) -> MultihashGeneric<T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T: TryFrom<u64>> Debug for MultihashGeneric<T>
[src]
impl<T: TryFrom<u64>> Deref for MultihashGeneric<T>
[src]
impl<T: TryFrom<u64>> Eq for MultihashGeneric<T>
[src]
impl<T: TryFrom<u64>> Hash for MultihashGeneric<T>
[src]
fn hash<H: Hasher>(&self, state: &mut H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T: TryFrom<u64>> Into<Vec<u8>> for MultihashGeneric<T>
[src]
impl<T: TryFrom<u64>> Ord for MultihashGeneric<T>
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[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]
fn eq(&self, other: &MultihashGeneric<T>) -> bool
[src]
#[must_use]fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'a, T: TryFrom<u64>> PartialEq<MultihashRefGeneric<'a, T>> for MultihashGeneric<T>
[src]
fn eq(&self, other: &MultihashRefGeneric<'a, T>) -> bool
[src]
#[must_use]fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T: TryFrom<u64>> PartialOrd<MultihashGeneric<T>> for MultihashGeneric<T>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T: TryFrom<u64>> TryFrom<Vec<u8>> for MultihashGeneric<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for MultihashGeneric<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for MultihashGeneric<T> where
T: Send,
T: Send,
impl<T> Sync for MultihashGeneric<T> where
T: Sync,
T: Sync,
impl<T> Unpin for MultihashGeneric<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for MultihashGeneric<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,