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