pub struct Block<S> { /* private fields */ }
Expand description
Block
Implementations
sourceimpl<S: StoreParams> Block<S>
impl<S: StoreParams> Block<S>
sourcepub fn new(cid: Cid, data: Vec<u8>) -> Result<Self>
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.
sourcepub fn new_unchecked(cid: Cid, data: Vec<u8>) -> Self
pub fn new_unchecked(cid: Cid, data: Vec<u8>) -> Self
Creates a new block without verifying the cid.
sourcepub fn into_inner(self) -> (Cid, Vec<u8>)
pub fn into_inner(self) -> (Cid, Vec<u8>)
Returns the inner cid and data.
sourcepub fn encode<CE: Codec, T: Encode<CE> + ?Sized>(
codec: CE,
hcode: S::Hashes,
payload: &T
) -> Result<Self>where
CE: Into<S::Codecs>,
pub fn encode<CE: Codec, T: Encode<CE> + ?Sized>(
codec: CE,
hcode: S::Hashes,
payload: &T
) -> Result<Self>where
CE: Into<S::Codecs>,
Encode a block.`
sourcepub fn decode<CD: Codec, T: Decode<CD>>(&self) -> Result<T>where
S::Codecs: Into<CD>,
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()));
sourcepub fn references<E: Extend<Cid>>(&self, set: &mut E) -> Result<()>where
Ipld: References<S::Codecs>,
pub fn references<E: Extend<Cid>>(&self, set: &mut E) -> Result<()>where
Ipld: References<S::Codecs>,
Returns the references.
Methods from Deref<Target = Cid>
pub fn write_bytes<W>(&self, w: W) -> Result<(), Error>where
W: Write,
pub fn write_bytes<W>(&self, w: W) -> Result<(), Error>where
W: Write,
Writes the bytes to a byte stream.
pub fn to_string_of_base(&self, base: Base) -> Result<String, Error>
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
impl<S> Eq for Block<S>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more