mago_codex/
lib.rs

1use mago_atom::Atom;
2use mago_atom::atom;
3use mago_span::Span;
4
5pub mod assertion;
6pub mod consts;
7pub mod context;
8pub mod diff;
9pub mod differ;
10pub mod flags;
11pub mod identifier;
12pub mod issue;
13pub mod metadata;
14pub mod misc;
15pub mod populator;
16pub mod reference;
17pub mod scanner;
18pub mod signature;
19pub mod signature_builder;
20pub mod symbol;
21pub mod ttype;
22pub mod visibility;
23
24mod utils;
25
26pub fn get_anonymous_class_name(span: Span) -> Atom {
27    use std::io::Write;
28
29    // A 64-byte buffer on the stack. This is ample space for the prefix,
30    // u64 file id, and 2 u32 integers, preventing any chance of a heap allocation.
31    let mut buffer = [0u8; 64];
32
33    // Use a block to limit the scope of the mutable writer
34    // `writer` is a mutable slice that implements `std::io::Write`.
35    let mut writer = &mut buffer[..];
36
37    // SAFETY: We use `unwrap_unchecked` here because we are writing to a fixed-size buffer
38    unsafe {
39        write!(writer, "class@anonymous:{}-{}:{}", span.file_id, span.start.offset, span.end.offset).unwrap_unchecked()
40    };
41
42    // Determine how many bytes were written by checking the length of the original buffer
43    // against what the `writer` had left. This is a common pattern for `io::Write` on slices.
44    let written_len = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());
45
46    atom(
47        // SAFETY: We use `unwrap_unchecked` here because we are certain the bytes
48        // up to `written_len` are valid UTF-8.
49        unsafe { std::str::from_utf8(&buffer[..written_len]).unwrap_unchecked() },
50    )
51}