Struct Block

Source
pub struct Block<S> { /* private fields */ }
Expand description

Block

Implementations§

Source§

impl<S> Block<S>
where S: StoreParams,

Source

pub fn new( cid: Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>, data: Vec<u8>, ) -> Result<Block<S>, Error>

Creates a new block. Returns an error if the hash doesn’t match the data.

Source

pub fn new_unchecked( cid: Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>, data: Vec<u8>, ) -> Block<S>

Creates a new block without verifying the cid.

Source

pub fn cid( &self, ) -> &Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>

Returns the cid.

Source

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

Returns the payload.

Source

pub fn into_inner( self, ) -> (Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>, Vec<u8>)

Returns the inner cid and data.

Source

pub fn encode<CE, T>( codec: CE, hcode: <S as StoreParams>::Hashes, payload: &T, ) -> Result<Block<S>, Error>
where CE: Codec + Into<<S as StoreParams>::Codecs>, T: Encode<CE> + ?Sized,

Encode a block.`

Source

pub fn decode<CD, T>(&self) -> Result<T, Error>
where CD: Codec, T: Decode<CD>, <S as StoreParams>::Codecs: Into<CD>,

Decodes a block.

§Example

Decoding to Ipld:

use libipld::block::Block;
use libipld::cbor::DagCborCodec;
use libipld::ipld::Ipld;
use libipld::multihash::Code;
use libipld::store::DefaultParams;

let block =
    Block::<DefaultParams>::encode(DagCborCodec, Code::Blake3_256, "Hello World!").unwrap();
let ipld = block.decode::<DagCborCodec, Ipld>().unwrap();

assert_eq!(ipld, Ipld::String("Hello World!".to_string()));
Source

pub fn ipld(&self) -> Result<Ipld, Error>
where Ipld: Decode<<S as StoreParams>::Codecs>,

Returns the decoded ipld.

Source

pub fn references<E>(&self, set: &mut E) -> Result<(), Error>

Returns the references.

Methods from Deref<Target = Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>>§

Source

pub fn version(&self) -> Version

Returns the cid version.

Source

pub fn codec(&self) -> u64

Returns the cid codec.

Source

pub fn hash(&self) -> &Multihash<S>

Returns the cid multihash.

Source

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

Writes the bytes to a byte stream.

Source

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

Returns the encoded bytes of the Cid.

Source

pub fn to_string_of_base(&self, base: Base) -> Result<String, Error>

Convert CID into a multibase encoded string

§Example
use cid::Cid;
use multibase::Base;
use multihash::{Code, MultihashDigest};

const RAW: u64 = 0x55;

let cid = Cid::new_v1(RAW, Code::Sha2_256.digest(b"foo"));
let encoded = cid.to_string_of_base(Base::Base64).unwrap();
assert_eq!(encoded, "mAVUSICwmtGto/8aP+ZtFPB0wQTQTQi1wZIO/oPmKXohiZueu");

Trait Implementations§

Source§

impl<S> AsRef<[u8]> for Block<S>

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<S> AsRef<Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Block<S>

Source§

fn as_ref( &self, ) -> &Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<S> Borrow<Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Block<S>

Source§

fn borrow( &self, ) -> &Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>

Immutably borrows from an owned value. Read more
Source§

impl<S> Clone for Block<S>
where S: Clone,

Source§

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

Returns a duplicate 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 for Block<S>

Source§

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

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

impl<S> Deref for Block<S>

Source§

type Target = Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Block<S> as Deref>::Target

Dereferences the value.
Source§

impl<S> Hash for Block<S>

Source§

fn hash<SH>(&self, hasher: &mut SH)
where SH: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<S> PartialEq for Block<S>

Source§

fn eq(&self, other: &Block<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> Eq for Block<S>

Auto Trait Implementations§

§

impl<S> Freeze for Block<S>

§

impl<S> RefUnwindSafe for Block<S>
where S: RefUnwindSafe,

§

impl<S> Send for Block<S>
where S: Send,

§

impl<S> Sync for Block<S>
where S: Sync,

§

impl<S> Unpin for Block<S>
where S: Unpin,

§

impl<S> UnwindSafe for Block<S>
where S: UnwindSafe,

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> CallHasher for T
where T: Hash + ?Sized,

Source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ProtocolName for T
where T: AsRef<[u8]>,

Source§

fn protocol_name(&self) -> &[u8]

The protocol name as bytes. Transmitted on the network. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> References<RawCodec> for T

Source§

fn references<R, E>(_c: RawCodec, _r: &mut R, _set: &mut E) -> Result<(), Error>
where R: Read, E: Extend<Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>>,

Scrape the references from an impl Read. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,