Struct ipfs_embed::Block
source · 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<64>, data: Vec<u8, Global>) -> Result<Block<S>, Error>
pub fn new(cid: Cid<64>, data: Vec<u8, Global>) -> 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<64>, data: Vec<u8, Global>) -> Block<S>
pub fn new_unchecked(cid: Cid<64>, data: Vec<u8, Global>) -> Block<S>
Creates a new block without verifying the cid.
sourcepub fn into_inner(self) -> (Cid<64>, Vec<u8, Global>)
pub fn into_inner(self) -> (Cid<64>, Vec<u8, Global>)
Returns the inner cid and data.
sourcepub 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,
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.`
sourcepub fn decode<CD, T>(&self) -> Result<T, Error>where
CD: Codec,
T: Decode<CD>,
<S as StoreParams>::Codecs: Into<CD>,
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()));
sourcepub fn ipld(&self) -> Result<Ipld, Error>where
Ipld: Decode<<S as StoreParams>::Codecs>,
pub fn ipld(&self) -> Result<Ipld, Error>where
Ipld: Decode<<S as StoreParams>::Codecs>,
Returns the decoded ipld.
sourcepub fn references<E>(&self, set: &mut E) -> Result<(), Error>where
E: Extend<Cid<64>>,
Ipld: References<<S as StoreParams>::Codecs>,
pub fn references<E>(&self, set: &mut E) -> Result<(), Error>where
E: Extend<Cid<64>>,
Ipld: References<<S as StoreParams>::Codecs>,
Returns the references.
Methods from Deref<Target = Cid<64>>§
pub fn version(&self) -> Version
pub fn version(&self) -> Version
Returns the cid version.
pub fn hash(&self) -> &Multihash<S>
pub fn hash(&self) -> &Multihash<S>
Returns the cid multihash.
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§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> ProtocolName for Twhere
T: AsRef<[u8]>,
impl<T> ProtocolName for Twhere
T: AsRef<[u8]>,
§fn protocol_name(&self) -> &[u8] ⓘ
fn protocol_name(&self) -> &[u8] ⓘ
The protocol name as bytes. Transmitted on the network. Read more
§impl<T> References<RawCodec> for T
impl<T> References<RawCodec> for T
source§impl<T> ToHex for Twhere
T: AsRef<[u8]>,
impl<T> ToHex for Twhere
T: AsRef<[u8]>,
source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
Encode the hex strict representing
self
into the result. Lower case
letters are used (e.g. f9b4ca
) Read moresource§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
Encode the hex strict representing
self
into the result. Upper case
letters are used (e.g. F9B4CA
) Read more