[−][src]Struct ipld_hamt::Hamt
Implementation of the HAMT data structure for IPLD.
Examples
use ipld_hamt::Hamt; let store = db::MemoryDB::default(); let mut map: Hamt<_, _, usize> = Hamt::new(&store); map.set(1, "a".to_string()).unwrap(); assert_eq!(map.get(&1).unwrap(), Some(&"a".to_string())); assert_eq!(map.delete(&1).unwrap(), Some((1, "a".to_string()))); assert_eq!(map.get::<_>(&1).unwrap(), None); let cid = map.flush().unwrap();
Implementations
impl<'a, BS, V, K, H> Hamt<'a, BS, V, K, H> where
K: Hash + Eq + PartialOrd + Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
BS: BlockStore,
H: HashAlgorithm,
[src]
K: Hash + Eq + PartialOrd + Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
BS: BlockStore,
H: HashAlgorithm,
pub fn new(store: &'a BS) -> Self
[src]
pub fn new_with_bit_width(store: &'a BS, bit_width: u32) -> Self
[src]
Construct hamt with a bit width
pub fn load(cid: &Cid, store: &'a BS) -> Result<Self, Error>
[src]
Lazily instantiate a hamt from this root Cid.
pub fn load_with_bit_width(
cid: &Cid,
store: &'a BS,
bit_width: u32
) -> Result<Self, Error>
[src]
cid: &Cid,
store: &'a BS,
bit_width: u32
) -> Result<Self, Error>
Lazily instantiate a hamt from this root Cid with a specified bit width.
pub fn set_root(&mut self, cid: &Cid) -> Result<(), Error>
[src]
Sets the root based on the Cid of the root node using the Hamt store
pub fn store(&self) -> &'a BS
[src]
Returns a reference to the underlying store of the Hamt.
pub fn set(&mut self, key: K, value: V) -> Result<(), Error>
[src]
Inserts a key-value pair into the HAMT.
If the HAMT did not have this key present, None
is returned.
If the HAMT did have this key present, the value is updated, and the old value is returned. The key is not updated, though;
Examples
use ipld_hamt::Hamt; let store = db::MemoryDB::default(); let mut map: Hamt<_, _, usize> = Hamt::new(&store); map.set(37, "a".to_string()).unwrap(); assert_eq!(map.is_empty(), false); map.set(37, "b".to_string()).unwrap(); map.set(37, "c".to_string()).unwrap();
pub fn get<Q: ?Sized>(&self, k: &Q) -> Result<Option<&V>, Error> where
K: Borrow<Q>,
Q: Hash + Eq,
V: DeserializeOwned,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
V: DeserializeOwned,
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map's key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use ipld_hamt::Hamt; let store = db::MemoryDB::default(); let mut map: Hamt<_, _, usize> = Hamt::new(&store); map.set(1, "a".to_string()).unwrap(); assert_eq!(map.get(&1).unwrap(), Some(&"a".to_string())); assert_eq!(map.get(&2).unwrap(), None);
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> Result<bool, Error> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
Returns true
if a value exists for the given key in the HAMT.
The key may be any borrowed form of the map's key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use ipld_hamt::Hamt; let store = db::MemoryDB::default(); let mut map: Hamt<_, _, usize> = Hamt::new(&store); map.set(1, "a".to_string()).unwrap(); assert_eq!(map.contains_key(&1).unwrap(), true); assert_eq!(map.contains_key(&2).unwrap(), false);
pub fn delete<Q: ?Sized>(&mut self, k: &Q) -> Result<Option<(K, V)>, Error> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
Removes a key from the HAMT, returning the value at the key if the key was previously in the HAMT.
The key may be any borrowed form of the HAMT's key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use ipld_hamt::Hamt; let store = db::MemoryDB::default(); let mut map: Hamt<_, _, usize> = Hamt::new(&store); map.set(1, "a".to_string()).unwrap(); assert_eq!(map.delete(&1).unwrap(), Some((1, "a".to_string()))); assert_eq!(map.delete(&1).unwrap(), None);
pub fn flush(&mut self) -> Result<Cid, Error>
[src]
Flush root and return Cid for hamt
pub fn is_empty(&self) -> bool
[src]
Returns true if the HAMT has no entries
pub fn for_each<F>(&self, f: F) -> Result<(), Box<dyn StdError>> where
V: DeserializeOwned,
F: FnMut(&K, &V) -> Result<(), Box<dyn StdError>>,
[src]
V: DeserializeOwned,
F: FnMut(&K, &V) -> Result<(), Box<dyn StdError>>,
Iterates over each KV in the Hamt and runs a function on the values.
This function will constrain all values to be of the same type
Examples
use ipld_hamt::Hamt; let store = db::MemoryDB::default(); let mut map: Hamt<_, _, usize> = Hamt::new(&store); map.set(1, 1).unwrap(); map.set(4, 2).unwrap(); let mut total = 0; map.for_each(|_, v: &u64| { total += v; Ok(()) }).unwrap(); assert_eq!(total, 3);
Trait Implementations
impl<'a, BS: Debug, V: Debug, K: Debug, H: Debug> Debug for Hamt<'a, BS, V, K, H>
[src]
impl<'a, K: PartialEq, V: PartialEq, S: BlockStore, H: HashAlgorithm> PartialEq<Hamt<'a, S, V, K, H>> for Hamt<'a, S, V, K, H>
[src]
pub fn eq(&self, other: &Self) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<BS, V, K, H, '_> Serialize for Hamt<'_, BS, V, K, H> where
K: Serialize,
V: Serialize,
H: HashAlgorithm,
[src]
K: Serialize,
V: Serialize,
H: HashAlgorithm,
Auto Trait Implementations
impl<'a, BS, V, K = BytesKey, H = Sha256> !RefUnwindSafe for Hamt<'a, BS, V, K, H>
impl<'a, BS, V, K, H> Send for Hamt<'a, BS, V, K, H> where
BS: Sync,
H: Send,
K: Send,
V: Send,
BS: Sync,
H: Send,
K: Send,
V: Send,
impl<'a, BS, V, K = BytesKey, H = Sha256> !Sync for Hamt<'a, BS, V, K, H>
impl<'a, BS, V, K, H> Unpin for Hamt<'a, BS, V, K, H> where
H: Unpin,
K: Unpin,
V: Unpin,
H: Unpin,
K: Unpin,
V: Unpin,
impl<'a, BS, V, K, H> UnwindSafe for Hamt<'a, BS, V, K, H> where
BS: RefUnwindSafe,
H: UnwindSafe,
K: UnwindSafe,
V: UnwindSafe,
BS: RefUnwindSafe,
H: UnwindSafe,
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,