-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspill_buffer.rs
More file actions
32 lines (25 loc) · 867 Bytes
/
Copy pathspill_buffer.rs
File metadata and controls
32 lines (25 loc) · 867 Bytes
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
//! Auto-spill: handle rare oversize payloads without panicking.
use std::num::NonZeroUsize;
use arena_alligator::FixedArena;
use bytes::BufMut;
fn nz(n: usize) -> NonZeroUsize {
NonZeroUsize::new(n).unwrap()
}
fn main() {
let arena = FixedArena::with_slot_capacity(nz(32), nz(1024))
.auto_spill()
.build()
.unwrap();
let mut buf = arena.allocate().unwrap();
buf.put_slice(&[0xAA; 512]);
assert!(!buf.is_spilled());
let small = buf.freeze();
println!("small: {} bytes, arena-backed", small.len());
let mut buf = arena.allocate().unwrap();
buf.put_slice(&[0xBB; 2048]);
assert!(buf.is_spilled());
let large = buf.freeze();
println!("large: {} bytes, heap-backed after spill", large.len());
let m = arena.metrics();
println!("spills: {}, bytes_live: {}", m.spills, m.bytes_live);
}