use std::path::PathBuf;
#[path = "../tests/support/model_gate_report.rs"]
mod model_gate_report;
#[path = "../tests/support/workspace_root.rs"]
#[allow(dead_code)]
mod workspace_root;
pub(crate) use workspace_root::models_root;
fn root(var: &str, sub: &str) -> PathBuf {
std::env::var_os(var).map_or_else(
|| {
let models = models_root();
if sub.is_empty() {
models
} else {
models.join(sub)
}
},
PathBuf::from,
)
}
#[test]
fn model_gate_report() {
let mut roots: Vec<(&str, PathBuf)> = Vec::new();
if cfg!(feature = "whisper") {
roots.push(("WHISPERKIT_TEST_MODELS", root("WHISPERKIT_TEST_MODELS", "")));
}
if cfg!(feature = "align") {
roots.push((
"ALIGNKIT_TEST_MODELS",
root("ALIGNKIT_TEST_MODELS", "alignkit"),
));
}
if cfg!(feature = "speaker") {
roots.push((
"SPEAKERKIT_TEST_MODELS",
root("SPEAKERKIT_TEST_MODELS", "speakerkit"),
));
roots.push((
"ARGMAX_TEST_MODELS",
root("ARGMAX_TEST_MODELS", "argmax-speakerkit"),
));
}
if cfg!(feature = "granite") {
roots.push((
"EMBEDKIT_TEST_MODELS",
root("EMBEDKIT_TEST_MODELS", "embedkit-granite"),
));
}
model_gate_report::report(&roots);
}
pub(crate) mod alloc_probe {
use std::{
alloc::{GlobalAlloc, Layout, System},
cell::Cell,
};
thread_local! {
static LIVE: Cell<usize> = const { Cell::new(0) };
static PEAK: Cell<usize> = const { Cell::new(0) };
static TOTAL: Cell<usize> = const { Cell::new(0) };
}
#[inline]
fn record_alloc(bytes: usize) {
let _ = TOTAL.try_with(|t| t.set(t.get().saturating_add(bytes)));
let _ = LIVE.try_with(|l| {
let now = l.get().saturating_add(bytes);
l.set(now);
let _ = PEAK.try_with(|p| {
if now > p.get() {
p.set(now);
}
});
});
}
#[inline]
fn record_free(bytes: usize) {
let _ = LIVE.try_with(|l| l.set(l.get().saturating_sub(bytes)));
}
struct Counting;
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr = unsafe { System.alloc(layout) };
if !ptr.is_null() {
record_alloc(layout.size());
}
ptr
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let ptr = unsafe { System.alloc_zeroed(layout) };
if !ptr.is_null() {
record_alloc(layout.size());
}
ptr
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
record_free(layout.size());
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let new_ptr = unsafe { System.realloc(ptr, layout, new_size) };
if !new_ptr.is_null() {
record_free(layout.size());
record_alloc(new_size);
}
new_ptr
}
}
#[global_allocator]
static ALLOCATOR: Counting = Counting;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Allocated {
pub(crate) peak: usize,
pub(crate) total: usize,
}
pub(crate) fn measure<T>(f: impl FnOnce() -> T) -> (T, Allocated) {
let live = LIVE.with(Cell::get);
let total = TOTAL.with(Cell::get);
PEAK.with(|p| p.set(live));
let out = f();
(
out,
Allocated {
peak: PEAK.with(Cell::get).saturating_sub(live),
total: TOTAL.with(Cell::get).saturating_sub(total),
},
)
}
#[test]
fn the_probe_sees_a_buffer_it_is_pointed_at() {
let (v, a) = measure(|| vec![0u8; 4_000_000]);
assert_eq!(v.len(), 4_000_000);
assert!(
a.total >= 4_000_000 && a.peak >= 4_000_000,
"the probe must see a 4 MB allocation it wraps: {a:?}"
);
let (_, released) = measure(|| drop(vec![0u8; 8_000_000]));
assert!(released.total >= 8_000_000, "{released:?}");
}
}
mod variant_shape {
use std::{
fmt,
path::{Path, PathBuf},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Shape {
Unit,
Newtype,
Tuple(usize),
Struct,
}
impl Shape {
pub const fn is_legal(self) -> bool {
matches!(self, Self::Unit | Self::Newtype)
}
}
impl fmt::Display for Shape {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unit => f.write_str("unit"),
Self::Newtype => f.write_str("newtype"),
Self::Tuple(n) => write!(f, "{n}-tuple"),
Self::Struct => f.write_str("struct-shaped"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Variant {
pub line: usize,
pub enum_name: String,
pub name: String,
pub shape: Shape,
}
const fn is_ident_start(b: u8) -> bool {
b.is_ascii_alphabetic() || b == b'_' || b >= 0x80
}
const fn is_ident_continue(b: u8) -> bool {
is_ident_start(b) || b.is_ascii_digit()
}
pub fn blank_noise(src: &str) -> String {
let b = src.as_bytes();
let mut out: Vec<u8> = Vec::with_capacity(b.len());
let mut i = 0;
macro_rules! blank_to {
($end:expr) => {{
let end = $end;
while i < end {
out.push(if b[i] == b'\n' { b'\n' } else { b' ' });
i += 1;
}
}};
}
while i < b.len() {
if is_ident_start(b[i]) {
let start = i;
while i < b.len() && is_ident_continue(b[i]) {
i += 1;
}
let word = &src[start..i];
let prefixed_raw =
matches!(word, "r" | "br" | "cr") && i < b.len() && (b[i] == b'"' || b[i] == b'#');
let prefixed_str = matches!(word, "b" | "c") && i < b.len() && b[i] == b'"';
out.extend_from_slice(word.as_bytes());
if prefixed_raw {
let hashes = {
let h = i;
while i < b.len() && b[i] == b'#' {
i += 1;
}
i - h
};
if i < b.len() && b[i] == b'"' {
out.resize(out.len() + hashes + 1, b' ');
i += 1;
let close = format!("\"{}", "#".repeat(hashes));
let end = src[i..].find(&close).map_or(b.len(), |p| i + p);
blank_to!(end);
blank_to!((end + close.len()).min(b.len()));
} else {
out.resize(out.len() + hashes, b'#');
}
continue;
}
if !prefixed_str {
continue;
}
}
if b[i] == b'/' && i + 1 < b.len() && b[i + 1] == b'/' {
let end = src[i..].find('\n').map_or(b.len(), |p| i + p);
blank_to!(end);
continue;
}
if b[i] == b'/' && i + 1 < b.len() && b[i + 1] == b'*' {
let mut depth = 0usize;
while i < b.len() {
if b[i] == b'/' && i + 1 < b.len() && b[i + 1] == b'*' {
depth += 1;
blank_to!(i + 2);
continue;
}
if b[i] == b'*' && i + 1 < b.len() && b[i + 1] == b'/' {
depth -= 1;
blank_to!(i + 2);
if depth == 0 {
break;
}
continue;
}
blank_to!(i + 1);
}
continue;
}
if b[i] == b'"' {
out.push(b' ');
i += 1;
while i < b.len() {
if b[i] == b'\\' && i + 1 < b.len() {
blank_to!(i + 2);
continue;
}
if b[i] == b'"' {
out.push(b' ');
i += 1;
break;
}
blank_to!(i + 1);
}
continue;
}
if b[i] == b'\'' {
let rest = &src[i + 1..];
let mut chars = rest.chars();
let literal_end = match chars.next() {
Some('\\') => rest.find('\'').map(|p| i + 1 + p + 1),
Some(c) => {
let w = c.len_utf8();
(rest.as_bytes().get(w) == Some(&b'\'')).then_some(i + 1 + w + 1)
}
None => None,
};
match literal_end {
Some(end) => blank_to!(end),
None => {
out.push(b'\'');
i += 1;
}
}
continue;
}
out.push(b[i]);
i += 1;
}
String::from_utf8(out).expect("blanking only ever emits ASCII or whole original bytes")
}
fn skip_balanced(bytes: &[u8], from: usize, open: u8, close: u8) -> usize {
let mut depth = 0usize;
let mut i = from;
while i < bytes.len() {
if bytes[i] == open {
depth += 1;
} else if bytes[i] == close {
depth -= 1;
if depth == 0 {
return i + 1;
}
}
i += 1;
}
bytes.len()
}
fn tuple_arity(body: &str) -> usize {
let b = body.as_bytes();
let (mut paren, mut angle, mut square, mut brace) = (0usize, 0usize, 0usize, 0usize);
let mut fields = 0usize;
let mut saw_content = false;
let mut i = 0;
while i < b.len() {
match b[i] {
b'-' if i + 1 < b.len() && b[i + 1] == b'>' => {
i += 2;
continue;
}
b'(' => paren += 1,
b')' => paren -= 1,
b'[' => square += 1,
b']' => square = square.saturating_sub(1),
b'{' => brace += 1,
b'}' => brace = brace.saturating_sub(1),
b'<' => angle += 1,
b'>' => angle = angle.saturating_sub(1),
b',' if paren == 1 && angle == 0 && square == 0 && brace == 0 => {
if saw_content {
fields += 1;
}
saw_content = false;
i += 1;
continue;
}
c if !c.is_ascii_whitespace() && paren >= 1 => saw_content = true,
_ => {}
}
i += 1;
}
fields + usize::from(saw_content)
}
pub fn variants_in(src: &str) -> Vec<Variant> {
let blanked = blank_noise(src);
let b = blanked.as_bytes();
let mut out = Vec::new();
let mut i = 0;
while i < b.len() {
if !(blanked[i..].starts_with("enum")
&& (i == 0 || !is_ident_continue(b[i - 1]))
&& b.get(i + 4).is_none_or(|&c| !is_ident_continue(c)))
{
i += 1;
continue;
}
let mut j = i + 4;
while j < b.len() && b[j].is_ascii_whitespace() {
j += 1;
}
let name_start = j;
while j < b.len() && is_ident_continue(b[j]) {
j += 1;
}
let enum_name = blanked[name_start..j].to_string();
let Some(open) = blanked[j..].find('{').map(|p| j + p) else {
i += 4;
continue;
};
let close = skip_balanced(b, open, b'{', b'}');
let body_start = open + 1;
let body_end = close.saturating_sub(1);
let body = &blanked[body_start..body_end];
let bb = body.as_bytes();
let (mut paren, mut brace, mut square) = (0usize, 0usize, 0usize);
let mut start = 0usize;
let mut entries: Vec<(usize, &str)> = Vec::new();
for (k, &c) in bb.iter().enumerate() {
match c {
b'(' => paren += 1,
b')' => paren = paren.saturating_sub(1),
b'{' => brace += 1,
b'}' => brace = brace.saturating_sub(1),
b'[' => square += 1,
b']' => square = square.saturating_sub(1),
b',' if paren == 0 && brace == 0 && square == 0 => {
entries.push((body_start + start, &body[start..k]));
start = k + 1;
}
_ => {}
}
}
entries.push((body_start + start, &body[start..]));
for (offset, entry) in entries {
let eb = entry.as_bytes();
let mut k = 0usize;
loop {
while k < eb.len() && eb[k].is_ascii_whitespace() {
k += 1;
}
if k < eb.len() && eb[k] == b'#' {
let br = entry[k..].find('[').map(|p| k + p);
match br {
Some(br) => k = skip_balanced(eb, br, b'[', b']'),
None => break,
}
} else {
break;
}
}
if k >= eb.len() || !is_ident_start(eb[k]) {
continue;
}
let vs = k;
while k < eb.len() && is_ident_continue(eb[k]) {
k += 1;
}
let name = entry[vs..k].to_string();
while k < eb.len() && eb[k].is_ascii_whitespace() {
k += 1;
}
let shape = match eb.get(k) {
Some(b'{') => Shape::Struct,
Some(b'(') => match tuple_arity(&entry[k..]) {
1 => Shape::Newtype,
n => Shape::Tuple(n),
},
_ => Shape::Unit,
};
let line = blanked[..offset + vs].matches('\n').count() + 1;
out.push(Variant {
line,
enum_name: enum_name.clone(),
name,
shape,
});
}
i = close;
}
out
}
pub fn source_root() -> PathBuf {
super::workspace_root::workspace_root()
}
pub fn rust_files(dir: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
let mut stack = vec![dir.to_path_buf()];
while let Some(d) = stack.pop() {
let entries = std::fs::read_dir(&d).unwrap_or_else(|e| panic!("read {}: {e}", d.display()));
for entry in entries {
let path = entry.expect("dir entry").path();
if path.is_dir() {
if path
.file_name()
.is_some_and(|n| n == "target" || n == ".git" || n == "Models")
{
continue;
}
stack.push(path);
} else if path.extension().is_some_and(|e| e == "rs") {
out.push(path);
}
}
}
out.sort();
out
}
}
#[test]
fn no_enum_in_the_workspace_has_a_struct_shaped_or_multi_field_variant() {
use variant_shape::{rust_files, source_root, variants_in};
let root = source_root();
assert!(
root.is_dir(),
"the workspace root must be reachable at {}",
root.display()
);
let files = rust_files(&root);
let mut variants = 0usize;
let mut enums = std::collections::BTreeSet::new();
let mut illegal: Vec<String> = Vec::new();
for file in &files {
let src = std::fs::read_to_string(file).expect("read source");
for v in variants_in(&src) {
variants += 1;
enums.insert((file.clone(), v.enum_name.clone()));
if !v.shape.is_legal() {
illegal.push(format!(
"{}:{} {}::{} is {}",
file.strip_prefix(&root).unwrap_or(file).display(),
v.line,
v.enum_name,
v.name,
v.shape
));
}
}
}
assert!(files.len() >= 200, "only {} .rs files found", files.len());
assert!(enums.len() >= 60, "only {} enums found", enums.len());
assert!(variants >= 250, "only {variants} variants found");
assert!(
illegal.is_empty(),
"variants must be UNIT or a NEWTYPE of exactly one payload \
(rust-type-conventions: never struct-shaped, never a 2-tuple):\n{}",
illegal.join("\n")
);
}
#[test]
fn the_shape_scanner_flags_exactly_the_illegal_shapes() {
use variant_shape::{Shape, variants_in};
let shapes = |src: &str| -> Vec<(String, Shape)> {
variants_in(src)
.into_iter()
.map(|v| (v.name, v.shape))
.collect()
};
assert_eq!(
shapes("enum E { A { x: u32 } }"),
vec![("A".to_string(), Shape::Struct)]
);
assert_eq!(
shapes("enum E { A {} }"),
vec![("A".to_string(), Shape::Struct)]
);
assert_eq!(
shapes("enum E { A(u32, u32) }"),
vec![("A".to_string(), Shape::Tuple(2))]
);
assert_eq!(
shapes("enum E { A() }"),
vec![("A".to_string(), Shape::Tuple(0))]
);
assert_eq!(
shapes("enum E { A(u8, u8, u8) }"),
vec![("A".to_string(), Shape::Tuple(3))]
);
assert_eq!(
shapes(
"enum E { A, B(u32), C = 3, D(BTreeMap<String, f64>), F(Result<A, B>), \
G(fn(u8, u8) -> u8), H(&'static str), I([u8; 4]), J(Vec<Vec<u8>>), K(u32,) }"
),
vec![
("A".to_string(), Shape::Unit),
("B".to_string(), Shape::Newtype),
("C".to_string(), Shape::Unit),
("D".to_string(), Shape::Newtype),
("F".to_string(), Shape::Newtype),
("G".to_string(), Shape::Newtype),
("H".to_string(), Shape::Newtype),
("I".to_string(), Shape::Newtype),
("J".to_string(), Shape::Newtype),
("K".to_string(), Shape::Newtype),
]
);
assert_eq!(
shapes("enum E { #[error(\"a { } b, c\")] A(u32), #[cfg(feature = \"x\")] B }"),
vec![
("A".to_string(), Shape::Newtype),
("B".to_string(), Shape::Unit)
]
);
for hidden in [
"let s = \"enum E { A { x: u32 } }\";",
"// enum E { A { x: u32 } }",
"/// enum E { A { x: u32 } }",
"/* enum E { A { x: u32 } } */",
"/* /* enum E { A { x: u32 } } */ */",
"let s = r#\"enum E { A { x: u32 } }\"#;",
"let s = r\"enum E { A { x: u32 } }\";",
] {
assert!(
variants_in(hidden).is_empty(),
"must not read a declaration out of {hidden}"
);
}
assert_eq!(
shapes("enum A<'a> { X(&'a str) } enum B { Y { z: u8 } }"),
vec![
("X".to_string(), Shape::Newtype),
("Y".to_string(), Shape::Struct)
]
);
assert!(variants_in("let c = '}'; let d = 'enum';").is_empty());
assert_eq!(
shapes("enum E { A(char) } // '\nenum F { B { c: u8 } }"),
vec![
("A".to_string(), Shape::Newtype),
("B".to_string(), Shape::Struct)
]
);
assert!(variants_in("struct Renumber { enumerate: u8 }").is_empty());
assert_eq!(
shapes("fn f() { enum E { A { x: u8 } } }"),
vec![("A".to_string(), Shape::Struct)]
);
}