use serde::Deserialize;
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
struct Counting;
thread_local! {
static ALLOCATIONS: Cell<usize> = const { Cell::new(0) };
}
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCATIONS.with(|n| n.set(n.get() + 1));
System.alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout)
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
ALLOCATIONS.with(|n| n.set(n.get() + 1));
System.realloc(ptr, layout, new_size)
}
}
#[global_allocator]
static ALLOCATOR: Counting = Counting;
fn allocations<T>(read: impl FnOnce() -> T) -> usize {
let before = ALLOCATIONS.with(Cell::get);
let read = std::hint::black_box(read());
let made = ALLOCATIONS.with(Cell::get) - before;
drop(read);
made
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(dead_code)]
enum Buffered {
Item { n: u64, pad: String },
Ready,
}
#[derive(Debug, plexus_macros::Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(dead_code)]
enum TypeFirst {
Item { n: u64, pad: String },
Ready,
}
#[test]
fn an_event_read_with_its_type_first_allocates_only_what_its_fields_hold() {
let item = r#"{"type":"item","n":4321,"pad":"xxxxxxxxxxxxxxxx"}"#;
assert_eq!(allocations(|| serde_json::from_str::<Buffered>(item).unwrap()), 2);
assert_eq!(allocations(|| serde_json::from_str::<TypeFirst>(item).unwrap()), 1);
let ready = r#"{"type":"ready","n":1,"pad":"x"}"#;
assert_eq!(allocations(|| serde_json::from_str::<Buffered>(ready).unwrap()), 1);
assert_eq!(allocations(|| serde_json::from_str::<TypeFirst>(ready).unwrap()), 0);
let last = r#"{"n":4321,"pad":"xxxxxxxxxxxxxxxx","type":"item"}"#;
assert_eq!(allocations(|| serde_json::from_str::<TypeFirst>(last).unwrap()), 2);
}