-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_alloc.rs
More file actions
52 lines (41 loc) · 1.17 KB
/
Copy pathasync_alloc.rs
File metadata and controls
52 lines (41 loc) · 1.17 KB
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
//! Async allocation: wait until capacity is available.
use std::num::NonZeroUsize;
use std::sync::Arc;
use arena_alligator::FixedArena;
use bytes::BufMut;
fn nz(n: usize) -> NonZeroUsize {
NonZeroUsize::new(n).unwrap()
}
#[tokio::main]
async fn main() {
let arena = Arc::new(
FixedArena::with_slot_capacity(nz(2), nz(256))
.build_async()
.unwrap(),
);
let mut buf1 = arena.allocate_async().await;
buf1.put_slice(b"request 1");
let bytes1 = buf1.freeze();
let mut buf2 = arena.allocate_async().await;
buf2.put_slice(b"request 2");
let bytes2 = buf2.freeze();
let arena2 = Arc::clone(&arena);
let waiter = tokio::spawn(async move {
let mut buf = arena2.allocate_async().await;
buf.put_slice(b"waited for this");
buf.freeze()
});
drop(bytes1);
let result = waiter.await.unwrap();
println!(
"async allocation got: {}",
std::str::from_utf8(&result).unwrap()
);
drop(bytes2);
drop(result);
let m = arena.metrics();
println!(
"allocations: {}, frees: {}, bytes_live: {}",
m.allocations_ok, m.frees, m.bytes_live
);
}