Struct libipld::block::Block

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

Block

Implementations§

source§

impl<S: StoreParams> Block<S>

source

pub fn new(cid: Cid, data: Vec<u8>) -> Result<Self>

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

source

pub fn new_unchecked(cid: Cid, data: Vec<u8>) -> Self

Creates a new block without verifying the cid.

source

pub fn cid(&self) -> &Cid

Returns the cid.

source

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

Returns the payload.

source

pub fn into_inner(self) -> (Cid, Vec<u8>)

Returns the inner cid and data.

source

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

Encode a block.`

source

pub fn decode<CD: Codec, T: Decode<CD>>(&self) -> Result<T>
where S::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>
where Ipld: Decode<S::Codecs>,

Returns the decoded ipld.

source

pub fn references<E: Extend<Cid>>(&self, set: &mut E) -> Result<()>
where Ipld: References<S::Codecs>,

Returns the references.

Methods from Deref<Target = Cid>§

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<usize, Error>
where W: Write,

Writes the bytes to a byte stream, returns the number of bytes written.

source

pub fn encoded_len(&self) -> usize

Returns the length in bytes needed to encode this cid into bytes.

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<64>> for Block<S>

source§

fn as_ref(&self) -> &Cid

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

impl<S> Borrow<Cid<64>> for Block<S>

source§

fn borrow(&self) -> &Cid

Immutably borrows from an owned value. Read more
source§

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

source§

fn clone(&self) -> Block<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 for Block<S>

source§

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

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

impl<S> Deref for Block<S>

§

type Target = Cid<64>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<S> Hash for Block<S>

source§

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

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: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

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

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

Scrape the references from an impl Read. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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

§

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

§

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