#[cfg(feature = "async-std")]
use async_std::main as async_main;
use hypercore::{HypercoreBuilder, HypercoreError, Storage};
#[cfg(feature = "tokio")]
use tokio::main as async_main;
#[async_main]
async fn main() {
let storage = Storage::new_memory()
.await
.expect("Could not create memory storage");
let mut hypercore = HypercoreBuilder::new(storage)
.build()
.await
.expect("Could not create memory hypercore");
hypercore.append(b"Hello, ").await.unwrap();
hypercore.append(b"from memory hypercore!").await.unwrap();
let batch: &[&[u8]] = &[
b"first value to clear",
b"second value to clear",
b"third value to keep",
];
let new_length = hypercore.append_batch(batch).await.unwrap().length;
hypercore
.clear(new_length - 3, new_length - 1)
.await
.unwrap();
assert!(hypercore.get(2).await.unwrap().is_none());
assert!(hypercore.get(3).await.unwrap().is_none());
assert_eq!(
hypercore.get(4).await.unwrap().unwrap(),
b"third value to keep"
);
println!(
"{}{}",
format_res(hypercore.get(0).await),
format_res(hypercore.get(1).await)
); }
fn format_res(res: Result<Option<Vec<u8>>, HypercoreError>) -> String {
match res {
Ok(Some(bytes)) => String::from_utf8(bytes).expect("Shouldn't fail in example"),
Ok(None) => "Got None in feed".to_string(),
Err(e) => format!("Error getting value from feed, reason = {e:?}"),
}
}