-
Notifications
You must be signed in to change notification settings - Fork 37
/
memory.rs
59 lines (52 loc) · 1.83 KB
/
memory.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#[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;
/// Example about using an in-memory hypercore.
#[async_main]
async fn main() {
// Create a memory storage
let storage = Storage::new_memory()
.await
.expect("Could not create memory storage");
// Build hypercore
let mut hypercore = HypercoreBuilder::new(storage)
.build()
.await
.expect("Could not create memory hypercore");
// Append values
hypercore.append(b"Hello, ").await.unwrap();
hypercore.append(b"from memory hypercore!").await.unwrap();
// Add three values and clear the first two
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();
// The two values return None, but the last one returns correctly
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"
);
// Print values, converting binary back to string
println!(
"{}{}",
format_res(hypercore.get(0).await),
format_res(hypercore.get(1).await)
); // prints "Hello, from memory hypercore!"
}
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:?}"),
}
}