pub struct Block<S> { /* private fields */ }
Expand description
Block
Implementations§
source§impl<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, T: Encode<CE> + ?Sized>(
codec: CE,
hcode: S::Hashes,
payload: &T,
) -> Result<Self>
pub fn encode<CE, T: Encode<CE> + ?Sized>( codec: CE, hcode: S::Hashes, payload: &T, ) -> Result<Self>
Encode a block.`
sourcepub fn decode<CD: Codec, T: Decode<CD>>(&self) -> Result<T>
pub fn decode<CD: Codec, T: Decode<CD>>(&self) -> Result<T>
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()));
Methods from Deref<Target = Cid>§
sourcepub fn write_bytes<W>(&self, w: W) -> Result<usize, Error>where
W: Write,
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.
sourcepub fn encoded_len(&self) -> usize
pub fn encoded_len(&self) -> usize
Returns the length in bytes needed to encode this cid into bytes.
sourcepub 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§
source§impl<S> PartialEq for Block<S>
impl<S> PartialEq for Block<S>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)