pub struct Block<S> { /* private fields */ }
Expand description
Block
Implementations§
Source§impl<S> Block<S>where
S: StoreParams,
impl<S> Block<S>where
S: StoreParams,
Sourcepub 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>
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.
Sourcepub 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>
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.
Sourcepub fn cid(
&self,
) -> &Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>
pub fn cid( &self, ) -> &Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>
Returns the cid.
Sourcepub fn into_inner(
self,
) -> (Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>, Vec<u8>)
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.
Sourcepub fn encode<CE, T>(
codec: CE,
hcode: <S as StoreParams>::Hashes,
payload: &T,
) -> Result<Block<S>, Error>
pub fn encode<CE, T>( codec: CE, hcode: <S as StoreParams>::Hashes, payload: &T, ) -> Result<Block<S>, Error>
Encode a block.`
Sourcepub fn decode<CD, T>(&self) -> Result<T, Error>
pub fn decode<CD, T>(&self) -> Result<T, Error>
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<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>>§
Sourcepub 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.
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> AsRef<Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Block<S>
impl<S> AsRef<Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Block<S>
Source§impl<S> Borrow<Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Block<S>
impl<S> Borrow<Cid<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> 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> CallHasher for T
impl<T> CallHasher for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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